问题描述
我承认直-C新手,但这让我难住了。我工作的实践链表实现,而且我通过简单地增加一个变量的函数split_node得到一个段错误:
I'm admittedly a straight-C newbie, but this has got me stumped. I'm working on a linked list implementation for practice, and I'm getting a segfault by simply adding a variable to the split_node function:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Node {
struct Node *child;
char *content;
};
void print_list(struct Node node);
void split_node(struct Node *node, int position);
int main() {
struct Node head, second, third;
head.content = "first";
second.content = "second";
third.content = "i'm third";
head.child = &second;
second.child = &third;
print_list(head);
split_node(&head, 3);
print_list(head);
return 0;
}
void print_list(struct Node node) {
printf("%s\n", node.content);
if(node.child) print_list(*node.child);
}
/*
Split node into two nodes, with the first position characters of the node's content remaining with node, and the remainder being copied to the new node. (It doesn't yet truncate the first node's string, but does do the copy.)
*/
void split_node(struct Node *node, int position) {
if(position >= strlen((*node).content)) return;
struct Node newNode;
newNode.child = (*node).child;
(*node).child = &newNode;
int length = (strlen((*node).content) - position);
newNode.content = malloc(sizeof(char) * (length + 1));
strncpy(newNode.content, (*node).content + sizeof(char) * position, length);
newNode.content[length] = '\0';
//int foo;
}
这code编译(GCC -Wall -o列表list.c)和运行的罚款:
This code compiles (gcc -Wall -o list list.c) and runs fine:
$ ./list
first
second
i'm third
first
st
second
i'm third
但是,如果我取消注释 INT富
在 split_node
的结束,编译并运行,我得到:
But if I uncomment int foo
at the end of split_node
, compile and run, I get:
$ ./list
first
second
i'm third
first
st
Segmentation fault
GDB给我这个回溯:
gdb gives me this backtrace:
#0 0x91d6ae70 in strlen ()
#1 0x91dd3126 in puts ()
#2 0x00001f21 in print_list (node={child = 0xbcec815b, content = 0x8b000000 <Address 0x8b000000 out of bounds>}) at list.c:41
#3 0x00001f3c in print_list (node={child = 0x8fe0154b, content = 0x1ff6 "i'm third"}) at list.c:42
#4 0x00001f3c in print_list (node={child = 0xbffff568, content = 0x1fef "second"}) at list.c:42
#5 0x00001f3c in print_list (node={child = 0xbffff570, content = 0x1fe9 "first"}) at list.c:42
#6 0x00001ee0 in main () at list.c:33
为什么要加入一个变量定义会导致段错误?这似乎是粉碎新创建的节点的内容的指针。我很困惑;任何帮助吗?
Why would adding a variable definition cause a segfault? It appears to be smashing the content pointer of the newly created node. I'm confused; any help?
推荐答案
您需要动态地分配你的节点(使用malloc)。
You need to dynamically allocate your nodes (using malloc).
当你拥有了它,你的新节点声明的堆栈。当拆分函数返回时,新的节点不再有效的内存。
As you have it, your new node is declared on the stack. When the split function returns, that new node is no longer valid memory.
添加一个变量会导致段错误,因为该变量改变了堆栈造成不同的效果,布局时,函数返回。
Adding a variable causes a segfault because that variable changes the layout of the stack causing slightly different behavior when the function returns.
这篇关于从加入一个变量段错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!