【发布时间】:2012-11-21 01:37:56
【问题描述】:
我明白,在编程世界中,没有什么事情是突然发生的,但我真的被困在这里......在我的 while 循环结束时,条件内的指针突然从 NULL 变为“某事”
我有以下功能:
tListInstr *copyList (tListInstr *sourceList) {
tListInstr *newList;
listInit(&newList);
listFirst(sourceList);
while(sourceList->active != NULL){
InstructionGenerate(newList,
sourceList->active->Instruction.instType,
sourceList->active->Instruction.addr1,
sourceList->active->Instruction.addr2,
sourceList->active->Instruction.addr3);
listNext(sourceList);
if(sourceList->active == NULL)
printf("wtf????\n");
}
return newList;
}
解释代码,它是解释器的一部分,该函数复制源语言中被调用函数的3个地址代码指令列表并返回副本,sourceList是要复制的列表(它永远不会为NULL)newList很明显新名单。 listInit 分配内存并初始化新列表,listFirst 将sourceList 的活动设置为其第一项。 listNext 将活动转移到下一项,就在当前活动的后面。 InstructionGenerate 将新指令附加到 newList。
我的问题是,在循环结束时 sourceList->active 显然是 NULL,因为我在终端上得到了无限的 wtf????,但是在它被打印出来并且 while 的条件得到测试之后,它有非 NULL 值(我检查过)和 while 无限循环。
有趣的是,当我删除 InstructionGenerate 调用时,它运行正常。但是InstructionGenerate 不会/不应该以任何方式影响sourceList 指针,因为它正在与newList 一起使用,如果它以某种奇怪的方式使用sourceList,我将无法理解,我会更改活动在它被调用之后并在它被调用之前测试条件。
我认为这不会有任何帮助,但这里是 InstructionGenerate 的代码和它调用的函数:
void InstructionGenerate(tListInstr *l, int varType,void *addr1, void *addr2,void *addr3){
tInstr I;
I.addr1 = addr1;
I.addr2 = addr2;
I.addr3 = addr3;
I.instType = varType;
listInstInsert(l,I);
}
void listInstInsert(tListInstr *L,tInstr I)
{
tListItem ptr = malloc(sizeof(struct listItem));
if(ptr == NULL)
return;
else
{
ptr->Instruction = I;
if (L->first == NULL){
ptr->nextItem = NULL;
L->first = ptr;
L->end = ptr;
}
else{
ptr->nextItem = NULL;
L->end->nextItem = ptr;
L->end = ptr;
}
}
}
最后是struct的
typedef struct tInstr
{
int instType;
void *addr1;
void *addr2;
void *addr3;
} tInstr;
typedef struct listItem
{
tInstr Instruction;
struct listItem *nextItem;
} *tListItem;
typedef struct tListInstr
{
struct listItem *first;
struct listItem *end;
struct listItem *active;
} tListInstr;
编译:
gcc 版本 4.4.5 (Debian 4.4.5-8)
gcc 版本 4.5.4 (GCC)
【问题讨论】:
-
编译所有警告和调试信息,例如使用
gcc -Wall -g并学习使用gdb调试器(以及valgrind)。 -
使用
gcc -Wall -Wextra -pedantic -ggdb -std=c99编译,valgrind没有任何警告,即使使用gdb我也无法找出发生了什么 -
使用
gdb的watch和step命令 -
能否也显示调用
copyList()函数的代码? -
我会验证 newList 实际上是由 listInit() 设置的。如果 newList 是一个 NULL 指针,那么这会导致各种有趣的行为。您也可以通过预处理器运行代码,以确保您不会成为某种宏替换错误的受害者。但这似乎不太可能。
标签: c pointers while-loop conditional-statements