space in string

This is a discussion on space in string within the C++ Programming forums, part of the General Programming Boards category; Hello everyone,
The space in string should use heap address memory space, not stack, right? But through debugging, for example,
...

space in string

Hello everyone,

The space in string should use heap address memory space, not stack, right? But through debugging, for example,

Code:

string str = "hello";

why I can not see the invocation of new operator? Anyone could point out where STL string class allocates space on heap and using which function to allocate please (any other approach other than using new to allocate space on heap?)?

That's an interesting question. I don't know which function std::string calls to allocate memory. I could only guess to the best of my ability which wouldn't get you very far. I (and everyone else) could probably help you more if I knew why you were looking for std::string to call the new operator in the first place. And yes, there are other ways to allocate memory besides using the new operator.

Some string implementations keep a small buffer (like maybe 16 bytes) for short strings to avoid dynamically allocating small buffers.

Try it with a string more than 16 characters. If that doesn't work, keep adding to the string in a loop, eventually it will have to allocate memory. If that doesn't work, you aren't trapping the allocation properly.