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?

Waqar
  • 8,558
  • 4
  • 35
  • 43
  • 1
    Are `foo` and `foo2` allocated on heap (global or static) or stack (non-static local)? In latter case, it may be stack overflow. – MikeCAT Jul 12 '20 at 18:26
  • 1
    Ah, they should be allocated on stack because `static` is not used and they shouldn't be global because variable-length array is used. – MikeCAT Jul 12 '20 at 18:28
  • Possible duplicate: [c++ - Segmentation fault on large array sizes - Stack Overflow](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) (I cannot vote because I first voted as debugging detail required) – MikeCAT Jul 12 '20 at 18:29
  • 3
    VLAs aren't in C++. Also stack has a limit which you might want to change or just use the heap like most people – Waqar Jul 12 '20 at 18:30
  • Read about `malloc` or `new` in C++. It is the right way for variable array of structures. – i486 Jul 12 '20 at 18:30
  • 5
    You should use `std::vector foo(length);` – MikeCAT Jul 12 '20 at 18:30
  • Does this answer your question? [Segmentation fault on large array sizes](https://stackoverflow.com/questions/1847789/segmentation-fault-on-large-array-sizes) – Marcin Orlowski Jul 12 '20 at 19:10

2 Answers2

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);
Waqar
  • 8,558
  • 4
  • 35
  • 43
  • No VLA, got it. `Foo myArray = new Foo[length];` breaks, `error: conversion from 'Foo*' to non-scalar type 'Foo' requested` Trying the vector solution... – Kelson Rumak Jul 12 '20 at 19:00
  • It should be `Foo* myArray = new Foo[length];` – Waqar Jul 12 '20 at 19:01
  • Got `vector foo(input);` to work nicely. Is there an advantage of one method over the other? – Kelson Rumak Jul 12 '20 at 19:15
  • 2
    Yes, you don't have to deallocate memory for vector. for the other you have to `delete[] myArray` to deallocate the memory. Use `vector`, highly recommended. – Waqar Jul 12 '20 at 19:27
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

JCWasmx86
  • 3,473
  • 2
  • 11
  • 29