c - C中的指针和“非法指令(核心转储)”
问题描述
我想写一个语法分析器,但是当我运行代码时,它给了我这个:
Illegal instruction (core dumped)
现在,我运行调试器,它告诉我在第一次迭代时(所以它不能是上下文的),错误发生在这里:
static int list(int poz, int *size) {
...
if(*size==poz)
...
这个函数是这样调用的:
list(-1,&size);
这是执行操作的整个代码:
static int nexttoken() {
if(pointer==filesize)
return -1;
return cchar=file[++pointer];///file is an array where i keep the contents of the file (without spaces)
}
static void getint(int *atr) {
*atr=0;
while(isdigit(nexttoken()))
*atr=(*atr)*10+cchar-'0';
return;
}
///...
static int atom() {
int integer,size,currentpoz;
getint(&integer);
while(cchar=='(') {
currentpoz=pointer;
list(-1,&size);
integer%=size;
pointer=currentpoz;
integer=list(integer,&size);
nexttoken();
}
return integer;
}
static int list(int poz,int *size) {
*size=0;
int retval=0;
while(nexttoken()!=')') {
if(*size==poz)
retval=atom();
else
atom();
*size++;
}
return retval;
}
我在另一个编译器上运行了相同的代码,它告诉我这是段错误(SIGSIEV)。我不知道是什么导致了这个问题,也不知道指针是如何给我这些的。
提前致谢,
米海
解决方案
*size++;
这可能是您的罪魁祸首-您没有更新size
指向的值,而是更改size
为指向不同的对象。Postfix++
的优先级高于 unary *
,因此表达式被解析为*(size++)
.
重写为
(*size)++;
看看这是否不会使问题消失。
推荐阅读
- reactjs - 清除异步选择上的缓存选项
- r - R:绘制时间线流程图
- node.js - Steam OpenId 无效签名 InternalOpenIDError:
- alexa - 用于自定义技能和令牌刷新说明的 Alexa a/c 链接
- reactjs - 在 React-Router V4 中以编程方式设置路由参数最终会出现“找不到页面”
- angular - fontawesome 整合在
离子 4 - 角度 7 - google-chrome-extension - 如何在我的系统中本地设置 Metamask?
- python - 如何将unicode日语从一个文件写入另一个文件?
- java - 使用 Spring Data JPA 在 Mongodb 文档中进行 PrePersist 和 PreUpdate
- amazon-web-services - 当用户说出诸如“双九”之类的内容时,如何以数字形式输入用户?