Stack overflow with large array, but not with the same large vector?

Today I came across a ridiculous problem working with large data structures. At first I used the vector to store up to 1,000,000 ints, but later decided that I really did not need the dynamic functionality of the vector (I reserved 1,000,000 points as soon as it was declared anyway), and it would be beneficial to be able to add values โ€‹โ€‹to any place in the data structure. So I switched it to an array and a BAM stack overflow. I assume this is because declaring the size of the array at compile time puts it on the stack and instead uses a dynamic vector in the heap (which, I assume, is bigger?).

So what is the correct answer here? Return to the dynamic memory system to get it in a heap? Increase stack size? Or am I actually getting away from everything here ...?

Thanks!

+3
source share
1 answer

At first I used a vector to store over 1,000,000 ints

A good idea.

but later decided that I really didnโ€™t need the dynamic functions of the vector (I reserved 1,000,000 points as soon as it was declared anyway)

Not such a good idea. You need it.

and it would be useful instead to be able to add values โ€‹โ€‹anywhere in the data structure.

I do not follow.

I assume this is because declaring the size of the array at compile time puts it on the stack and instead uses a dynamic vector in the heap (what do I assume more?).

A lot of. By default, the call stack is typically 1 MB-2 MB in size. Your heap (free store) is really limited by your available RAM.

So what is the correct answer here? Return to the dynamic memory system to get it in a heap?

Yes.

[edit: Joachim right - static is another possible answer.]

Increase stack size?

You could, but even if you could pull 4 MB out of it, you left no room for yourself to view other local data variables. The best used dynamic memory is what the appropriate thing to do.

Or am I leaving the bottom of everything here ...?

No.

+4
source

Source: https://habr.com/ru/post/1447185/


All Articles