0
#include<stdio.h>
#define UN_IN -1
#define COL 10001
#define ROW 5000
int main()
{
    int r,c;
    long int a[ROW][COL];
    for (r=0;r<ROW;r++)
    for(c=0;c<COL;c++)
    {
    a[r][c]=UN_IN;
    // b[r][c]=UN_IN;
     }
     printf("%d %d", a[34][56],a[12][67]);
     getch();
     return 0;
     }

I was solving a problem which required large input. Am using a two dimension array but it gives me segmentation fault when I run it.
Please help.

3
Contributors
2
Replies
4
Views
6 Years
Discussion Span
Last Post by Narue
0

You are more than likely corrupting the stack with that huge array. Move the array into global, outside any function, such as move line 8 just after line 4.

0

>#define COL 10001
>#define ROW 5000
>long int a[ROW][COL];

Let's see, 10001 * 5000, multiply that by 4 on the assumption that your longs are 32-bit, and you've got a single object on the stack taking up ~190MB. I'm confident that the default stack size for your compiler is significantly smaller than that. For huge arrays like this you should be using malloc to allocate on the heap.

Edited by Narue: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.