C语言 当试图访问下一个节点时,指向结构的全局指针在函数调用中没有更新,全局指针返回nil

iyzzxitl  于 2023-03-22  发布在  其他
关注(0)|答案(1)|浏览(91)

为什么在第一段代码中,free_hashtable的any-〉next和root-〉next返回nil?如果我在generate函数中打印,它确实返回一个地址......那么,由于 *root是全局的,为什么我不能访问它?
同样,为什么在第二段代码中,当我递归调用一个函数,它有一个指向全局结构指针的参数(这样我就可以在其中导航),它只给我一个主地址,如果我尝试像函数(any-〉next)一样,返回的值与any not any-〉next相同。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

const int nodes = 27; // 26 letters + '.
int height = 3;

typedef struct hash_table
{
    char word[46]; // 45 letters + NULL.
    struct hash_table *next;
}
hash_table;

typedef struct trie
{
    bool is_word;
    struct trie *character[nodes];
    hash_table *next;
}
trie;

trie *generate(int times);
void free_hashtable(trie *any, int times);
void free_hashtable2(hash_table *any);
void free_trie(trie *any);

trie *root;

int main(void)
{
    // Generate a trie with a height specified in a variable
    // and also allocate memory to the entire structure.
    root = generate(height);

    free_hashtable(root, height);
    free_trie(root);
    free(root);

    return 0;
}

trie *generate(int times)
{
    // Allocate memory to root, to character[i]
    // and also change the access of character[i] to root.
    // So in the second run, root would be equivalent to
    // root->character[i].
    root = malloc(sizeof(trie));

    //printf("%p\n", root); HERE it gives me an address.
    root->next = NULL;

    if (times > 0)
    {
        for (int i = 0; i < nodes; i++)
        {
            // For every single pointer generates more nodes pointers.
            root->character[i] = generate(times - 1);
        }
    }

    else
    {
        for (int i = 0; i < nodes; i++)
        {
            // Set all remaining pointers to null
            // as there aren't more arrays.
            root->character[i] = NULL;
        }
        // Malloc for new structure.
        root->next = malloc(sizeof(hash_table));
        root->next->next = NULL;
    }

    // Return the address of root,
    // so the root in the main function can point
    // to the trie created.
    return root;
}

void free_hashtable(trie *any, int times)
{
    printf("%p\n", root->character[0]); // print nil, yet, if I print in the generate function,
    // it does give me an address.

    printf("%p\n", any->character[0]); // returns nil, but why?
    // Shouldn't any = root? and then any->next = root->next?

    for (int i = 0; i < nodes; i++)
    {
        if (times > 1)
        {
            free_hashtable(any->character[i], times - 1);
        }
    }

    if (times == 1)
    {
        for (int i = 0; i < nodes; i++)
        {
            //Where root is root->character[i]->character[i]
            if (any->character[i]->next->next != NULL)
            {
                free_hashtable2(any->character[i]->next);
            }
            free(any->character[i]->next);
        }
    }

    return;
}

void free_hashtable2(hash_table *any)
{
    if (any->next != NULL)
    {
        free_hashtable2(any->next);
        free(any->next);
    }

    return;
}

void free_trie(trie *any)
{
    for (int i = 0; i < nodes; i++)
    {
        if (any->character[i] != NULL)
        {
            // Calls function first so it can works backwards.
            free_trie(any->character[i]);
            free(any->character[i]);
        }
    }

    return;
}
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

typedef struct test
{
    char word[15];
    struct test *next;
}
test;

test *root;
test *generate(int times);
void free_dummy(test *any);

int main(void)
{
    root = generate(3);
    free_dummy(root);
    free(root);
}

test *generate(int times)
{
    root = malloc(sizeof(test));
    strcpy(root->word, "dummy");
    if (root == NULL)
    {
        return NULL;
    }
    if (times != 0)
    {
        root->next = generate(times - 1);
    }
    if (times == 0)
    {
        root->next = NULL;
    }
    return root;
}

void free_dummy(test *any) // Comes as root.
{
    printf("%p\n", any); // any never updates to root->next, but any is pointing to root. Shouldn't any->next = root->next?
    if (any->next != NULL)
    {
        free_dummy(any->next);
        free(any->next);
    }
    return;
}

谢谢大家!欢迎大家帮忙!

laik7k3q

laik7k3q1#

您递归调用函数generate(),会发生两件事:在每次调用中覆盖全局变量root = malloc(...)(泄漏内存),以及在最后一次调用时,当times == 0重置root->characters[i] = NULL.时覆盖全局变量root = malloc(...)
我必须将节点更改为符号常量以使代码能够编译,并更改高度以保持一致性。
generate()中,使用一个局部变量root代替。更喜欢传递一个与类型相反的变量到sizeof。我没有修复它,但你应该检查malloc()是否成功。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

#define nodes 27 // 26 letters + '.
#define height 3

typedef struct hash_table {
    char word[46]; // 45 letters + NULL.
    struct hash_table *next;
} hash_table;

typedef struct trie {
    bool is_word;
    struct trie *character[nodes];
    hash_table *next;
} trie;

trie *generate(int times) {
    trie *root = malloc(sizeof *root);
    root->next = NULL;
    if(!times) {
        for (int i = 0; i < nodes; i++)
            root->character[i] = NULL;
        root->next = malloc(sizeof *root->next);
        root->next->next = NULL;
        return root;
    }
    for (int i = 0; i < nodes; i++)
        root->character[i] = generate(times - 1);
    return root;
}

void free_hashtable2(hash_table *any) {
    if (!any->next) return;
    free_hashtable2(any->next);
    free(any->next);
}

void free_trie(trie *any) {
    for (int i = 0; i < nodes; i++) {
        if (!any->character[i]) continue;
        free_trie(any->character[i]);
        free(any->character[i]);
    }
}

void free_hashtable(trie *any, int times) {
    printf("%p\n", (void *) any->character[0]);
    for (int i = 0; i < nodes; i++)
        if (times > 1)
            free_hashtable(any->character[i], times - 1);
    if (times == 1)
        for (int i = 0; i < nodes; i++) {
            if (any->character[i]->next->next)
                free_hashtable2(any->character[i]->next);
            free(any->character[i]->next);
        }
}

int main(void) {
    trie *root = generate(height);
    free_hashtable(root, height);
    free_trie(root);
    free(root);
}

对我来说,3个不同的自由函数是一种代码气味。相反,编写一个自由函数,它做的是你的构建器函数generate()的“相反”。

相关问题