Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/56.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 数字打印程序中的分段错误_C_Segmentation Fault - Fatal编程技术网

C 数字打印程序中的分段错误

C 数字打印程序中的分段错误,c,segmentation-fault,C,Segmentation Fault,当我运行此程序时,会出现分段错误 #include <stdio.h> #include <stdlib.h> #include <string.h> static char* exe; void usage(void) { printf("Usage: %s <number of integers>\n", exe); } int main(int argc, char** argv) { //This program re

当我运行此程序时,会出现分段错误

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static char* exe;

void usage(void) {
    printf("Usage: %s <number of integers>\n", exe);
}

int main(int argc, char** argv) {
    //This program reads in n integers and outputs them/
    //in reverse order. However, for some odd reason, I/
    //am getting an error when I run it with no command/
    //line arguments. It is supposed to display helpful/
    //usage information out, but instead it segfaults??/
    exe = malloc(50 * sizeof(*exe));
    strncpy(exe, argv[0], 49);

    if(argc != 2) {
        usage();
        exit(0);
    }

    int n = atoi(argv[1]);
    int* numbers = malloc(n * sizeof(*numbers));

    int i;
    for(i = 0; i < n; i++) {
        scanf("%d\n", &numbers[i]);
    }

    for(i = 9; i >= 0; i--) {
        printf("%d:\t%d\n", 10 - i, numbers[i]);
    }

    free(numbers);
    free(exe);
    return 0;
}
#包括
#包括
#包括
静态字符*exe;
无效用法(void){
printf(“用法:%s\n”,exe);
}
int main(int argc,字符**argv){
//这个程序读入n个整数并输出它们/
//然而,由于一些奇怪的原因,我/
//我在没有命令的情况下运行时出错/
//行参数。它应该显示有用的/
//将使用信息输出,但它会自动识别故障/
exe=malloc(50*sizeof(*exe));
strncpy(exe,argv[0],49);
如果(argc!=2){
用法();
出口(0);
}
int n=atoi(argv[1]);
int*numbers=malloc(n*sizeof(*编号));
int i;
对于(i=0;i=0;i--){
printf(“%d:\t%d\n”,10-i,数字[i]);
}
免费(数字);
免费(exe);
返回0;
}

变量
argv[0]
包含指向正在运行的程序名称的指针。您的程序正在尝试读取从该指针开始的49个字符或NULL,以先到者为准。在您的情况下,您可能正在移动到一个您无权访问的新页面

您需要确保
exe
字符串在
strncpy
之后具有空终止符

尝试在
strncpy
之后添加此行:

    exe[49] = '\0';

这是因为
??/
是一个变为
\
的三角图,导致
exe=malloc…
行变为注释的一部分。因此,
exe
仍然为空,并且在取消引用它时崩溃。

调试器在哪里说segfault正在发生?您为该程序提供了什么输入?带有
strncpy
的线路会导致SEGCFAULT。我像
/numbers 10
可能重复的No的可能重复一样运行它,这是
argv[0]
,而不是
argv[1]
。如果src的长度小于n,strncpy()会向dest写入额外的空字节,以确保总共写入n个字节。
strncpy
添加空终止符。。。另外,
strncpy
函数本身会导致segfault。strncpy仅在第一个
n
(本例中为49个)字符中有一个空终止符时才会添加空终止符。否则就不会了.+1嗯,哇。我不知道这是一个合法的问题,还是我们只是被欺骗了。我的钱在OP上,只是读了另一个特别提到的trigraph问题。真不错。完全错过了三角图。哈哈,很好,我想知道有多少人知道三角图。在我问这个问题不到10分钟的时候,有人发布了正确的答案。是的,我确实事先知道三角图(请看完全对齐的注释:P),但了解三角图并小心它们绝对是件好事。祝贺你,先生,你得到了我的支持并接受了。如果你没有包括整个程序,而只包括前面的两行代码和有趣的评论,那么这个问题可能会更令人困惑。我剪切并粘贴了整个程序,默认情况下GCC会警告三角图。