- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我已经为 Case Register 编写了 C 程序。我的程序运行良好。我使用 -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;
}
最佳答案
pd.array 只能容纳 1024 个结果。在写入之前,您必须检查 no_prod 是否 < 1024,否则您将写入未分配的内存,这就是导致段错误的原因。一旦 no_prod 达到 1024,您必须中止该程序(我假设您还没有使用动态分配)。
不过,最好不要在检查时实际写入 1024,而是使用数组大小的宏(如果您也没有使用过宏,请不要担心)。
关于c - 如何在c程序中避免 "Segmentation Fault",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14473977/
这是一个类作业,用于将有序插入到已排序的链表中。我已经避免访问空指针,并且添加了一些调试输出行以将段错误缩小到 while 循环本身的条件语句。 我可以将一个节点添加到空列表并将具有较小键的节点添加到
一个小型测试程序在 64 位 Linux 上使用 gfortran (4.4.5) 返回段错误。 n=2_8**22_8 时不存在故障。 gdb 指示在循环的第一次迭代期间函数 mylen 中发生段错
我正在使用 Spring-WS,当我通过 webServiceTemplate 中的 marshalSendAndReceive 调用 Web 服务时,我收到错误代码为 500 的 SOAP 错误。不
我有一个具有此方法的单例数据管理器: -(NSArray*)fetchItems { NSEntityDescription *entity = [NSEntityDescription
我有一个调用 Web 服务的 Java 应用程序 (A)。 1)然后我有另一个使用 A jar 的应用程序 (B)。调用成功,返回需要的数据。 2)然后我有一个也使用A jar 的网络应用程序。在这种
我正在 Java 应用程序中对第三方网络服务进行 SOAP 网络服务调用。用于查找 Web 服务、传输数据和发出 SOAP 请求的 Java 类是使用来自 WSDL 的 Apache CXF 库生成的
我正在尝试学习 SOAP 协议(protocol)。因此,在使用以下命令借助 wsimport 生成 net.webservicex 代码后 C:\Program Files\Java\jdk1.8.
我正在构建一个基于配备 arm64 CPU 的 UltraScale+ FPGA 的数据采集系统。数据通过 DMA 传输到 RAM。驱动程序中的 DMA 缓冲区保留如下: virt_buf[i] =
我使用 QtDesigner 创建了两个对话框“listdialog.ui”和“editdialog.ui”,已发布使用Qt5.3.1,然后添加到项目“phone book.pro”“带有源代码”使用
有谁知道空中交通管制系统是如何实现软件容错的? 一些 URL 会很有帮助。 最佳答案 Lockheed 不久前发表了一些关于此的文章。看看here . 许多系统都使用 Ada,它明确支持验证命题和其他
我认为阅读内存应该不会引起任何问题,例如 char *d=""; char *d2="test"; memcmp(d,d2,10); memcmp() 永远 会失败吗? 最佳答案 您的假设是不正确的,
这个程序在我的 UNIX 机器上导致了段错误。我将原因缩小到 memset() 的第二次调用。 为什么会出现这种行为?第一个“ block ”代码与第二个几乎相同,不是吗?为什么 第一次 调用 mem
我很困惑进程在使用虚拟内存时如何可能出现段错误。据我了解,“虚拟”内存允许进程访问所有可用内存,然后将其映射到“实际”硬件内存。通过这种转换,进程怎么可能尝试访问不允许访问的内存部分? 最佳答案 听起
Tritwise操作(向右旋转和疯狂操作)无法正确运行,并在Malbolge编译器/解释器中引发分段错误。 在看到有关Coding Challenges和Code Golf的惊人答案之后,我决定开始在
这是我的代码 #include #include #include /************************************************** a is a poin
我有下面的代码。当我取消注释 temperature(i,j) = anode_temperature 时,我会出现 SegFault。 forall(i=0:Cells(1), j=0:Cells(
我是 C 编程新手,我的代码中出现了段错误。该程序使用返回函数来询问用户他们的银行帐户中有多少钱。稍后我将添加代码来计算利息。感谢您为我查看此内容,因为我很难找出为什么会出现此错误。 #include
为了测试,我编写了一个代码来计算 #include int main(void) { int p, i, primes[50], index; boo
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
考虑 const a = [1,2,3] console.log(a[5]) // returns undefined 我是一名 C 和 Fortran 程序员,我预计会发生段错误。这里的内存是如何管
我是一名优秀的程序员,十分优秀!