【问题标题】:Memory allocation doesn't "hold" when returning to main返回 main 时内存分配不会“保持”
【发布时间】:2014-11-21 09:38:08
【问题描述】:

我有一个家庭作业,我需要在其中分配内存给一个指针,该指针指向一个指针数组 (pNode**)。 下面是一个函数,它接收一个指针数组,并为其分配内存以及里面的所有相关指针和数据。

简而言之,函数应该为指向节点指针数组的指针分配内存。

**注意:我已经从函数中删除了与我的问题无关的某些元素。 函数 newGNode 为 Node 结构分配内存

int getChildren(pNode** childrenNodes)
{
    *childrenNodes = (pNode*)malloc(sizeof(pNode));
    for (int i = 0; i < NUM_OF_CHILDREN; i++)
    {
        childrenNodes[i] = (pNode *)newGNode();
    }
    return numOfChildren;
}

这就是我在主函数中的调用方式:

int main()
{
    pNode * ng = NULL;
    int test = getChildren(&ng);

}

无论我尝试做什么,我似乎都无法在主函数中获得分配“坚持”。在 getChildren 函数中,我可以在调试器中看到内存已按照我的需要精确分配。但是,当函数返回主函数时,ng 指针似乎已损坏,因为调试器告诉我它无法读取内存。

我在网上搜索并尝试了不同的方法,但似乎都没有。有谁知道为什么这不能按预期工作?我猜测内存分配中的某些内容是错误的,但似乎无法弄清楚是什么。

Here 是一个与我类似的问题,但它并没有真正帮助我

谢谢!

【问题讨论】:

  • 你传递了两个参数,但原型只有一个。也许您忘记了帖子中的某些内容。
  • 作为旁注:你不应该转换mallocc-faq.com/malloc/mallocnocast.html的结果
  • 您使用的是 C 还是 C++?您应该避免在 C++ 中使用 malloc(以及在 Modern C++ 中,新的)。
  • @G.Samaras 是的,谢谢,修复了帖子(反正第二个参数没有使用)
  • @user475680 我不认为他的意思是你应该使用 calloc 而不是 malloc。您根本不应该在 C++ 中使用任何 [re|m|c]alloc(除非有特殊情况)。如果您不使用 c++,请不要使用 c++ 标记问题。

标签: c arrays pointers void-pointers


【解决方案1】:

1) 你用两个参数调用getChildren(),但它只需要一个参数:

int getChildren(pNode** childrenNodes)

2) 您想要一个数组,但为单个 pNode 保留空间:

*childrenNodes = (pNode*)malloc(sizeof(pNode));

改成

*childrenNodes = malloc(sizeof(pNode) * NUM_OF_CHILDREN); /* Don't cast malloc */

编辑:

看来你想要一个指针数组,那么ng必须声明为:

pNode **ng = NULL; /* pointer to pointer */

您需要 3 级间接接收ng

int getChildren(pNode ***childrenNodes)

并为指针数组保留空间:

*childrenNodes = malloc(sizeof(pNode *) * NUM_OF_CHILDREN);

ints 的示例:

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

int arr[] = {1, 2};

void func(int ***p)
{
    *p = malloc(sizeof(int *) * 2);
    (*p)[0] = &arr[0];
    (*p)[1] = &arr[1];
}

int main(void)
{
    int **p;

    func(&p);
    printf("%d %d\n", *p[0], *p[1]);
    free(p);
    return 0;
}

但是***被认为是不好的风格,而不是:

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

int arr[] = {1, 2};

int **func(void)
{
    int **p;

    p = malloc(sizeof(int *) * 2);
    p[0] = &arr[0];
    p[1] = &arr[1];
    return p;
}

int main(void)
{
    int **p = func();

    printf("%d %d\n", *p[0], *p[1]);
    free(p);
    return 0;
}

【讨论】:

  • 是的,谢谢,我在发布后注意到了有问题的 malloc。这似乎有效,但是发生了一些奇怪的事情。当我访问 main 中的数组指针时,似乎只分配了 5 个中的 3 个。当我查看 &ng[3] 时,我看到了垃圾,但是当我查看 &ng[0] 时,我看到了实际数据(0 等)
  • 如果你想分配n指针使用*childrenNodes = malloc(sizeof(pNode *) * NUM_OF_CHILDREN);(注意pNode *而不是pnode),并且ng必须声明为pNode **ng = NULL;
  • 行“*childrenNodes = malloc(sizeof(pNode *) * NUM_OF_CHILDREN);”使 Visual Studio 崩溃(未处理的异常...),但是当我删除 * 并编写 childrenNodes = 时,它可以正常工作。阅读您的答案后,我进行了一些调整,逻辑上似乎还可以,在函数内分配似乎可以正常工作,但同样,在主函数中,无论我在调试中的“手表”中做什么,它都显示为 Unable读内存
【解决方案2】:

我希望其他回答能解决您的问题,但如果您不介意另一种方法,[不使用双指针],请考虑以下代码。我相信这是自我解释。

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

int * alloc_memory()
{
    int * pTempInt = NULL;

    pTempInt = calloc (1, sizeof(*pTempInt));  //you can replace 1 with desired number
    printf("in alloc_memory, before alloc pInt = %p\n", pTempInt);

    return pTempInt;

}


int main()
{

    int * pInt = NULL;

    printf("in main, before alloc pInt = %p\n", pInt);
    pInt = alloc_memory();
    printf("in main, after alloc pInt = %p\n", pInt);

    free(pInt);

    return 0;
}

【讨论】:

    【解决方案3】:
    //you could attack the problem directly with:
    // where 'Node' is the struct tag name, 
    // not a pointer to struct definition 
    // and not and struct definition name 
    // and not a typedef of a struct
    
    // note:
    // gdb prefers struct tag names as then it can display 
    // each of the associated fields of the struct.
    
    // also struct { ... } name; is a depreciated format in modern C
    
    struct Node
    {
        ...
    };
    
    struct Node ** getChildren( void );
    
    struct Node ** getChildren()
    {
        // get array of pointers to nodes
        struct Node ** ppChildNodes = malloc( sizeof(struct Node*)*NUM_OF_CHILDREN);
    
        // get struct Node size allocation for each child pointer
        for( int i=0; i< NUM_OF_CHILDREN; i++)
        {
            ppChildNodes[i] = malloc( sizeof(struct Node) );
        }
        return ppChildNodes;
    }
    
    
    int main()
    {
        struct Node ** ng = getChildren();
    
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-17
      • 2014-10-23
      • 1970-01-01
      • 2021-08-23
      • 1970-01-01
      • 2018-08-20
      • 2018-07-25
      • 2012-09-21
      • 2013-12-17
      相关资源
      最近更新 更多