C++ array[size] greater than 65535 throwing inconsistent overflow

0

Building a struct array with size [x>65535] throws 0xC00000FD error, even if x is declared as int64_t, but inconsistently. Will work fine in one line, not in the next.

int64_t length;
length = (pull from other code);
Foo foo[length];
//^ this works
Foo2 foo2[length];
//^ this one doesn't

Is this a problem with array construction? C++ max? Compiler/memory limit?

c++
arrays
asked on Stack Overflow Jul 12, 2020 by Kelson Rumak • edited Jul 12, 2020 by Waqar

2 Answers

4

Problem 1

VLA (Variable Length Arrays) aren't in standard C++. This means the following is illegal (even if it works):

int length;
cin >> length;
int array[length];

Problem 2

What is 0xC00000FD error code? It means stack overflow exception. That means you exceeded the stack limit set by your operating system. Since you are on windows, your stack limit by default is 1 MB.

How to fix?

Use the heap.

int* myArray = new int[length];
//or even better
std::vector<int> myArray;
myArray.reserve(length);
answered on Stack Overflow Jul 12, 2020 by Waqar • edited Jul 12, 2020 by Waqar
1

You will blow the stack, as you only have a small stack (I think the default is at around 1MB) and VLA arrays are allocated on the stack. (65535*sizeof(int)==~256kb) 256 kb is not the whole stack, but if you allocate another one, you are at 512kb. That's around the half of the stack. Add call frames and other locals and you will pass the size soon ==> Stackoverflow

answered on Stack Overflow Jul 12, 2020 by JCWasmx86

User contributions licensed under CC BY-SA 3.0