Hello
How can I create an array with changing size?
Shuo
Hello
How can I create an array with changing size?
Shuo
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...
Last edited by klipseracer; 01-26-2008 at 08:56 PM. Reason: EDIT: additon
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.
Last edited by King Mir; 01-27-2008 at 06:24 AM.
It is too clear and so it is hard to see.
A dunce once searched for fire with a lighted lantern.
Had he known what fire was,
He could have cooked his rice much sooner.
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.std:vector can never do this, but even if it did, it wouldn't be much of an optimization.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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).Try to expand the array without moving the array? That's what the vector does all the time and it is an important optimization.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Isn't this what the amortized constant time push_backs mean?
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
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.Isn't this what the amortized constant time push_backs mean?
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
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?
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.
All the buzzt!
CornedBee
"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
- Flon's Law
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.