Quote Originally Posted by Elysia View Post
"a" is 2 bytes and a string object also keeps track of length, so I would say it takes up at least 2 + 4 + 4 = 10 bytes of memory.
An empty string takes up 8 or 9 bytes I would say. But it really depends on the implementation. I don't think there's any mention in the C++ standard as to what size the object should be.
(Size [unsigned integer, 4 bytes] + string [char pointer, 4 bytes] + string data [chars, 0 bytes+]).
So if we build on this, 9 bytes for an empty string = 9 * 1 000 000 bytes = ~8.5 MB.
(This does not take into account vector overhead for each element, if any.)
In reality on Windows it is probably much more than that.
4 bytes for the pointer to the string on the heap,
4 bytes for the size,
Then there's the string on the heap which as you know requires at least 2 bytes. Your system probably wont dynamically allocate any less that 8 bytes at a time from the heap, so that's another 8 bytes.
So, 16 bytes per string = 16000000 bytes, or over 15.2MB.

(This is assuming that the implementation doesn't used shared COW strings for small strings, which modern compilers don't seem to any more, or so I've read)