I've been working on a bunch of image processing programs.. nothing fancy, mostly experimenting quick and dirty. The image data is stored in vectors which are declared on the stack (I try to avoid having to use pointers when I don't need to pass data around). I've noticed that some of my functions have been behaving very strangely despite countless amounts of debugging and stepping. Sometimes the debugger would give me an error that it cannot evaluate a certain variable, among other things. Things generally just do not make sense, and past experience tells me that when this happens this means that there is some kind of overflow or memory corruption going on. The first thing that came to mind was that it was probably due to me storing lots of image data into vectors.
However, I was under the impression that vectors store their actual data in the heap, and so I thought it wouldn't hurt to have a few of these large vectors on the stack. Am I wrong in thinking this? Should I be allocating my vectors and storing them in the heap rather than the stack?
Thanks,
So no matter how you create a vector, its element is always allocated on the heap .
So, a vector can work as a stack, but a stack cannot work as a vector, because you cannot insert or get an element at a random position.
The memory allocated won't change but the last element will have its destructor called and the end pointer moves one position down.
Using a vector of vectors: Is inefficient in terms of memory allocation, due to multiple blocks being allocated. Models a jagged right hand edge, so bugs can creep in.
[...]vectors store their actual data in the heap
vector
, like all other containers, uses an allocator object for memory management. Typically, if you don't specify anything as the template's second parameter, the default allocator -- std::allocator
from <memory>
-- is used. It is the allocator's responsibility to reserve memory. It is free to do so either from the free-store or on the stack.
Most implementations typically use the pimpl idiom and store a pointer within the vector
object which points to the actual memory on the free-store.
I've noticed that some of my functions have been behaving very strangely despite countless amounts of debugging and stepping
You may want to check that you are using your vector
s properly. Look up the standard as to what gurantees you get with each member function, what conditions must be satisfied for the contained types and when your iterators get invalidated. That should be a good start.
std::vector
does not store its memory within itself. It allocates memory from the heap (or wherever your allocator gets it from). So whether the vector
itself is on the stack is irrelevant.
I would be willing to say that 99.9% of vector implementations store all of their data in the heap. Maybe somebody out there made a stack implementation, but you're probably not dealing with that. If random, intermittent failures are occurring a corner case not getting checked with pointer arithmetic is more likely the case. Either way, we can't know without you posting code
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With