[英]Segmentation fault with strncpy
EDIT: Variable names编辑:变量名
I'm making a linked list, and when I try to free a node it gives me an error.我正在制作一个链表,当我尝试释放一个节点时,它给了我一个错误。 I tracked my code and found that my error is rooted when I create a node with this code.
我跟踪了我的代码,发现当我用这段代码创建一个节点时,我的错误是根深蒂固的。
The weird part is that if I assign one char less than what I want it to be it works.奇怪的是,如果我分配的字符比我想要的少一个字符,它就会起作用。 Also, it works fine for assigning "word", the issue lays in "id".
此外,它适用于分配“单词”,问题在于“id”。
struct node* makeNewNode (char* word, char* id, int occurrences) {
//make space for the new node
struct node* temp = malloc (sizeof(struct node*));
temp->word = malloc(sizeof(char) * strlen(word) + 1);
strncpy(temp->word, word, strlen(word));
temp->word[strlen(word)] = '\0';
temp->id = malloc(sizeof(char) * strlen(id) + 1);
strncpy(temp->id, id, strlen(id));
temp->id[strlen(id)] = '\0';
//assign the number of occurrences to the argument passed in
temp->occurrences = occurrences;
//return the newly created node
return temp;
}
The struct for the node is:节点的结构是:
struct node {
char* word;
char* id;
int occurrences;
struct node* next;
};
What I mean by one less is that this works:我的意思是少一个是有效的:
strncpy(temp->id, id, strlen(id)-1);
However it means that I am losing one char consistently.但是,这意味着我一直在丢失一个字符。
I've tried to manually copy the string with a for loop but it also doesn't work.我试图用 for 循环手动复制字符串,但它也不起作用。 I've tried appending one '\\0' char but it also doesn't work.
我试过附加一个 '\\0' 字符,但它也不起作用。
If needed I can provide what I'm using to test this如果需要,我可以提供我用来测试的内容
The likely candidate is this line:可能的候选人是这一行:
struct node* temp = malloc (sizeof(struct node*));
Which creates enough space to store a pointer to a node , not to a node itself.这创造了足够的空间来存储指向节点的指针,而不是节点本身。 Remove the
*
from the sizeof
expression.从
sizeof
表达式中删除*
。 Alternatively (and the way I would write this code), just don't use types in sizeof
expressions if you can avoid it:或者(以及我编写此代码的方式),如果可以避免,请不要在
sizeof
表达式中使用类型:
struct node *temp= malloc(sizeof *temp);
Other notes:其他注意事项:
As mentioned by @VladFeinstein, use strdup
instead of your malloc
/ strlen
/ strncpy
/ \\0
dance.正如@VladFeinstein 所提到的,使用
strdup
而不是你的malloc
/ strlen
/ strncpy
/ \\0
舞蹈。
temp->word = strdup(word); temp->id = strdup(id);
If you choose to not do that, notice that your order of operations seems confused in the malloc
size expressions:如果您选择不这样做,请注意您的操作顺序在
malloc
大小表达式中似乎很混乱:
temp->word = malloc(sizeof(char) * strlen(word) + 1);
It's still correct, but only because sizeof(char)
is 1
.它仍然是正确的,但只是因为
sizeof(char)
是1
。 I'd simply write:我只想写:
temp->word = malloc(strlen(word) + 1);
But if you're really set on leaving sizeof(char)
in there, make sure you parenthesize the addition in the expression correctly.但是,如果您真的打算将
sizeof(char)
留在那里,请确保正确地将表达式中的加法括起来。
We can look for an off by 1
error in your code.我们可以在您的代码中查找一个
off by 1
错误。
Alternatively, you can replace the use of malloc
, strncpy
and adding \\0
with one call to strdup
.或者,您可以将
malloc
、 strncpy
的使用替换为对strdup
一次调用并添加\\0
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.