1

STL optimizes memory allocation for string objects by providing memory for string objects from a memory pool kept by the standard library. Is it possible to disable this optimization?

I am using VS 2008

Reunanen
  • 7,921
  • 2
  • 35
  • 57
Maanu
  • 5,093
  • 11
  • 59
  • 82
  • 1
    if you tell us why you would want to, there's a greater chance someone could offer you an alternative solution as well. – falstro May 04 '11 at 07:41
  • I am getting a crash while creating an STL string object. But I cannot identify the root cause of the issue. There are may stlstring objects shared by multiple threads. I want to narrow the debugging process by allocating memory for each stl string object from the heap – Maanu May 04 '11 at 07:43
  • does your code run fine if you omit creation of stl::string objects? – asami May 04 '11 at 07:52
  • 2
    Trust me, you don't want to do this . The only real way to debug multi-threaded code is to sit down, away from the computer, and think hard, not to start adding more layers of code that could have bugs. The standard library will have been far more thoroughly debugged than anything you are likely to write. –  May 04 '11 at 07:53

2 Answers2

3

No, you can't. From the C++ reference on string::string:

Except for the copy constructor, an optional final parameter exists for all basic_string constructors, whose type is its Allocator template argument. This parameter influences the memory allocation model to be used for the object. To provide a better readability, and because under no known compiler implementation the memory allocation model for strings (allocator) is affected by its value, it has not been included in the declarations above, but see the basic template member declaration ahead for a more complete declaration.

Jordan Lewis
  • 16,900
  • 4
  • 29
  • 46
1

The following question will help you in understanding how the std::basic_string can be manipulated to be used with various allocators

How do I allocate a std::string on the stack using glibc's string implementation?

Community
  • 1
  • 1
asami
  • 773
  • 6
  • 12