1

Possible Duplicate:
Getting a stack overflow exception when declaring a large array

My system is x86-64 linux and here is my simple code :

#include<stdio.h>
#define N 1024
int main()
{
    int a[N][N];
    int b[N][N];
    printf("hello world\n");
    return 0;
}

And its assembly code from objdump:

00000000004004fc <main>:

4004fc: 55                      push   %rbp
4004fd: 48 89 e5                mov    %rsp,%rbp
400500: 48 81 ec 00 00 80 00    sub    $0x800000,%rsp
400507: bf c4 05 40 00          mov    $0x4005c4,%edi
40050c: e8 cf fe ff ff          callq  4003e0 <puts@plt>
400511: b8 00 00 00 00          mov    $0x0,%eax
400516: c9                      leaveq 
400517: c3                      retq   
400518: 0f 1f 84 00 00 00 00    nopl   0x0(%rax,%rax,1)
40051f: 00 

The weird thing is this program will break down when calling printf() function. However, if I define N to be 512, this program works well. I have no idea why. Is there any stack size limitation which limits the memory use by stack?

Does anybody know why? Thanks.

Community
  • 1
  • 1
coinsyx
  • 643
  • 2
  • 7
  • 13

4 Answers4

2

Error due to exceeding stack size. To get rid of this create the arrays in heap. To do that use malloc or other dynamic memory allocation functions.

Like int *a = malloc(N*N*sizeof(int)) this way memory is located in heap.
Also you should test if this memory has been allocated for you by checking:

if(a)
     // do stuff with a
hmatar
  • 2,437
  • 2
  • 17
  • 27
1

Yes there is a maximum stack size which is small, most of the times smaller than a few KBs. You are trying to allocate 1024*1024*sizeof(int)=4194304bytes or 4MBs of data into a single stack allocation which results in a crash.

There are 2 ways to counter this:

1) Allocate the memory outside of the stack

#include<stdio.h>
#define N 1024

int a[N][N];
int b[N][N];    

int main()
{
    printf("hello world\n");
    return 0;
}

or 2) dynamically allocate memory from the heap inside main() using malloc():

    #include<stdio.h>
    #define N 1024

    int main()
    {
        int **a = malloc(N, sizeof(int*));
        int **b = malloc(N, sizeof(int*));
        for (int i=0; i<N; i++)
        {
          a[i]=malloc(N, sizeof(int));
          b[i]=malloc(N, sizeof(int));
         }
        printf("hello world\n");
        return 0;
    }

Note: Do not forget to free() any dynamically allocated memory after you are done with the data it holds, otherwise your program will leak memory.

Theocharis K.
  • 1,281
  • 1
  • 16
  • 43
0

Yes, there is limit on how much you can allocate on stack at a given time. It is compiler dependent. On visual studio default size is 1 MB. Check your compiler settings to know the size. My guess is since you are exceeding this limit it is crashing.

Asha
  • 11,002
  • 6
  • 44
  • 66
0

I suppose I might as well give my answer, borrowed from here:

int (*a)[N] = malloc(N * N * sizeof(int));
int (*b)[N] = malloc(N * N * sizeof(int));

Now you can access them like the 2D arrays in your original code; e.g. a[50][100] = 37;

Do not forget to free(a) and free(b) when you are done with them.

Community
  • 1
  • 1
Nemo
  • 70,042
  • 10
  • 116
  • 153