C 如何通过指向结构的指针间接释放并写入结构内部数组中的指针以使其为空?
我必须将指针指向的引用设置为NULL。C 如何通过指向结构的指针间接释放并写入结构内部数组中的指针以使其为空?,c,pointers,recursion,null,children,C,Pointers,Recursion,Null,Children,我必须将指针指向的引用设置为NULL。 但是函数deleteP(…)似乎不起作用,由输出指示。 函数delete()和memoryset()以某种方式工作,即使后者只是用零填充内存(由数组中的指针指向)。 我希望数组中的指针最终为NULL,但这不起作用 我需要通过指向结构(在我的代码中是el,是deleteP(…)中的一个局部指针变量)的指针来完成所有操作(即将fs->child[20]设置为NULL等)。这是因为我多次迭代fs子级及其子级,然后将当前子级放入el 我该怎么解决呢 #includ
但是函数
deleteP(…)
似乎不起作用,由输出指示。函数
delete()
和memoryset()
以某种方式工作,即使后者只是用零填充内存(由数组中的指针指向)。我希望数组中的指针最终为NULL,但这不起作用 我需要通过指向结构(在我的代码中是
el
,是deleteP(…)
中的一个局部指针变量)的指针来完成所有操作(即将fs->child[20]
设置为NULL等)。这是因为我多次迭代fs
子级及其子级,然后将当前子级放入el
我该怎么解决呢
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define HEIGHT 255
#define LENGTH 256
#define MAX_CHILDREN 1024
typedef struct FS FS;
typedef struct Elem Elem;
struct Elem {
char name[LENGTH];
char content[256];
int isFile;
int emptyCells;
Elem *child[MAX_CHILDREN];
};
struct FS {
int emptyCells;
Elem *child[MAX_CHILDREN];
};
void delete(FS *fs){
free(fs->child[20]);
fs->child[20] = NULL;
}
void deleteP(FS *fs){
Elem *el = calloc(1, sizeof (Elem));
el = fs->child[20];
free(el);
el = NULL;
}
void memoryset(FS *fs){
Elem *el = calloc(1, sizeof (Elem));
el = fs->child[20];
memset(el, 0, sizeof(Elem));
}
int main(int argc, const char * argv[]) {
FS *fs = calloc (1, sizeof(FS));
fs->emptyCells = MAX_CHILDREN;
fs->child[20] = calloc (1, sizeof(Elem));
strcpy(fs->child[20]->name, "Hello");
printf("No delete: %s\n", fs->child[20]->name);
memoryset(fs);
printf("MEMSET: %s\n", fs->child[20]->name);
fs->child[20] = calloc (1, sizeof(Elem));
strcpy(fs->child[20]->name, "Hello");
delete(fs);
printf("Delete: %s\n", fs->child[20]->name);
fs->child[20] = calloc (1, sizeof(Elem));
strcpy(fs->child[20]->name, "Hello");
deleteP(fs);
printf("DeleteP: %s\n", fs->child[20]->name);
}
请允许我用我的话表达你的目标
(在聊天中验证我的理解后):
- 要释放存储在数组中的指针指向的内存, 它在一个结构中,你可以得到一个指向它的指针
- 无法直接访问数组成员,
仅通过指向包含结构的指针 - 您还希望将NULL写入数组成员
- 查看代码,您试图通过创建数组成员(指针)的副本来实现这一点
- 它基本上是有效的,只是数组成员最终不会为NULL (以及邢和蓝精灵提到的一些问题)
void deleteP(FS *fs){
Elem **el; // pointer to pointer, instead of pointer (and no calloc)
el = &(fs->child[20]); // pointer to array member
free(*el); // free the pointer in the array member (not a copy of it),
// though the effect is the same
*el = NULL; // write NULL to the array member, not a copy of it,
// this is what changes the effect to what you want
}
如果我没有输入错误,这或多或少是我们聊天的结果。据我所知,它解决了你的问题。干杯 据我所知,这还应该修复xing在
deleteP(…)
中发现的内存泄漏但也要注意在
内存集(…)
中修复它
这并不能解决BLUEPIXY发现的UB(未定义行为)问题。
为此,您需要修改调试打印,并确保不取消对已释放内存的任何指针的引用(操作顺序问题),也不取消对设置为NULL的任何指针的引用
顺便说一下,这可以在没有局部指针变量的情况下完成;通过参数
fs
执行所有操作。但我将解决方案与您自己的代码保持更接近,以便更好地明确区别。另外,在我看来,通过本地指针进行操作的方式更具可读性。它甚至可能更快,但看到现代编译器有多好,我怀疑它;实际原因是清晰易读。在C语言中,指针可以是空的,但一旦分配了引用,就不能是空的。你能重新措辞吗?我不明白你所说的“将指针指向NULL的引用设置为NULL”是什么意思。或者可以描述一个情况示例,以及在您尝试完成后的结果。@Yunnosch我想设置为NULLfs->child[20]
,它由el
delete(fs)指向;printf(“删除:%s\n”,fs->child[20]->name)代码>和deleteP(fs);printf(“DeleteP:%s\n”,fs->child[20]->name)代码>:这些是UB。让我们来看看。
void deleteP(FS *fs){
Elem **el; // pointer to pointer, instead of pointer (and no calloc)
el = &(fs->child[20]); // pointer to array member
free(*el); // free the pointer in the array member (not a copy of it),
// though the effect is the same
*el = NULL; // write NULL to the array member, not a copy of it,
// this is what changes the effect to what you want
}