如何避免;分段故障“;在c程序中
我已经为Case寄存器编写了C程序。我的程序运行良好。我使用如何避免;分段故障“;在c程序中,c,linux,segmentation-fault,C,Linux,Segmentation Fault,我已经为Case寄存器编写了C程序。我的程序运行良好。我使用-Wall进行编译。它没有显示任何警告。但是问题是,如果我想用bash循环运行它,例如.test.sh |./caseRegister,输出如下: :>>:1498.00 :>>:1499.00 :>>:1500.00 :>>:1501.00 :>>:1502.00 :>>:1503.00 :>>:1504.00 :>>:1505.00 :&
-Wall
进行编译。它没有显示任何警告。但是问题是
,如果我想用bash循环运行它
,例如.test.sh |./caseRegister
,输出如下:
:>>:1498.00
:>>:1499.00
:>>:1500.00
:>>:1501.00
:>>:1502.00
:>>:1503.00
:>>:1504.00
:>>:1505.00
:>>:1506.00
:>>:1507.00
:>>:1508.00
:>>:1509.00
:>>:1510.00
:>>:1511.00
:>>:1512.00
:>>:1513.00
:>>:1514.00
:>>:1515.00
:>>:1516.00
:>>:1517.00
:>>:1518.00
Segmentation fault
该test.sh是:
#/bin/sh
i=1
run=1
for ((; ; ))
do
echo $i
#echo $((i++))
done
为什么我的代码长期显示“分段错误”?请任何人向我解释原因和预防技术,以纠正它。先谢谢你
我的代码是:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
struct prod_details
{
int no_prod;
double total;
double array[1024];
}pd;
char *getinput(char *inp)
{
printf(":");
gets(inp);
if(strlen(inp) > 11)
{
printf("Input is restricted to 10 character\n");
getinput(inp);
}
return inp;
}
void print()
{
printf("Grant Total is : %.2f\n",pd.total);
}
int check(char *str)
{
int i,minusflag=0,plusflag=0,optrflag=0;
if(strlen(str) == 0)
return 0;
if(str[strlen(str) -1] == '+' || str[strlen(str) -1] == '-' || str[strlen(str) -1] == '*')
{
printf("last operator\n");
return 1;
}
if(str[0] == '-')
{
i=1;
while(str[i] != '-' )
{
if(str[i] == '\0')
{
minusflag=1;
break;
}
i++;
}
if(str[i] == '-')
{
minusflag=0;
return 1;
}
}
if(str[0] == '-')
{
i=1;
while(str[i] != '+' )
{
if(str[i] == '\0')
{
minusflag=1;
break;
}
i++;
}
if(str[i] == '+')
{
minusflag=0;
return 1;
}
}
if(str[0] == '-' && minusflag == 1)
return 2;
if(str[0] == '+')
{
i=1;
while(str[i] != '+')
{
if(str[i] == '\0')
{
plusflag=1;
break;
}
i++;
}
if(str[i] == '+')
{
plusflag=0;
return 1;
}
}
if(str[0] == '+')
{
i=1;
while(str[i] != '-' )
{
if(str[i] == '\0')
{
plusflag=1;
break;
}
i++;
}
if(str[i] == '-')
{
plusflag=0;
return 1;
}
}
if(str[0] == '+' && plusflag == 1)
return 2;
if(str[0] == '*')
return 1;
if((str[0] == '+' || str[0] == '-') && (str[1] == '+' || str[1] == '-' || str[1] == '*' || str[1] == '/' ))
return 1;
for(i=0;i<strlen(str);i++)
{
if(((str[i] >= '!' && str[i] <= '/') || (str[i] >= ':' && str[i] <= '~')))
{
if(str[i] == '*' || str[i] == '+' || str[i] == '-' || str[i] == '/')
{
optrflag++;
}
else
return 1;
}
}
if(optrflag == 1)
return 3;
else if(optrflag > 1)
return 1;
return 2;
}
int expcalc(char *str)
{
char copy[10];
char op;
char *temp;
char numb[10],numf[10];
printf("Start of expcal\n");
int i;
double result=0;
for(i=0;i<strlen(str);i++)
{
if(str[i] == '+' || str[i] == '-' || str[i] == '*' || str[i] == '/')
{
op = str[i];
}
}
strcpy(copy,str);
i=0;
while(str[i] != op)
{
numf[i] = str[i];
i++;
}
numf[i] ='\0';
temp=strchr(copy,op);
i=1;
printf("\n");
while(temp[i] != '\0')
{
numb[i-1] = temp[i];
i++;
}
numb[i-1] = '\0';
switch(op)
{
case '+':
result=atof(numf)+atof(numb);
break;
case '-':
result=atof(numf)-atof(numb);
break;
case '*':
result=atof(numf)*atof(numb);
break;
case '/':
result=atof(numf)/atof(numb);
break;
default:
break;
}
printf("%.2f\n",result);
if((pd.total+result) < 0)
{
printf("Couldn't calculate\n");
return 0;
}
pd.array[pd.no_prod]=result;
pd.total=pd.total+result;
printf(">>:%.2f\n",pd.total);
pd.no_prod++;
return 0;
}
int calc(char *str)
{
if((pd.total+atof(str)) < 0)
{
printf("Coundn't Calculate\n");
return 0;
}
pd.array[pd.no_prod]=atof(str);
pd.total=pd.total+pd.array[pd.no_prod];
printf(">>:%.2f\n",pd.total);
pd.no_prod++;
return 0;
}
int call()
{
int chkflg;
char input[1024];
getinput(input);
chkflg=check(input);
if(chkflg == 3)
{
expcalc(input);
call();
}
else if(chkflg == 2)
{
calc(input);
call();
}
else if(chkflg == 1)
{
printf("You have entered Wrogly!!!\n Please enter correctly\n");
call();
}
else
{
print();
return 1;
}
return 0;
}
int main()
{
printf("..CASE RIGISTER..\n");
call();
return 0;
}
#包括
#包括
#包括
#包括
结构产品详细信息
{
int no_产品;
双倍总数;
双数组[1024];
}pd;
char*getinput(char*inp)
{
printf(“:”);
获取(inp);
如果(strlen(inp)>11)
{
printf(“输入限制为10个字符\n”);
获取输入(inp);
}
返回inp;
}
作废打印()
{
printf(“赠款总额为%.2f\n”,pd.总额);
}
整数检查(字符*str)
{
int i,minusflag=0,plusflag=0,optrflag=0;
如果(strlen(str)==0)
返回0;
如果(str[strlen(str)-1]='+'| | str[strlen(str)-1]='-'.| | str[strlen(str)-1]='*')
{
printf(“最后一个运算符”);
返回1;
}
如果(str[0]='-')
{
i=1;
while(str[i]!='-')
{
如果(str[i]='\0')
{
minusflag=1;
打破
}
i++;
}
如果(str[i]='-')
{
minusflag=0;
返回1;
}
}
如果(str[0]='-')
{
i=1;
while(str[i]!='+')
{
如果(str[i]='\0')
{
minusflag=1;
打破
}
i++;
}
如果(str[i]=='+')
{
minusflag=0;
返回1;
}
}
如果(str[0]='-'&&minusflag==1)
返回2;
如果(str[0]=='+')
{
i=1;
while(str[i]!='+')
{
如果(str[i]='\0')
{
plusflag=1;
打破
}
i++;
}
如果(str[i]=='+')
{
plusflag=0;
返回1;
}
}
如果(str[0]=='+')
{
i=1;
while(str[i]!='-')
{
如果(str[i]='\0')
{
plusflag=1;
打破
}
i++;
}
如果(str[i]='-')
{
plusflag=0;
返回1;
}
}
如果(str[0]='+'&&plusflag==1)
返回2;
如果(str[0]='*')
返回1;
如果((str[0]='+'| | | str[0]='-')&&(str[1]='+'| | | str[1]='-''.'*'.| | str[1]='='/'))
返回1;
对于(i=0;i='!'&&str[i]=':'&&str[i]1)
返回1;
返回2;
}
int expcalc(字符*str)
{
字符拷贝[10];
char op;
字符*温度;
char numb[10],numf[10];
printf(“expcal的开始\n”);
int i;
双结果=0;
for(i=0;ipd.array只能容纳1024个结果。在写入之前,您必须检查没有\u prod小于1024,否则您将在未分配的内存中写入,这会导致分段错误。一旦没有\u prod达到1024,您就必须中止程序(我假设您还没有使用动态分配)
不过,最好不要在检查时实际写入1024个字符,而是对数组大小使用宏(如果您也没有使用宏,则不要担心这一点).您的代码中存在问题,很可能您使用的指针没有指向您期望的内容。请使用调试器找出原因和位置。我认为我们需要查看C代码才能回答此问题。分段错误意味着您的程序访问或执行了无效内存。您有一个错误。查看一些代码可能会有所帮助人们分析它,但首先你应该在它上运行一个调试器,看看你是否能自己找到它。使用bashulimit
设置限制以获得一些core
转储。使用gcc-Wall-g
编译程序;使用gdb
和valgrind
来调试它。分段冲突显示程序“s buggy!”BasileStryneVitch如何使用bash循环运行gdb?