在 C 中将 *char 数组的元素设置为 NULL
Set an element of an array of *char as NULL in C
我的任务是遍历二叉搜索树并收集每个节点中的键并将它们存储在数组中。在此之后,我必须将这个数组的最后一个元素设置为 NULL。这一切都在一个函数内完成,然后 returns 这个 *char.
数组
我相信关于收集密钥并将它们存储在数组中的部分运行良好,因为我已经对其进行了足够的测试。但是,我无法将数组的最后一个元素正确定义为 NULL。我认为应该这样做:
list_keys[tree_size(tree)] = NULL;
然而,在一些 printf 之后,我意识到这是在清理我的树。当我在此行之前打印 tree_size(tree) 时,它会正确地给出树的大小。但是,当我在该行之后执行此操作时,它给了我 0。我不认为问题出在函数 tree_size()
中,因为当我尝试访问该行之前的树元素时,它运行良好,但在该行之后,我收到 Segmentation fault (core dumped)
错误,可能是由于尝试访问不再存在的内容。
我不知道出了什么问题,非常感谢您的帮助。提前致谢。
编辑:
tree
属于 tree_t
类型,定义为:
struct tree_t {
struct entry_t *entry;
struct tree_t *right_child;
struct tree_t *left_child;
};
tree_size()
定义为:
int tree_size(struct tree_t *tree) {
if (tree->entry != NULL) {
//if two children exist
if (tree->left_child != NULL && tree->right_child != NULL) {
return 1 + tree_size(tree->left_child) + tree_size(tree->right_child);
}
//there's a right child and there isnt a left child
if (tree->right_child != NULL && tree->left_child == NULL) {
return 1 + tree_size(tree->right_child);
}
//there's a left child and there isnt a right child
if (tree->left_child != NULL && tree->right_child == NULL) {
return 1 + tree_size(tree->left_child);
}
//there are no children
if (tree->left_child == NULL && tree->right_child == NULL) {
return 1;
}
}
//if the entry is empty
return 0;
}
基本上,我现在正在做的是:
首先,我定义list_keys
并分配内存:
char **list_keys;
list_keys = (char *)malloc((tree_size(tree)+1)*sizeof(char));
然后,我调用一个辅助函数 tree_get_keys_aux(tree, list_keys, 0)
来完成我提到的初始部分。它被定义为:
void tree_get_keys_aux(struct tree_t *tree, char **list_keys, int index) {
//N
list_keys[index] = (char *)malloc((strlen(tree->entry->key))*sizeof(char)); //allocating memory for each string that I want to add to the list
strcpy(list_keys[index],tree->entry->key); //copying the content
index = index + 1;
//L
if (tree->left_child != NULL) {
tree_get_keys_aux(tree->left_child, list_keys, index);
}
//R
if (tree->right_child != NULL) {
tree_get_keys_aux(tree->right_child, list_keys, index);
}
return;
}
然后,我做给我带来问题的那一行,
list_keys[tree_size(tree)] = NULL;
最后,
return list_keys;
首先,
list_keys = (char *)malloc((tree_size(tree)+1)*sizeof(char));
错了。元素是 char*
,所以你必须为此分配,否则你将导致 out-of-range 访问。
应该是:
list_keys = malloc((tree_size(tree)+1)*sizeof(char*));
或
list_keys = malloc((tree_size(tree)+1)*sizeof(*list_keys));
另请参阅:c - Do I cast the result of malloc? - Stack Overflow
其次,
list_keys[index] = (char *)malloc((strlen(tree->entry->key))*sizeof(char)); //allocating memory for each string that I want to add to the list
strcpy(list_keys[index],tree->entry->key); //copying the content
错了。您必须再分配一个元素来终止 null-character.
应该是:
list_keys[index] = malloc((strlen(tree->entry->key) + 1)*sizeof(char)); //allocating memory for each string that I want to add to the list
strcpy(list_keys[index],tree->entry->key); //copying the content
第三,tree_get_keys_aux
未识别左侧 child 中的元素数量,左侧 child 中的数据将被右侧 child 中的数据覆盖。
为避免这种覆盖,您可以使用 tree_size
来确定树的大小并据此推进 index
。
//L
if (tree->left_child != NULL) {
tree_get_keys_aux(tree->left_child, list_keys, index);
index += tree_size(tree->left_child); // add this
}
//R
if (tree->right_child != NULL) {
tree_get_keys_aux(tree->right_child, list_keys, index);
}
我的任务是遍历二叉搜索树并收集每个节点中的键并将它们存储在数组中。在此之后,我必须将这个数组的最后一个元素设置为 NULL。这一切都在一个函数内完成,然后 returns 这个 *char.
数组我相信关于收集密钥并将它们存储在数组中的部分运行良好,因为我已经对其进行了足够的测试。但是,我无法将数组的最后一个元素正确定义为 NULL。我认为应该这样做:
list_keys[tree_size(tree)] = NULL;
然而,在一些 printf 之后,我意识到这是在清理我的树。当我在此行之前打印 tree_size(tree) 时,它会正确地给出树的大小。但是,当我在该行之后执行此操作时,它给了我 0。我不认为问题出在函数 tree_size()
中,因为当我尝试访问该行之前的树元素时,它运行良好,但在该行之后,我收到 Segmentation fault (core dumped)
错误,可能是由于尝试访问不再存在的内容。
我不知道出了什么问题,非常感谢您的帮助。提前致谢。
编辑:
tree
属于 tree_t
类型,定义为:
struct tree_t {
struct entry_t *entry;
struct tree_t *right_child;
struct tree_t *left_child;
};
tree_size()
定义为:
int tree_size(struct tree_t *tree) {
if (tree->entry != NULL) {
//if two children exist
if (tree->left_child != NULL && tree->right_child != NULL) {
return 1 + tree_size(tree->left_child) + tree_size(tree->right_child);
}
//there's a right child and there isnt a left child
if (tree->right_child != NULL && tree->left_child == NULL) {
return 1 + tree_size(tree->right_child);
}
//there's a left child and there isnt a right child
if (tree->left_child != NULL && tree->right_child == NULL) {
return 1 + tree_size(tree->left_child);
}
//there are no children
if (tree->left_child == NULL && tree->right_child == NULL) {
return 1;
}
}
//if the entry is empty
return 0;
}
基本上,我现在正在做的是:
首先,我定义list_keys
并分配内存:
char **list_keys;
list_keys = (char *)malloc((tree_size(tree)+1)*sizeof(char));
然后,我调用一个辅助函数 tree_get_keys_aux(tree, list_keys, 0)
来完成我提到的初始部分。它被定义为:
void tree_get_keys_aux(struct tree_t *tree, char **list_keys, int index) {
//N
list_keys[index] = (char *)malloc((strlen(tree->entry->key))*sizeof(char)); //allocating memory for each string that I want to add to the list
strcpy(list_keys[index],tree->entry->key); //copying the content
index = index + 1;
//L
if (tree->left_child != NULL) {
tree_get_keys_aux(tree->left_child, list_keys, index);
}
//R
if (tree->right_child != NULL) {
tree_get_keys_aux(tree->right_child, list_keys, index);
}
return;
}
然后,我做给我带来问题的那一行,
list_keys[tree_size(tree)] = NULL;
最后,
return list_keys;
首先,
list_keys = (char *)malloc((tree_size(tree)+1)*sizeof(char));
错了。元素是 char*
,所以你必须为此分配,否则你将导致 out-of-range 访问。
应该是:
list_keys = malloc((tree_size(tree)+1)*sizeof(char*));
或
list_keys = malloc((tree_size(tree)+1)*sizeof(*list_keys));
另请参阅:c - Do I cast the result of malloc? - Stack Overflow
其次,
list_keys[index] = (char *)malloc((strlen(tree->entry->key))*sizeof(char)); //allocating memory for each string that I want to add to the list
strcpy(list_keys[index],tree->entry->key); //copying the content
错了。您必须再分配一个元素来终止 null-character.
应该是:
list_keys[index] = malloc((strlen(tree->entry->key) + 1)*sizeof(char)); //allocating memory for each string that I want to add to the list
strcpy(list_keys[index],tree->entry->key); //copying the content
第三,tree_get_keys_aux
未识别左侧 child 中的元素数量,左侧 child 中的数据将被右侧 child 中的数据覆盖。
为避免这种覆盖,您可以使用 tree_size
来确定树的大小并据此推进 index
。
//L
if (tree->left_child != NULL) {
tree_get_keys_aux(tree->left_child, list_keys, index);
index += tree_size(tree->left_child); // add this
}
//R
if (tree->right_child != NULL) {
tree_get_keys_aux(tree->right_child, list_keys, index);
}