释放可能未初始化的结构成员
我有一个像这样的结构,
typedef struct Player {
char *name;
char *heroID;
char *heroName;
int slotNo;
} Player;
然后将其定义为静态分配的数组
Player players[10];
当我尚未完全分配每个 Player
char* 字段时,我的程序可能必须退出> struct in players
并且我决定在退出之前释放所有分配的内存,即使现代操作系统不要求您这样做,因为这样做是很好的编程实践。
但是,我不能只循环 players
和 free(player[i].name)
等,因为它可能未初始化。
解决这个问题的唯一方法是,在定义数组后手动将每个 char 指针初始化为 NULL,然后在释放内存时,检查指针是否为 NULL 来决定是否应该释放它?
如果是这样,当我通过使用大括号声明玩家数组时,初始化、for 循环和手动赋值或定义值的最佳方法是什么。或者还有别的办法吗?
I have a struct like so,
typedef struct Player {
char *name;
char *heroID;
char *heroName;
int slotNo;
} Player;
I then define it as a statically allocated array
Player players[10];
My program may have to exit when I haven't completely allocated all the char*
fields of each Player
struct in players
and I have decided that I will free any allocated memory before exiting even though modern OS's don't require you to because it is good programming practice to do so.
However, I can't just loop through players
and free(player[i].name)
etc because it could be uninitialized.
Is the only way of getting around this problem, manually initializing each char pointer to NULL after I define the array and then when freeing memory, check to see if the pointer is NULL or not to decide whether I should free it?
If so, what is the best way of initializing, for loop and manual assignment or defining values when i declare the players array through the use of braces. Or is there another way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这绝对不是唯一的方法,但它是最常见和标准的方法。事实上,大多数程序员总是将指针初始化为零以防止段错误。
初始化的最佳方法是创建一个
for
循环或memset
所有内容为零(或使用calloc
,这是最简单的)。It's definitely not the only way, but it's the most common and standard way to do so. In fact, most programmers will always initialize pointers to zero to prevent seg faults.
The best way to initialize is to just create a
for
loop ormemset
everything to zero (or usecalloc
, which is the easiest).您可以这样定义数组:
这会将整个数组及其所有成员设置为
0
(这就是NULL
的真正含义)。You can define the array like this:
This will set the whole array and all its members to
0
(which is whatNULL
really is).您可以使用
calloc
创建指向该结构的指针,以便所有字段都将初始化为 0 (NULL
)。将未使用的指针初始化为 NULL 非常重要。如果没有这个,你就是一个段错误的最佳方法
Edit(1)
当然,在你的场景中,你可以在每个结构中使用一些
bool is_used
标志,但你可以使用 NULL 指针可以做得更好。我发现没有理由以其他方式这样做。You can create a pointer to the structure using
calloc
so all the fields will be initialized to 0 (NULL
).Initializing unused pointers to NULL is very important. Without that, you are on a best way to a seg fault
Edit(1)
Of course, in your scenario you can use some
bool is_used
flag inside each struct but you can do it better withNULL
pointers. I find no reason to do it other way around.由于您的数组具有静态存储持续时间,因此它具有隐式初始值设定项。你的代码相当于
所以你可以安全地将这些指针传递给
free
,无论你分配它们时发生了什么。话虽如此,如果对
malloc
的调用失败,并且后续对free
的调用也失败,请不要感到惊讶。一旦发生堆分配失败,中止该进程通常是相当合理的。Since your array has static storage duration it has an implicit initializer. Your code is equivalent to
So you are safe to pass these pointers to
free
no matter what happened when you allocated them.Having said all that, if a call to
malloc
failed don't be surprised if subsequent calls tofree
also fail. Once you have had a heap allocation failure it is often quite reasonable to abort the process.