释放可能未初始化的结构成员

发布于 2024-12-28 17:54:24 字数 604 浏览 3 评论 0原文

我有一个像这样的结构,

typedef struct Player {
    char *name;
    char *heroID;
    char *heroName;
    int slotNo;
} Player;

然后将其定义为静态分配的数组

Player players[10];

当我尚未完全分配每个 Playerchar* 字段时,我的程序可能必须退出> struct in players 并且我决定在退出之前释放所有分配的内存,即使现代操作系统不要求您这样做,因为这样做是很好的编程实践。

但是,我不能只循环 playersfree(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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

老街孤人 2025-01-04 17:54:24

解决这个问题的唯一方法是,在定义数组后手动将每个字符指针初始化为 NULL,然后在释放内存时,检查指针是否为 NULL 来决定是否应该释放它?< /p>

这绝对不是唯一的方法,但它是最常见和标准的方法。事实上,大多数程序员总是将指针初始化为零以防止段错误。

初始化的最佳方法是创建一个 for 循环或 memset 所有内容为零(或使用 calloc,这是最简单的)。

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?

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 or memset everything to zero (or use calloc, which is the easiest).

醉城メ夜风 2025-01-04 17:54:24

您可以这样定义数组:

Player players[10] = { 0 };

这会将整个数组及其所有成员设置为 0(这就是 NULL 的真正含义)。

You can define the array like this:

Player players[10] = { 0 };

This will set the whole array and all its members to 0 (which is what NULL really is).

他不在意 2025-01-04 17:54:24

您可以使用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 with NULL pointers. I find no reason to do it other way around.

—━☆沉默づ 2025-01-04 17:54:24

由于您的数组具有静态存储持续时间,因此它具有隐式初始值设定项。你的代码相当于

Player players[10] = { 0 };

所以你可以安全地将这些指针传递给free,无论你分配它们时发生了什么。

话虽如此,如果对 malloc 的调用失败,并且后续对 free 的调用也失败,请不要感到惊讶。一旦发生堆分配失败,中止该进程通常是相当合理的。

Since your array has static storage duration it has an implicit initializer. Your code is equivalent to

Player players[10] = { 0 };

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 to free also fail. Once you have had a heap allocation failure it is often quite reasonable to abort the process.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文