views:

234

answers:

5

If you write int m[1000000]; inside the main function of C/C++, it will get a runtime error for stack overflow. Instead if you write vector<int> m; and then push_back 1000000 elements there, it will run fine.

I am very curious about why this is happening. They both are local memory, aren't they? Thanks in advance.

+6  A: 

Yes, the vector itself is an automatic (stack) object. But the vector holds a pointer to its contents (an internal dynamic array), and that will be allocated on the heap (by default). To simplify a little, you can think of vector as doing malloc/realloc or new[] calls internally (actually it uses an allocator).

EDIT: As I noted, automatic variables are allocated on the stack, while malloc generally allocates on the heap. The available memory for each are platform and even configuration-specific, but the available stack memory is typically much more limited.

Matthew Flaschen
you forgot to explain the size limitations of heap vs. stack. malloc / realloc / new are all dandy, but they don't actually explain anything..
ianmac45
@matthew-flaschen : This might be slightly unrelated but I have a problem around it. Now If i want to provide custom allocator for these classes and which will be of different sizes (for sure), I have to use placement new and override the new / delete. Now the normal memory managers gets fragmented, so i have posted a question here, http://stackoverflow.com/questions/3920453/custom-memory-allocator-manager-in-c-which-approach.
yadab
@ianmac45, the question was whether they were both "local memory". As I explained, `vector` uses heap ("non-local") memory. I'll elaborate on the size issues.
Matthew Flaschen
+1  A: 

The vector object itself is on the stack; but internally it will allocate memory from the heap as needed to store an arbitary number of elements. So the stack cost is small and fixed for it.

Edmund
+6  A: 

The amount of stack memory is limited, because it has to be reserved in advance. However, the amount of heap memory will typically expend up to much higher limits imposed by your OS, but "nearly" reaching the limits of your virtual address space (2GB for a 32-bit machine, a whole lot more for a 64-bit machine).

You can increase the amount of reserved stack space, typically as a setting to your linker.

André Caron
You forgot to mention that this is system-specific. On GNU/Linux, you can use `ulimit -s` to increase the stack size right before executing.
Matthew Flaschen
Yes, I did forget that!
André Caron
A: 

You can't write vector<int> in C. "vector" is a C++ class that hides the complexity of the underlying memory management. vector m is still a "local variable", but under the covers, it automatically manages the allocation of memory from a separate pool using the special capabilities of C++ classes.

nobar
+1  A: 

int m[1000000] - It will allocate the 1000000 ints on stack. as stack is limited so it will throw the stack overflow runtime error.

vector m; and then push_back of 1000000 elements are working because internally vector allocates the memory on heap not on stack. so in your application stack only the vector object is present so it is not throwing the stack overflow runtime error.

Dhiraj