Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
942 views
in Technique[技术] by (71.8m points)

c - Segmentation fault in scanf

While running this code, I encounter Segmentation fault at scanf(). This is possibly due to the declaration of large arrays(I checked it by commenting the arrays declaration).

#include<stdio.h>
int main()
{
    int test;
    //int n,ok,counter,i,j;
    char a[1000][1000];
    int x[1000][1000],y[1000][1000];
    scanf("%d",&test);
    printf("%d",test);
    return 0;
}

Since, I need those arrays, can someone suggest me how correctify this code.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The problem is that you are defining some huge objects locally. Local variables are created on the stack and the stack has limits (per thread). Sometimes the stack can be a maximum of 1 megabyte. Your arrays would be well beyond that. My guess is you are actually overflowing the stack. You could move the array definitions outside of main and your program should work as those arrays wouldn't be created on the stack. You could also define your arrays by making them static in main. This has the same effect as declaring them outside.

Globally defined variables (including uninitialized arrays) and static uninitialized variables (even if they are in a function) generally get placed in a data segment and initialized when your program is run. They are also guaranteed to be set to all 0. This Wiki reference describes this data area in C as:

BSS in C

In C, statically-allocated objects without an explicit initializer are initialized to zero (for arithmetic types) or a null pointer (for pointer types). Implementations of C typically represent zero values and null pointer values using a bit pattern consisting solely of zero-valued bits (though this is not required by the C standard). Hence, the BSS section typically includes all uninitialized variables declared at file scope (i.e., outside of any function) as well as uninitialized local variables declared with the static keyword. An implementation may also assign statically-allocated variables initialized with a value consisting solely of zero-valued bits to the bss section.

The BSS segment is not constrained like the stack is. BSS could use up to available memory if the resources exist and you don't exceed any process quotas.

Another alternative is to dynamically allocate the arrays using malloc which would put them on the heap. The following code would be the easiest way to create your arrays. I used #define to make it clearer what is a row and column. After these arrays are defined and memory allocated they can be used like any normal 2D array.

#include<stdio.h>
#include<stdlib.h>
int main()
{
    #define ROWS 1000
    #define COLUMNS 1000
    int test;

    char (*a)[COLUMNS] = malloc(ROWS * sizeof *a);
    int  (*x)[COLUMNS] = malloc(ROWS * sizeof *x);
    int  (*y)[COLUMNS] = malloc(ROWS * sizeof *y);

    a[100][20] = 'X';
    x[4][999] = 666;
    y[500][0] =  42;

    scanf("%d",&test);
    printf("%d",test);

    free(a);
    free(x);
    free(y);

    return 0;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...