Hello
How can I create an array with changing size?
Shuo
Printable View
Hello
How can I create an array with changing size?
Shuo
You can't. Use std::vector instead.
you can usefor dynamically creating a buffer of any size on the fly.Code:Buffer = new char[szBuffer];
but you can't change the size of an existing buffer, at least not that I am aware of...
True. There's no way to change the size of an existing array. The only way is to dynamically allocate, free, reallocate to change size, which is exactly what std::vector does for you.
There is in C. realloc() is a function that will resize an array to a larger size. It will try to expand the memory without moving the array if it can. Pointers passed to realloc() must have been allocated with malloc or calloc, so this optimization can be intrusive in C++, unless it is wrapped in a container. std:vector can never do this, but even if it did, it wouldn't be much of an optimization.
But most cases that you need the array size to change, you should use std::vector or std::deque. Use std::deque if the array can decrease significantly in size, as std::deque is able to decrease the size of it's buffer.
Do exactly what? Try to expand the array without moving the array? That's what the vector does all the time and it is an important optimization.Quote:
std:vector can never do this, but even if it did, it wouldn't be much of an optimization.
From what I see the C++ Standard makes no mention of this, so that is not what vectors do (though if it is technically feasible, a standard library implementor might implement the vector to do it).Quote:
Try to expand the array without moving the array? That's what the vector does all the time and it is an important optimization.
Isn't this what the amortized constant time push_backs mean?
No, it does not. That means that in the long run, the complexity is constant time since the occasional linear time allocation of memory and copying over of elements becomes more and more rare.Quote:
Isn't this what the amortized constant time push_backs mean?
I know vectors create a new block of memory and copy all their elements to the new one, then delete the old block; but wouldn't it be possible for a vector to use realloc() and in-place new to try to keep the same piece of memory?
As you know, realloc isn't safe for C++ memory. Copy constructors for classes aren't called, for example.
Plus from what I understand realloc just tries to allocate a new block of memory if it can't expand the current one and move the data over.
std::vector must use the methods of the Allocator template parameter for its memory management. Since these don't include something realloc-like, it cannot use realloc.
How would you propose that? Once you use realloc, the damage is done.
Yeah, I guess it couldn't use realloc() & placement new (I got the name wrong before) by default since some objects might rely on their own memory address internally... But maybe a custom allocator that uses realloc() & placement new would work if you know how the objects stored in it will handle a change of address behind the scenes.