0 c pointers segmentation-fault
我想编写一个语法分析器,但是当我运行代码时,它给了我这个:
Illegal instruction (core dumped)
Run Code Online (Sandbox Code Playgroud)
现在,我运行了调试器,它告诉我在第一次迭代时(因此它不能与上下文相关),错误发生在这里:
static int list(int poz, int *size) {
...
if(*size==poz)
...
Run Code Online (Sandbox Code Playgroud)
这个函数的调用方式如下:
list(-1,&size);
Run Code Online (Sandbox Code Playgroud)
这是执行操作的完整代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
我在另一个编译器上运行相同的代码,它告诉我这是段错误(SIGSIEV)。我不知道是什么原因导致了这个问题,也不知道指针是如何给我这些信息的。
提前致谢,
米哈伊
*size++;
Run Code Online (Sandbox Code Playgroud)
这可能是罪魁祸首 - 您没有更新size指向的值,而是更改size为指向不同的对象。Postfix++的优先级高于 unary *,因此该表达式被解析为*(size++).
将其重写为
(*size)++;
Run Code Online (Sandbox Code Playgroud)
看看这是否能让问题消失。
| 归档时间: |
|
| 查看次数: |
1452 次 |
| 最近记录: |