Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it a bad idea to store large vectors in the stack?

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,

like image 478
9a3eedi Avatar asked Jun 07 '12 00:06

9a3eedi


People also ask

Are vectors stored on the heap or stack?

So no matter how you create a vector, its element is always allocated on the heap .

Can a vector be used as a stack?

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.

What happens when vector is full?

The memory allocated won't change but the last element will have its destructor called and the end pointer moves one position down.

Are vector vectors bad?

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.


3 Answers

[...]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 vectors 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.

like image 130
dirkgently Avatar answered Sep 20 '22 19:09

dirkgently


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.

like image 44
Nicol Bolas Avatar answered Sep 20 '22 19:09

Nicol Bolas


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

like image 41
Robert Mason Avatar answered Sep 20 '22 19:09

Robert Mason