Hi guys,
I was running a simple code on Fedora 11. it has this error " segmentation fault". The code is too simple to have any mistakes........ Can anyone help me with this? What is this "segmentation fault".

Thank you very much.

#include "stdio.h"  
int main()
{
        int T[1001][4001]; 
        int i=0;
    int j=0;     
        for(j=0;j<1001;j++)
        { 
                    for(i=0;i<4001;i++)
                { 
                                T[j][i]=0; 
                        }
            }  

    printf("Thank you!\n");       

    return 0;
}

I saved this file as mainapp.cpp.

command line:

g++ mainapp.cpp -o test
./test

then it says segmentation fault........

Edited 3 Years Ago by Nick Evan: Fixed formatting

Try defining you array like below:

#include "stdio.h"

int T[1001][4001];

int main()
{
int i=0;
int j=0;
for(j=0;j<1001;j++)
{
for(i=0;i<4001;i++)
{
T[j][i]=0;
}
}

printf("Thank you!\n");

return 0;
}

Also a seg fault is generally triggered when a user application tries to access an invalid address.

Edited 7 Years Ago by gerard4143: n/a

Your array maybe too big. Try decreasing the size of the array and see if the seg fault goes away...G

You are right, if I tried a smaller one like T[101][4001], it is ok.....

But the problem is, int T[1001][4001] is not big at all. And I need to define such arrays in my computation.

Do you know what the problem is?

You are right, if I tried a smaller one like T[101][4001], it is ok.....

But the problem is, int T[1001][4001] is not big at all. And I need to define such arrays in my computation.

Do you know what the problem is?

Yes look at my second solution

int T[1001][4001];
needs a huge memory that is equeal to

1001 * 4001 * 4 [sizeof(int) is 4 in linux]
that equeals to

4,000,000 * 4 byte approx
= 16,000,000 byte
= 16,000 KB (approx)
= 16 MB (approx)

due to this size only your system is giving a segmentation fault.

try your program with simply one line

int main()
{
int T[1001][4001];
return 0;
}

it should give a seg fault


if you are doing computation with numbers which takes atmost two byte u can use the "short" data type instead. It should then work fine.

thanks
hope thats of some help

This article has been dead for over six months. Start a new discussion instead.