Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 内存分配不需要';";持有;当返回main时_C_Arrays_Pointers_Void Pointers - Fatal编程技术网

C 内存分配不需要';";持有;当返回main时

C 内存分配不需要';";持有;当返回main时,c,arrays,pointers,void-pointers,C,Arrays,Pointers,Void Pointers,我有一个家庭作业,其中我需要分配内存给指向指针数组的指针(pNode**)。 下面是一个函数,它接收指针数组的数组,并为其分配内存以及其中的所有相关指针和数据 简而言之,函数应该为指向节点指针数组的指针分配内存 **注意:我已经从函数中删除了与我的问题无关的某些元素。 函数newGNode为节点结构分配内存 int getChildren(pNode** childrenNodes) { *childrenNodes = (pNode*)malloc(sizeof(pNode));

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

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

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

int getChildren(pNode** childrenNodes)
{
    *childrenNodes = (pNode*)malloc(sizeof(pNode));
    for (int i = 0; i < NUM_OF_CHILDREN; i++)
    {
        childrenNodes[i] = (pNode *)newGNode();
    }
    return numOfChildren;
}
无论我尝试做什么,我似乎都无法在主函数中获得分配“stick”。在getChildren函数中,我可以在调试器中看到内存的分配正是我想要的。但是,当函数返回main时,ng指针似乎已损坏,因为调试器告诉我它无法读取内存

我在网上搜索过,尝试过不同的方法,但似乎都不管用。有人知道为什么这不能按预期工作吗?我猜内存分配中的某些内容是错误的,但似乎不知道是什么

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

谢谢大家!

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\u子节点)

带有
int
s的示例:

#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 * 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;
}
#包括
#包括
int*alloc_内存()
{
int*pTempInt=NULL;
pTempInt=calloc(1,sizeof(*pTempInt));//您可以用所需的数字替换1
printf(“在alloc_内存中,在alloc pInt=%p\n”之前,pTempInt);
返回pTempInt;
}
int main()
{
int*pInt=NULL;
printf(“主要,在alloc pInt=%p\n”之前,品脱);
pInt=alloc_memory();
printf(“在main中,alloc pInt=%p\n”之后,pInt);
免费(品脱);
返回0;
}
//您可以通过以下方法直接解决此问题:
//其中“Node”是结构标记名,
//不是指向结构定义的指针
//and not和结构定义名称
//而不是结构的typedef
//注:
//gdb更喜欢结构标记名,因为它可以显示
//结构的每个关联字段。
//还有结构{…}名称;在现代C语言中是一种贬值格式
结构体类型
{
...
};
结构节点**获取子节点(空);
结构节点**getChildren()
{
//获取指向节点的指针数组
结构节点**ppChildNodes=malloc(sizeof(struct Node*)*NUM\u子节点);
//获取每个子指针的结构节点大小分配
for(int i=0;i
您传递了两个参数,但原型有一个参数。也许你忘了你的帖子里的东西。作为旁白:你不应该把<代码> MalOC[/CODE ]的结果用C或C++来做。你应该避免C++中的Malc(和在现代C++中,new)。@ G.SAMARAS是的,谢谢,固定了POST(第二个PARAM无论如何都没有使用)@ USE475 680。我认为他不是说你应该使用CaloC而不是MALOC。你不应该在C++中使用任何[ReMyc]的异类(除非特殊情况除外)。如果你不使用C++,不要用C++来标记问题。是的,谢谢,我注意到发布后有问题的MALOC。这似乎是可行的,但有一些奇怪的事情。当我访问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=”时,它工作正常。在阅读了你的答案后,我做了一些调整,在逻辑上似乎还可以,在函数中分配似乎工作正常,但在主函数中,无论我在调试中的“watch”中做什么,它都表示无法读取内存
#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;
}
#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;
}
//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();

}