我正在用C编写一个列表示例,其中新节点被推送到堆栈的末尾。当我尝试将新节点推到最后时,我总是得到一个Bus Error: 10。下面是我的推送函数:
void push(struct node *tail, struct node *newNode) {
tail->next = newNode; // gdb says the problem is here
tail = tail->next;
}我使用push(tail, newNode);调用它
如果有必要,下面是我的结构:
struct node
{
int hour;
int minute;
char *name;
struct node *next;
};下面是显示通向push()的代码的主函数
int main()
{
char inputString[50];
int timeHour, timeMin;
struct node *head;
struct node *tail;
while ((scanf("%d:%d", &timeHour, &timeMin)) != EOF) {
scanf("%s", inputString);
if (strcmp(inputString, "enqueue") == 0) {
if (head == NULL) {
head = malloc(sizeof(struct node));
head->hour = timeHour;
head->minute = timeMin;
// get name
scanf("%s", inputString);
head->name = malloc(strlen(inputString)+1);
strcpy(head->name, inputString);
tail = head;
printEnqueue(head);
} else {
struct node *newEntry = malloc(sizeof(struct node));
newEntry->hour = timeHour;
newEntry->minute = timeMin;
// get name
scanf("%s", inputString);
newEntry->name = malloc(strlen(inputString)+1);
strcpy(newEntry->name, inputString);
push(tail, newEntry);
printEnqueue(newEntry);
}
} else {
pop(&head, timeHour, timeMin);
}
}
return 0;
}发布于 2013-04-16 13:57:24
我怀疑main函数中的head和tail节点没有正确初始化。
从您的代码中可以看出,如果head为NULL,那么它将被分配一个新节点。然而,您对head的定义并不能确保它最初是NULL (tail也是如此)。因此,您可以绕过if (head == NULL)分支(请确保它们确实是从gdb执行的:)。
Bus error很少见。所以我用谷歌搜索了一下,从here上看,在以下情况下可能会出现总线错误
使用地址不满足其对齐要求的处理器指令的
。
这可能是因为tail没有对齐,代码直接运行到else分支。因此push(tail, newEntry);将访问未对齐的尾部(这也验证了我的可疑对象)。
发布于 2013-04-16 12:17:03
修改#3:while ((scanf("%d:%d", &timeHour, &timeMin)) != EOF)在这个循环的主体中,不能保证两个整数timeHour和timeMin被赋值。也许你指的是while ((scanf("%d:%d", &timeHour, &timeMin)) == 2)。
修改#2:当你将一个值传递给一个函数时,你传递的是值,而不是变量。您在push中对tail的赋值对调用者(您的main)是不可见的。您需要传递一个指向该变量的指针(例如,&head,这是一个struct node **),并像以前一样分配给*tail。或者,您可以从push执行return newNode;,并使用返回值作为新的head。
修正:这看起来甚至不像是可以编译的。让我们来看看push。
void push(struct node **tail, struct node *newNode) {
(*tail)->next = *newNode; // gdb says the problem is here
*tail = (*tail)->next;
}*newNode的类型是什么?struct node。(*tail)->next的类型是什么?它在下面的代码片段中:
struct node
{
int hour;
int minute;
char *name;
struct node *next;
};修复你的不一致,并确保你的最小的,可编译的测试用例在你发布它之前是可编译的。
别忘了检查scanf的返回值!在您的例子中,除非发生错误,否则它应该返回1。
head->name = malloc(strlen(inputString));
strcpy(head->name, inputString);这是错误的,因为您没有分配足够的空间来存储'\0'字符。我想你指的是malloc(strlen(inputString) + 1)。在您的代码中有两个此错误的实例。我不打算重复我的话。
struct node *newEntry = malloc(sizeof(struct node));
push(&tail, newEntry);newEntry的类型是什么?struct node *。
void push(struct node **tail, struct node **newNode)newNode的类型是什么?struct node **。你看到不一致的地方了吗?您需要传入一个struct node **,但newEntry是一个struct node *。
发布于 2013-04-16 14:07:45
变化
void push(struct node *tail, struct node *newNode)
{
tail->next = newNode; // gdb says the problem is here
tail = tail->next;
}至
void push(struct node **tail, struct node *newNode)
{
(*tail)->next = newNode; // gdb says the problem is here
(*tail) = (*tail)->next;
}那就这样叫它吧
push(&tail, newEntry);正如你目前所拥有的,'tail‘永远不会改变,因为你不会把变量的地址传递给函数,所以你不能改变它所指向的东西。
还要确保你初始化了所有的局部变量(header,tail,...),让它成为一个习惯。
https://stackoverflow.com/questions/16028533
复制相似问题