0

I am trying to define an array of N = 1000000000 integers on the heap. My code is the following:

#define N 1000000000
int A[N];

But I am getting the following error when I try to compile my code:

enter image description here

This works for smaller numbers so why isn't it working now?

EDIT: After suggestions, I have defined A like this:

int *A = malloc(4000000000ULL);

I am now getting the error: Initializer element is not constant. It should also be noted that this is a global array.

Shivalnu
  • 119
  • 2
  • 16
  • Calculate the amount of memory that would be required for that number of integers. – Ken White Mar 28 '17 at 21:36
  • Does that mean that my system ran out of RAM to allocate? – Shivalnu Mar 28 '17 at 21:38
  • Assuming each `sizeof(int) == 4` you're asking for 4000000000B = 3.73GiB. While you may have so much memory, you should allocate it with `int *A = malloc(1000000000 * sizeof(int));`. – Elmar Peise Mar 28 '17 at 21:38
  • That is on the stack – Ed Heal Mar 28 '17 at 21:39
  • 1
    You can't initialize a file-scope variable with a run-time call to `malloc` like that - you'll need to declare it as `int *A;`, and then in `main` (or some other function), write `A = malloc( 4000000000ULL );`. Note that this request may fail - you may not have a chunk of free memory that large. Always check the result after calling `malloc`. – John Bode Mar 28 '17 at 22:16
  • @JohnBode Is A still a global array if I define it like that? Because although I am not getting the error now and the code compiles, it is crashing after a while. The malloc is successful. – Shivalnu Mar 28 '17 at 22:23
  • @Peter - Since you declared it ouside of any function, the variable `A` is global, yes. – John Bode Mar 28 '17 at 22:26
  • @JohnBode Your previous comment solved my problem. You should post it as an answer. – Shivalnu Mar 28 '17 at 22:38
  • Do the malloc inside `main` – M.M Mar 28 '17 at 22:40

2 Answers2

1

Assuming an int is 32 bits, you're trying to allocate 4GB of space. And you're not allocating it on the heap. You need to call malloc to do that. If this variable is declared in a function, this space it getting allocated on the stack. That's way too big.

dbush
  • 205,898
  • 23
  • 218
  • 273
0

Your declared array is allocated on the stack, rather than the heap. Memory is allocated on the heap by calling malloc.

This leads to the error you're seeing here because the stack has a maximum size that isn't just bounded by the amount of RAM available. Per the MSDN docs, each thread has a max stack size, and this is configurable (or can be set for a PE by the linker).

In practice, it would be unusual to declare an array like this on the stack.

mfsiega
  • 2,852
  • 19
  • 22
  • Caveat: I don't know anything whatsoever about developing on Windows. – mfsiega Mar 28 '17 at 21:50
  • It's on the stack only if the array is declared inside a function. It looks like OP declared it outside, having static storage duration. – vsoftco Mar 28 '17 at 21:50
  • @vsoftco true. In practice, I think the error message the poster is seeing has to do with going outside of the addressable space, rather than where the array lives. – mfsiega Mar 28 '17 at 21:53