[英]Strcpy resulting in segmentation fault
I'm not really sure why strcpy is resulting in a segmentation fault and was wondering if someone could explain to me why. 我不太确定为什么strcpy会导致分段错误,并且想知道是否有人可以向我解释原因。 I originally had
temp->data = name
but that resulted in the Node value changing every time I changed the name array and was looking for a solution 我本来是
temp->data = name
但是这导致每次我更改name数组并寻找解决方案时Node值都会改变
typedef struct BST {
char *data;
struct BST *left;
struct BST *right;
}node;
node *create(char name[]){
node *temp;
temp = (node *) malloc(strlen(name) + 1);
strcpy(temp->data, name);
temp->left = temp->right = NULL;
return temp;
}
Given the structure shown, you are allocating insufficient memory and copying to an uninitialized pointer. 给定所示的结构,您正在分配不足的内存并将其复制到未初始化的指针。 Both are dangerous.
两者都是危险的。
You need something more like: 您需要更多类似的东西:
node *create(char name[]){
node *temp = malloc(sizeof(*temp));
if (temp == NULL)
return NULL;
temp->data = malloc(strlen(name) + 1);
if (temp->data == NULL)
{
free(temp);
return NULL;
}
strcpy(temp->data, name);
temp->left = temp->right = NULL;
// temp->generation = 0; // removed from revised question
return temp;
}
Consider whether you can use strdup()
to allocate a copy (duplicate) of the string. 考虑是否可以使用
strdup()
分配字符串的副本(重复项)。 You'd still need to check that was successful. 您仍然需要检查是否成功。 Note that freeing a
node
involves two calls to free()
. 请注意,释放
node
涉及两次调用free()
。 Also, the calling code needs to check whether the node was successfully allocated. 另外,调用代码需要检查节点是否已成功分配。 However, this code imposes no error handling strategy on its caller — the calling code can do what it likes as long as it doesn't try to dereference a null pointer returned by the code.
但是,此代码没有在其调用方上施加任何错误处理策略-调用代码可以执行其喜欢的操作,只要它不尝试取消引用该代码返回的空指针即可。
Alternatively, you could use a C99 'flexible array member' like this: 另外,您可以使用C99“灵活数组成员”,如下所示:
typedef struct BST {
struct BST *left;
struct BST *right;
char data[];
} node;
node *create(char name[]){
node *temp = malloc(sizeof(*temp) + strlen(name) + 1);
if (temp == NULL)
return NULL;
strcpy(temp->data, name);
temp->left = temp->right = NULL;
// temp->generation = 0; // removed from revised question
return temp;
}
Now you can free the structure with a single free()
call. 现在,您可以通过一次
free()
调用来释放结构。 However, you can't create an array of these structures (though you could have an array of pointers to such structures). 但是,您不能创建这些结构的数组(尽管您可以具有指向此类结构的指针的数组)。 In the context of a tree, that's unlikely to be a problem.
在树的上下文中,这不太可能成为问题。
You should malloc your node first with temp = (node *) malloc(sizeof(node));
您应该首先使用
temp = (node *) malloc(sizeof(node));
then malloc your new string with temp->data = (char *) malloc(strlen(name) + 1);
然后使用
temp->data = (char *) malloc(strlen(name) + 1);
malloc您的新字符串temp->data = (char *) malloc(strlen(name) + 1);
then you can use strcpy(temp->data, name);
然后您可以使用
strcpy(temp->data, name);
Also, you need to set your generation to whatever value you want. 另外,您需要将生成的时间设置为所需的任何值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.