Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/72.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 无法释放父结构的子结构_C_Memory_Struct_Malloc - Fatal编程技术网

C 无法释放父结构的子结构

C 无法释放父结构的子结构,c,memory,struct,malloc,C,Memory,Struct,Malloc,我有一个奇怪的bug,我无法释放一个我用malloc()存储的结构 我有一个结构父结构和子结构,其中child是int结构。我使用malloc()为父结构和子结构分配ram(其中子结构是malloc(),大小与子结构的数组相同)。然后我使用memcpy将数组复制到父结构上 #include <stdlib.h> #include <stdio.h> #include <string.h> typedef struct child child_t; type

我有一个奇怪的bug,我无法释放一个我用malloc()存储的结构

我有一个结构父结构和子结构,其中child是int结构。我使用malloc()为父结构和子结构分配ram(其中子结构是malloc(),大小与子结构的数组相同)。然后我使用memcpy将数组复制到父结构上

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

typedef struct child child_t;
typedef struct parent parent_t;

struct child {
    int item;
};

struct parent {
    child_t *_child;
};

child_t list[] = {
    { .item = 1 },
    { .item = 2 },
    { .item = 3 },
};

int main(void) {
    parent_t *_parent = malloc(sizeof(parent_t));

    _parent->_child = malloc(sizeof(list));
    memcpy(&_parent->_child, &list, sizeof(list));

    free(_parent->_child);
    free(_parent);
    printf("success\n");
    return 1;
}
#包括
#包括
#包括
类型定义结构子项;
typedef结构父级\u t;
结构子对象{
国际项目;
};
结构父级{
儿童;
};
子列表[]={
{.item=1},
{.item=2},
{.item=3},
};
内部主(空){
parent_t*_parent=malloc(sizeof(parent_t));
_父项->子项=malloc(sizeof(list));
memcpy(&_parent->_child,&list,sizeof(list));
免费(_父->子);
自由(父母);
printf(“成功”\n);
返回1;
}
free(\u parent->\u child)最后只是给我一个中止(内核转储)的错误。我已经检查了ram的使用情况,我知道我应该释放孩子,但我不知道怎么做。

memcpy(&u parent->\u child,&list,sizeof(list))是问题所在

尝试:

memcpy(_parent->_child,list,sizeof(list))

memcpy
的签名为:

void*memcpy(void*str1,const-void*str2,大小)

而是将一个指针传递给一个指针

这会触发未定义的行为,因为您的缓冲区包含垃圾,而垃圾基本上是
free()
无法使用的。

memcpy(&u parent->\u child,&list,sizeof(list))是问题所在

尝试:

memcpy(_parent->_child,list,sizeof(list))

memcpy
的签名为:

void*memcpy(void*str1,const-void*str2,大小)

而是将一个指针传递给一个指针


这会触发未定义的行为,因为您的缓冲区包含垃圾,而垃圾基本上是
free()
无法使用的。

您应该从
memcpy
语句中删除
&
s
\u parent->\u child
已经是地址,数组
列表[]
将衰减为指针。这样做的效果是覆盖指针成员本身(和其他成员),而不是它所指向的内存

memcpy(_parent->_child, list, sizeof(list));
这就是为什么你不能

free(_parent->_child);

因为您破坏了试图释放的指针,所以应该从
memcpy
语句中删除
&
s
\u parent->\u child
已经是地址,数组
列表[]
将衰减为指针。这样做的效果是覆盖指针成员本身(和其他成员),而不是它所指向的内存

memcpy(_parent->_child, list, sizeof(list));
这就是为什么你不能

free(_parent->_child);

因为你破坏了你试图释放的指针,所以我只需要
&\u parent->child
&list
,如果
\u parent->child
不是指针,而
list
不是数组?是的,如果它们是简单类型,但使用
memcpy
时,参数几乎总是数组,或者一个指针,因为简单类型不需要
memcpy
struct
可以传递给
memcpy
,在这种情况下,您需要
,因为
struct
可以按值传递。但是无论如何,你可以用
=
分配一个
结构
值,就像分配一个简单类型一样。啊,所以我只需要
&\u parent->child
&list
,如果
\u parent->child
不是指针,而
list
不是数组?是的,如果它们是简单类型,但是使用
memcpy
时,参数几乎总是一个数组或指针,因为简单类型不需要
memcpy
struct
可以传递给
memcpy
,在这种情况下,您需要
,因为
struct
可以按值传递。但是无论如何,您可以使用
=
分配一个
struct
值,就像使用简单类型一样。