Richard said:
My array of strings is very large. I did a small test using char* vs string
and the results were really bad. I must be doing something wrong:
vector<char*> test;
test.resize(10000000);
for(int t = 0; t != 10000000; ++t)
{
test[t] = "TEST 123";
}
That takes up a minimal amount of memory if using char*.
Yeah... You got a vector all elements of which are the same. No extra
memory is allocated, just 10 million pointers. The consumption of memory
(not considering the overhead for dynamic memory management) is quite easy
to calculate:
sizeof(vector<char*>) +
sizeof(char*) * test.size() + sizeof("TEST 123")
(which should give about 40000000, give or take a few bytes)
> But if you change
it to vector<string> test; it takes up around 1000 times more memory! What
am I doing wrong?
I am not sure, to be honest with you. Every 'string' maintains its own
array of char internally. When you resize the 'test' vector to contain
ten millions of 'string' objects, it first puts a bunch of empty ones
there, and then when you in the loop assign those values, every string
in the vector needs to allocate its own small array (and possibly a bit
larger than asked for), which may lead to severe fragmentation of memory,
especially considering that a temporary is probably created to accommodate
your "TEST 123" literal... The final memory cost should be around
sizeof(vector<string>) +
sizeof(string) * test.size() +
sizeof("TEST 123") * test.size()
It is hard to believe it takes "around 1000 times more memory". The
string objects themselves are not that big, so you might be looking at
four, maybe ten, times the memory consumption, but definitely not the
thousand times. Are you running on a 64-bit machine? 1000 times more
with a vector of 10 million pointers is beyond what a 32-bit machine can
handle, that's for sure.
V