Creating your own std::vector like class from scratch

Pages: 123
is this really necessary?
You are commenting on deallocating memory routine. That is nessesary.

Yes. you need to destroy old values, unless destructor is trivial. In your case it is not trivial. (and actually it is too large. Only thing you actually need is: ~myclass() { delete value; } — no test). If your class would be written properly (adhering to rule of 5) then all these destructors would be no-op (after move) and probably optimised away anyway.
You are commenting on deallocating memory routine. That is nessesary.

ups sorry. that was mistake

Also, i meant delete [] value... ups there too

anyways.
lets have vector<vector<int>> a;
now lets make some
vector<int> aa[20];
for( int i = 0; i < 20; i++ ) for( int l = 0; l < 100000; l++ ) aa[i].push_back(l);

now we do this
for( int i = 0; i < 20; i++ ) a.push_back(aa[i]);

when vector needs to grow and reallocate then it would
copy 100000 ints to new array, then destroy 100000 ints from old array?

That would be ... crazy
First of all that operation probably won't trigger reallocation at all.
Even if it would (say you know well when reallocation happens and this is edge case), then yes. It copies 100'000 ints. 20 times. No, you won't notice it. It is just about 8 mb of data passed around in memory. And then you will not see another allocation for a very long time.
then destroy 100000 ints from old array?
As int is trivial type, no destructor calls are made. Just memory deallocation.

You have any other ideas how to grow ungrowable?
Topic archived. No new replies allowed.
Pages: 123