如何在C中初始化指向指针结构的指针?

发布于 2024-09-29 06:24:48 字数 766 浏览 6 评论 0原文

我有一个结构,它是一个节点,另一个结构是这些节点的列表。在列表结构中,它是一个节点数组,但它不是一个数组,而是一个指向具有大小整数的指针的指针:

typedef struct node {
    struct node *next;
    MyDef *entry;
} Node;


typedef struct list {
    Node **table;
    int size;
} List;

List *initialize(void)
{
    List *l;
    Node **n;

    if ((l = (List *)malloc(sizeof(List))) == NULL)
        return NULL;
    l->size = 11;

    /* I think this is correctly allocating the memory for this 'array' of nodes */
    if ((n = (Node **)malloc(l->size * sizeof(Node))) == NULL)
        return NULL;

    /* Now, how do I set MyDef *entry and Node *next to NULL for each of the 'array'? */

    l->table = n;

    return l;
}

How do I set MyDef *entry and Node *next to NULL for every 'array'?

I have a struct which is a node, and another which is a list of these nodes. In the list struct, its an array of nodes, but instead of an array, it's a pointer to pointer with a size integer:

typedef struct node {
    struct node *next;
    MyDef *entry;
} Node;


typedef struct list {
    Node **table;
    int size;
} List;

List *initialize(void)
{
    List *l;
    Node **n;

    if ((l = (List *)malloc(sizeof(List))) == NULL)
        return NULL;
    l->size = 11;

    /* I think this is correctly allocating the memory for this 'array' of nodes */
    if ((n = (Node **)malloc(l->size * sizeof(Node))) == NULL)
        return NULL;

    /* Now, how do I set MyDef *entry and Node *next to NULL for each of the 'array'? */

    l->table = n;

    return l;
}

How do I set MyDef *entry and Node *next to NULL for each of the 'array'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

糖果控 2024-10-06 06:24:48

(Node **) 是指向 Node 的 [array of] 指针,因此您分配的数组不会有任何结构成员。

您应该使用 (Node *),然后您将拥有 Node 结构的指向数组,或者单独分配每个 Node,然后将指向它们的指针放入数组中。

标准 C 库中存在适合您情况的函数 calloc() :它用 0 初始化分配的区域(对应于 (char/short/int/long)0、0.0 和 NULL)。

还有内存泄漏。

/* I think this is correctly allocating the memory for this 'array' of nodes */
if (... == NULL)
    return NULL;

当数组分配失败时,您不会释放 List,但会丢失指向它的指针。将其重写为:

/* I think this is correctly allocating the memory for this 'array' of nodes */
if ((n = (Node **)malloc(l->size * sizeof(Node))) == NULL) {
    free(l);
    return NULL;
}

因此,从我的角度来看,正确的代码将是:

typedef struct node {
    struct node *next;
    MyDef *entry;
} Node;


typedef struct list {
    Node *table; /* (!) single asterisk */
    int size;
} List;

List *initialize(void)
{
    List *l;
    Node **n;

    if ((l = (MList *)malloc(sizeof(List))) == NULL)
        return NULL;
    l->size = 11;

    /* I think this is correctly allocating the memory for this 'array' of nodes */
    if ((n = (Node *)calloc(l->size, sizeof(Node))) == NULL)
    {
        free(l);
        return NULL;
    }

    /* Now, how do I set MyDef *entry and Node *next to NULL for each of the 'array'? */

    l->table = n;

    return l;
}

此外,C99 允许您创建可变大小的结构,因此您可以像这样初始化结构

typedef struct list {
    int size;
    Node table[0]
} List;

并在表中分配尽可能多的节点
malloc(sizeof(List) + sizeof(Node)*n);

(Node **) is pointer to [array of] pointer to Node, so array you allocate will not have any struct members.

You should use (Node *) and then you'll have pointed array of Node structs, or allocate each Node separately, then place pointers to them into your array.

There's exist function calloc() in standard C library for your case: it inits allocated area with 0's (which corresponds to (char/short/int/long)0, 0.0 and NULL).

Also there's a memory leak.

/* I think this is correctly allocating the memory for this 'array' of nodes */
if (... == NULL)
    return NULL;

When array allocation fails you do not free List, but lose pointer to it. Rewrite it as:

/* I think this is correctly allocating the memory for this 'array' of nodes */
if ((n = (Node **)malloc(l->size * sizeof(Node))) == NULL) {
    free(l);
    return NULL;
}

So from my point of wiev correct code would be:

typedef struct node {
    struct node *next;
    MyDef *entry;
} Node;


typedef struct list {
    Node *table; /* (!) single asterisk */
    int size;
} List;

List *initialize(void)
{
    List *l;
    Node **n;

    if ((l = (MList *)malloc(sizeof(List))) == NULL)
        return NULL;
    l->size = 11;

    /* I think this is correctly allocating the memory for this 'array' of nodes */
    if ((n = (Node *)calloc(l->size, sizeof(Node))) == NULL)
    {
        free(l);
        return NULL;
    }

    /* Now, how do I set MyDef *entry and Node *next to NULL for each of the 'array'? */

    l->table = n;

    return l;
}

Futhermore C99 allows you to make variable size structs, so you able to init struct like

typedef struct list {
    int size;
    Node table[0]
} List;

And allocate as many Node's in table as you want using
malloc(sizeof(List) + sizeof(Node)*n);

瞄了个咪的 2024-10-06 06:24:48

首先,在我看来,您在分配数组的代码中存在错误:它应该说 sizeof(Node*) 而不是 sizeof(Node) ,因为您想要分配一个指向 Node 的指针数组而不是 Node 对象数组。

然后您可以遍历数组列表:

for ( unsigned i = 0; i < l->size; ++i )
{
    Node* node = l->table[ i ];
    node->entry = NULL;
    node->next = NULL;
}

另一个提示:您确实应该检查初始化函数是否存在内存泄漏的可能性。

First of all, it seems to me that you have an error in your code allocating the array: It should say sizeof(Node*) rather than sizeof(Node) as you want to allocate an array of pointers to Node not an array of Node objects.

Then you can iterate through the array list:

for ( unsigned i = 0; i < l->size; ++i )
{
    Node* node = l->table[ i ];
    node->entry = NULL;
    node->next = NULL;
}

Another hint: You really should check your initialization function against possibilities of memory leaks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文