Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Are you sure about that? I don't really know the internals, but I would assume the implementation looks something like: Let the compiler put the array (of characters or numbers) somewhere where I can get them, and I'll take that pointer and keep it in my stack-allocated STL data structure.

Any references that say you're right?



The best reference is the source code.

Let's look at the string. In C/C++ there is no reliable and portable way for your program to find out whether some (char*) points to a constant string literal or not. So the only oprion for a dynamic string implementation is to always copy any data the constructor receives to somewhere else, normally to the dynamic memory. As a result, any initialization involves dynamic allocation and copying of data in C++.

At the same time languages that have built-in support for dynamic structures avoid this by creating proper structures in the constant segment. So a statement like string s = "abc" would do nothing but assign a pointer.

This is a huge problem in C++ and even C++0x doesn't solve it unfortunately.



I'm not convinced it's solving the problem I mentioned, but I'll try to figure out myself, since this becomes off-topic already.


Looks exactly like what was missing. Sweet.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: