#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.

Recommended Answers

All 2 Replies

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.

>#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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.