gpt4 book ai didi

c - 使用 malloc 释放在 c 中分配的空间

转载 作者:太空狗 更新时间:2023-10-29 17:16:57 26 4
gpt4 key购买 nike

我对这段代码有疑问:

typedef struct pop {
unsigned long int *np; // matrix
unsigned long int f;
long double fp;
unsigned long int *R; // matrix
unsigned long int *C; // matrix
unsigned long int Dp;
unsigned long int Ds;
unsigned long int count;
struct popolazione *ptrTempLst; // pointer
struct popolazione *leftTree; // left tree pointer
struct popolazione *rightTree; // right tree pointer
} Node;

当我释放为此结构分配的空间时,我是否必须先释放指向结构内部矩阵的指针?

例如,

 Node *ptr=(Node *) malloc(sizeOf(Node));
ptr->np=(unsigned long int *)malloc(10*sizeOf(unsigned long int));

/*code code code*/

// is necessary: free(ptr->np);

free(ptr);

提前致谢

最佳答案

是的。

每次调用 malloc 都必须匹配调用 free

关于c - 使用 malloc 释放在 c 中分配的空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10742988/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com