Kelson Rumak
Kelson Rumak

Reputation: 31

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

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?

Upvotes: 0

Views: 396

Answers (2)

Waqar
Waqar

Reputation: 9376

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);

Upvotes: 4

JCWasmx86
JCWasmx86

Reputation: 3583

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

Upvotes: 1

Related Questions