4

I ran into a funny issue today working with large data structures. I initially was using a vector to store upwards of 1000000 ints but later decided I didn't actually need the dynamic functionality of the vector (I was reserving 1000000 spots as soon as it was declared anyway) and it would be beneficial to, instead, be able to add values any place in the data structure. So I switched it to an array and BAM stack overflow. I'm guessing this is because declaring the size of the array at compile time puts it in the stack and making use of a dynamic vector instead placed it on the heap (which I'm guessing is larger?).

So what's the right answer here? Move back to a dynamic memory system just so it gets put on the heap? Increase the size of the stack? Or am I way off base on the whole thing here...?

Thanks!

3
  • Well, you could make the array to be global (or static) and it won't use stack space. The reason that you get a stack overflow is because all local variable (including arguments) are on the stack, that includes all memory for arrays. Commented Nov 20, 2012 at 1:28
  • Bang on Joachim! Static saves the day again! Commented Nov 20, 2012 at 1:31
  • A question about an actual instance of a stack overflow... Commented Nov 20, 2012 at 1:48

1 Answer 1

4

I initially was using a vector to store upwards of 1000000 ints

Good idea.

but later decided I didn't actually need the dynamic functionality of the vector (I was reserving 1000000 spots as soon as it was declared anyway)

Not such a good idea. You did need it.

and it would be beneficial to, instead, be able to add values any place in the data structure.

I don't follow.

I'm guessing this is because declaring the size of the array at compile time puts it in the stack and making use of a dynamic vector instead placed it on the heap (which I'm guessing is larger?).

Much. The call stack is typically of the order of 1MB-2MB in size by default. Your "heap" (free store) is only really bounded by your available RAM.

So what's the right answer here? Move back to a dynamic memory system just so it gets put on the heap?

Yes.

[edit: Joachim's right — static is another possible answer.]

Increase the size of the stack?

You could but even if you could stretch 4MB out of it, you've left yourself no wiggle room for other local data variables. Best use dynamic memory — that's the appropriate thing to do.

Or am I way off base on the whole thing here...?

No.

Sign up to request clarification or add additional context in comments.

12 Comments

On Windows I think the default stack size is 1 MB.
@BenjaminLindley: I did mean 8KB, but I admit that I was mostly making that part up.
Got it, back to the vector it is. For future reference, is there any way to force a non-dynamic array to be put on the heap instead of the stack?
@Hoyt: int* ptr = new int[1000000]; /* f(ptr); */ delete[] ptr;
@Hoyt: in principle it depends on the OS, in practice it's dynamically allocated by the loader. The executable contains metadata how much memory is needed, and when the executable is loaded it's given the right amount. Compilers for properly-embedded systems without an OS might dedicate the whole resources of the device to one program, so can just decide what physical address to put the statics at and configure malloc to allocate from elsewhere.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.