调试了几个小时,段错误。找不到问题
Debugging for hours, segmentation fault. Can't find the issue
有一些代码似乎可以工作,但突然之间,每次代码 运行。
时,我就开始遇到分段错误
希望新人能帮我找到问题所在。
它 运行 这行 (printf("There are %d arguments excluding (%s)\n", count-1, *(input));
) 并在之后崩溃。
我试过使用 gdb 并检查我的代码,但我似乎找不到问题所在。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int getDiff(char **list[], int n);
int getSum(char *list[], int n);
int main( int count, char *input[] )
{
int total;
printf("There are %d arguments excluding (%s)\n", count-1, *(input));
if(strcmp(*(input+1),"sum") == 0){
int i;
for(i = 2; i<=count;){
printf("%d ", atoi(*(input + 2)));
i++;
if(i < count){
printf("+ ");
}
}
total = getSum(input, count);
}
if(strcmp(*(input+1),"diff") == 0){
total = getDiff(&input, count);
}
printf(" === %d ====", total);
}
int getDiff(char **list[], int n){
int i;
int total = atoi(**(list + 2));
for (i=3; i<= n;) {
int convert;
convert = atoi(**(list + i));
total = total - convert;
i++;
}
return total;
}
int getSum(char *list[], int n){
int i;
int total = atoi(*(list + 2));
for (i=3; i<= n;) {
int convert;
convert = atoi(*(list + i));
total = total + convert;
i++;
}
return total;
}
运行和return应该是从数字转换而来的整数之和。
这是 gdb 告诉我的
程序接收到信号 SIGSEGV,分段错误。
__strcmp_sse42 中的 0x00007ffff7b4c196 () 来自 /lib64/libc.so.6
getSum
中的索引需要一些修正。输入 list
是 [0-program, 1-"sum", 2-“3”, and 3-“4”]。 n=4。然而,总和看起来从 3 到 4(包括 4)。列表中没有触发 SEGV 的元素 #4。
考虑将循环限制为 i<n
而不是 i<=n
。
风格方面,将 i++ 移动到 'for' 语句,声明变量并将它们设置在同一行,并考虑使用索引,而不是指针样式引用(list[i],而不是 *( list+i). 它会让代码更容易阅读,希望能有更好的成绩!
int getSum(char *list[], int n){
int total = atoi(*(list + 2));
for (int i=3; i< n; i++) {
int convert = atoi(*(list + i));
total = total + convert;
}
return total;
}
最后,考虑修复变量的打印输出printf("%d ", atoi(*(input + 2)));
。它复制了第二个参数,这不是你想要的。
有一些代码似乎可以工作,但突然之间,每次代码 运行。
时,我就开始遇到分段错误希望新人能帮我找到问题所在。
它 运行 这行 (printf("There are %d arguments excluding (%s)\n", count-1, *(input));
) 并在之后崩溃。
我试过使用 gdb 并检查我的代码,但我似乎找不到问题所在。
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int getDiff(char **list[], int n);
int getSum(char *list[], int n);
int main( int count, char *input[] )
{
int total;
printf("There are %d arguments excluding (%s)\n", count-1, *(input));
if(strcmp(*(input+1),"sum") == 0){
int i;
for(i = 2; i<=count;){
printf("%d ", atoi(*(input + 2)));
i++;
if(i < count){
printf("+ ");
}
}
total = getSum(input, count);
}
if(strcmp(*(input+1),"diff") == 0){
total = getDiff(&input, count);
}
printf(" === %d ====", total);
}
int getDiff(char **list[], int n){
int i;
int total = atoi(**(list + 2));
for (i=3; i<= n;) {
int convert;
convert = atoi(**(list + i));
total = total - convert;
i++;
}
return total;
}
int getSum(char *list[], int n){
int i;
int total = atoi(*(list + 2));
for (i=3; i<= n;) {
int convert;
convert = atoi(*(list + i));
total = total + convert;
i++;
}
return total;
}
运行和return应该是从数字转换而来的整数之和。
这是 gdb 告诉我的
程序接收到信号 SIGSEGV,分段错误。 __strcmp_sse42 中的 0x00007ffff7b4c196 () 来自 /lib64/libc.so.6
getSum
中的索引需要一些修正。输入 list
是 [0-program, 1-"sum", 2-“3”, and 3-“4”]。 n=4。然而,总和看起来从 3 到 4(包括 4)。列表中没有触发 SEGV 的元素 #4。
考虑将循环限制为 i<n
而不是 i<=n
。
风格方面,将 i++ 移动到 'for' 语句,声明变量并将它们设置在同一行,并考虑使用索引,而不是指针样式引用(list[i],而不是 *( list+i). 它会让代码更容易阅读,希望能有更好的成绩!
int getSum(char *list[], int n){
int total = atoi(*(list + 2));
for (int i=3; i< n; i++) {
int convert = atoi(*(list + i));
total = total + convert;
}
return total;
}
最后,考虑修复变量的打印输出printf("%d ", atoi(*(input + 2)));
。它复制了第二个参数,这不是你想要的。