[英]Array of pointers to structs in a pointer to a struct
The code below attempts to initialise a node and in doing so, dynamically initialise an array of pointers to child nodes. 下面的代码尝试初始化节点,并在此过程中动态初始化指向子节点的指针数组。 However, I get a Segmentation fault: 11
when I try to access the children. 但是,我遇到了Segmentation fault: 11
当我尝试访问子级时。 I realise that I should not be getting any meaningful values (ie it will just be junk in memory) but I don't know why I'm getting the segmentation fault. 我意识到我不应该获取任何有意义的值(即,它将只是内存中的垃圾),但是我不知道为什么会遇到分段错误。
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include <string.h>
#define INIT_SIZE 10
typedef struct node_t node_t;
struct node_t {
char *word;
node_t **children;
int arr_len;
};
void set_array_vals(node_t *root);
int main(int argc, char const *argv[]) {
char *word = "hello";
node_t *root = malloc(sizeof(node_t));
assert(root);
root->children = malloc(sizeof(node_t *) * INIT_SIZE);
assert(root->children);
root->arr_len = INIT_SIZE;
root->word = malloc((sizeof(char)) * (strlen(word) + 1));
assert(root->word);
strcpy(root->word, word);
set_array_vals(root);
printf("Arr len: %d\n", root->arr_len);
return 0;
}
void set_array_vals(node_t *root) {
int i;
for (i=1; i<root->arr_len; i++) {
node_t *this_node = root->children[i];
printf("%d: %d\n", i, this_node->arr_len);
}
}
In set_array_vals
you get the pointers from the "array" root->children
, but that array is not initialized, and the pointers will be indeterminate and seemingly random. 在set_array_vals
您从“ array” root->children
获取指针,但是该数组未初始化,并且指针将是不确定的并且看似随机的。 Dereferencing those pointers leads to undefined behavior . 取消引用那些指针会导致未定义的行为 。
Also, you seem to forget that array indexes start at zero . 同样,您似乎忘记了数组索引从零开始 。 And once you make all pointers in the root->children
array valid, you have to remember to initialize the structures they point to as well, otherwise the value of this_node->arr_len
will be indeterminate. 并且一旦使root->children
数组中的所有指针有效,就必须记住还要初始化它们指向的结构,否则this_node->arr_len
的值将是不确定的。
As pointed by others, firstly children
is of node_t **
type & you have allocated memory only for root->children
not for root->children[row]
. 正如其他人指出的那样,首先, children
是node_t **
类型的,并且您仅为root->children
分配了内存,而不为root->children[row]
分配了内存。 Allocate memory dynamically for root->children[row]
& assign some values. 为root->children[row]
动态分配内存并分配一些值。 It may looks like 看起来像
root->arr_len = INIT_SIZE;
for(int row = 0; row < root->arr_len ;row++) {
root->children[row] = malloc(sizeof(node_t));/* allocate memory for each children */
root->children[row]->arr_len = row + 99;/* ?? assign some values into member of struct so that you can print in
set_array_vals & verify */
}
And in set_array_vals()
start printing from i=0
as above you allocated memory from root->children[0]
to root->children[9]
& accessing beyond size may results in undefined behavior. 在set_array_vals()
从i=0
开始打印,如上所述,您set_array_vals()
内存从root->children[0]
分配给root->children[9]
,超出大小的访问可能导致不确定的行为。
void set_array_vals(node_t *root) {
for (int i = 0; i < root->arr_len; i++) { /* start printing from i=0 , not i=1 */
#if 0
node_t *this_node = root->children[i]; /* no need of any temporary pointer */
printf("%d: %d\n", i, this_node->arr_len);
#endif
printf("%d: %d\n", i, root->children[i]->arr_len);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.