Is it nice to store large vectors on the stack?

I worked on a bunch of image processing programs. Nothing unusual, mostly experimenting quickly and dirty. Image data is stored in vectors declared on the stack (I try to avoid using pointers when I don't need to pass data). I noticed that some of my functions behave very strange, despite the myriad of debugging and stepping. Sometimes the debugger gave me an error, which, in particular, could not evaluate a certain variable. Things, as a rule, simply do not make sense, and past experience tells me that when this happens, it means that there is some kind of memory overflow or corruption. The first thing that came to mind was that it was probably due to the fact that I stored a lot of image data in vectors.

However, I got the impression that the vectors store their actual data on the heap, and so I thought it would not hurt to have several of these large vectors on the stack. Am I really wrong about that? Should I allocate my vectors and store them on the heap, and not on the stack?

Thank,

+5
source share
3 answers

[...] vectors store their actual data on the heap

vector, like all other containers, uses a allocator to manage memory. As a rule, if you do not specify anything as the second parameter of the template, the default allocator is used - std::allocatorfrom <memory>-. Responsibility for the reservation lies with the distributor. This can be done either from free storage or from the stack.

pimpl vector, .

, ,

, vector. , -, . .

+7

std::vector . ( , ). , , vector , .

+3

I would like to say that 99.9% of vector implementations store all their data on the heap. Maybe someone out there made a stack implementation, but you probably aren't dealing with this. If random, intermittent failures occur, then an angle that does not receive a check using pointer arithmetic is more likely. In any case, we cannot know if you do not post the code.

+1
source

All Articles