在scanf中的分段故障 [英] Segmentation fault in scanf

查看:111
本文介绍了在scanf中的分段故障的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在运行此code,我遇到段错误在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;
}

一直以来,我需要那些阵列,可以有人建议我怎么correctify这个code。

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

推荐答案

的问题是,你是在本地定义一些巨大的对象。局部变量是在栈上创建和堆栈有限制(每个线程)。有时,堆最多可1兆字节的是。你的阵列将远远超出。我的猜测是,你实际上是溢出堆栈。你可以移动的数组定义的主之外,你的程序将正常运行这些阵列将不会在栈上创建。您也可以通过在使其静态定义阵列主。这与外界宣布他们具有相同的效果。

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.

全局变量(包括初始化数组)和静态未初始化的变量(即使他们是在一个函数)通常能获得放置在一个数据段和初始化时,你的程序是跑。它们也保证设置为全0。这维基引用的描述用C该数据区如:

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用C

在C,没有明确的初始化静态分配的对象初始化为零(算术类型)或一个空指针(指针类型)。的C实现通常重新present使用仅由零值位(虽然这不是由C标准要求)的位模式零值和空指针值。因此,BSS部分通常包括在文件范围内声明的所有未初始化的变量(即在任何函数之外)用static关键字声明未初始化的局部变量以及。实现也可以分配一个值完全的零值比特组成的BSS部分初始化静态分配变量。

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.

BSS 段不受限像堆栈。 BSS 是否存在的资源和你没有任何超出配额的过程中可以使用最多的可用内存。

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.

另一种方法是使用的malloc 这让她们在堆上动态分配的数组。下面code将创建数组最简单的方法。我用的#define 来更清楚什么是行和列。之后这些阵列被定义和存储器分配它们可用于像任何正常2D阵列

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;
}

这篇关于在scanf中的分段故障的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆