std::string storage

I want to ask where is an std::string stored? I am little confused about it. What I understand is that it is stored in the stack and it has a pointer to dynamically allocate space in the heap. Is this correct?
Is there any point to declare a string as new inside a class to avoid the class scope (if the class object gets destroyed) and continue on using my std::string?
Is there any point to declare a string as new inside a class to avoid the class scope (if the class object gets destroyed) and continue on using my std::string?

It is your class object that uses the std::string, not you; so to me it makes sense to lose the string if you lose the object which contains it.

If you want to avoid the "class scope", then simply don't declare your std::string as a data member of your class.

A messier alternative would be declaring it as a static member.
http://ideone.com/LZDiar

If I misunderstood your question, I'd be grateful if you tried to rephrase it.
Well the thing is that other objects of the same class might use the same std::string through pointers so I don't want it to be destroyed every time the object that created it gets destroyed.
Well, either:
the other objects need to use their own copy of the string, rather than using a pointer,
or
create the string outside the objects, so you have control over when it is created and destroyed.
You can declare the string as a static data member of the class if all objects of the class have to share the string.
The best solution really depends on what exactly you're trying to accomplish.

Do the objects that use the string never change the string? Do those objects need to change which string they refer to?

One possible solution is to use a variation of the map scheme you were already ready using. You could store the (const) strings in the map as a key with a reference counter as the data and have your objects reference those strings (via pointer or reference depending on the requirements of your objects), increasing or decreasing the reference count as the objects attach or detach themselves from the string. Of course, you cannot change the strings stored this way, as it would screw up the ordering of the map. If you need to 'change' a string you'd need to make a copy, make the changes, and insert the 'changed' string into the map.

If, on the other hand, changes made to a string should be propagated to other objects that refer to the same string, a vector or deque may be a more appropriate container.

Another possibility is just to use a copy-on-write string implementation, or a smart pointer that manages the lifetime of the object.

But, you don't really give enough information about what you're trying to accomplish to really make a recommendation.

Yea I am sorry, I wasn't clear enough.
My basic concern is how and where a std::string allocates memory.
Forget about the rest.
By default, std::string use the default allocator -- std::allocator -- to manage its memory.

Allocator
http://www.cplusplus.com/reference/std/memory/allocator/

And std::allocator uses new and delete, which means heap memory.

Andy
Thank you very much.
Topic archived. No new replies allowed.