在尝试 free() 之前确定结构成员是否具有有效数据

发布于 2024-12-21 16:39:49 字数 666 浏览 4 评论 0原文

我使用下面的代码来释放 meshes 结构中 malloced 的内存,其中包含 triangleArraysfaces

这会崩溃,因为并非 struct 中的每个位置都有数据。我想要做的是仅当 struct 包含数组该成员的数据时才调用 free 。但是,使用 if (self.meshes[meshIdx].triangleArrays[triangleArrayIdx].faces !=NULL) 似乎不起作用。

for (int meshIdx = 0; meshIdx <=meshTriangleArrays; meshIdx ++) {
    for (int triangleArrayIdx = 0; triangleArrayIdx <=1; triangleArrayIdx ++) {
        if (self.meshes[meshIdx].triangleArrays[triangleArrayIdx].faces !=NULL) {
            free(self.meshes[meshIdx].triangleArrays[triangleArrayIdx].faces);
        }
    }
}

I am using the code below to free up malloced memory in the meshes struct, which contains triangleArrays and faces.

This crashes because not every position in the struct has data. What I want to do is only call free if the struct contains data at that member of the array. However using if (self.meshes[meshIdx].triangleArrays[triangleArrayIdx].faces !=NULL) does not seem to work.

for (int meshIdx = 0; meshIdx <=meshTriangleArrays; meshIdx ++) {
    for (int triangleArrayIdx = 0; triangleArrayIdx <=1; triangleArrayIdx ++) {
        if (self.meshes[meshIdx].triangleArrays[triangleArrayIdx].faces !=NULL) {
            free(self.meshes[meshIdx].triangleArrays[triangleArrayIdx].faces);
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

嘦怹 2024-12-28 16:39:49

在空指针上调用free实际上是没问题的。

您没有提供足够的代码来完全诊断此问题,但需要注意以下几点:

  • 您需要确保 self.meshes[...].triangleArrays[...].faces 总是通过调用 malloc (或其他)或将其设置为 NULL 来初始化。否则,它可能(并且很可能)是一个您无权释放的随机垃圾指针。
  • 您需要确保所有不同的 self.meshes[...].triangleArrays[...].faces 指针都是不同的指针。您只能在 malloc 指针上调用 free 一次。例如,像这样:
    int * p = (int *) malloc(sizeof(int));
    免费(p);
    免费(p); // 未定义的行为
    
    可能会导致崩溃。

Calling free on a null pointer is actually fine.

You haven't given enough code to fully diagnose this problem, but a few things to look at:

  • You need to make sure that self.meshes[...].triangleArrays[...].faces is always initialized, either by a call to malloc (or whatnot), or by setting it to NULL. Otherwise it can (and likely will) be a random garbage pointer that you don't have permission to free.
  • You need to make sure that all the different self.meshes[...].triangleArrays[...].faces pointers are distinct pointers. You are only allowed to call free exactly once on a malloc'd pointer. For example, something like this:
    int * p = (int *) malloc(sizeof(int));
    free(p);
    free(p); // undefined behavior
    

    can cause a crash.

旧城烟雨 2024-12-28 16:39:49

下面的代码崩溃,因为并非结构中的每个位置都有数据。

不会,它不会因为向 free() 传递 NULL 指针而崩溃。如果您传入 NULL 指针,则不会发生任何情况,请参阅文档。

什么错误正在被抛出?也向我们展示您的初始化代码,即您如何分配faces及其上面的所有内容?您可能会向 free() 传递一些错误/未初始化的数据。

顺便说一句,由于您提出这个问题的方式,我相信您认为简单地声明一个数组就会用 NULL 填充每个元素。事实并非如此,它们可能充满了任何东西,如果你将其传递给free,你就会崩溃(如果你幸运的话)。

The below code crashes because not every position in the struct has data.

No, it doesn't crash due to passing a NULL pointer to free(). If you pass in a NULL pointer nothing happens, see the documentation.

What error is being thrown? Show us your initialization code as well, i.e., how are you allocating faces and everything above it? You are likely passing in some bad/uninitialized data to free().

BTW, due to the way you have asked this question I am lead to believe that you think simply declaring an array will fill every element with NULL. This is not the case, they may be filled with anything, and if you pass that to free you will crash (if you're lucky).

罗罗贝儿 2024-12-28 16:39:49

首先,triangleArrays 数组是如何创建的?未分配的成员是否可能包含垃圾而不是 NULL?

How was the triangleArrays array created in the first place? Is it possible that the non-allocated members contain garbage instead of NULL?

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