在尝试 free() 之前确定结构成员是否具有有效数据
我使用下面的代码来释放 meshes
结构中 malloc
ed 的内存,其中包含 triangleArrays
和 faces
。
这会崩溃,因为并非 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 malloc
ed 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
在空指针上调用
free
实际上是没问题的。您没有提供足够的代码来完全诊断此问题,但需要注意以下几点:
self.meshes[...].triangleArrays[...].faces
总是通过调用malloc
(或其他)或将其设置为NULL
来初始化。否则,它可能(并且很可能)是一个您无权释放的随机垃圾指针。malloc
指针上调用free
一次。例如,像这样: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:
self.meshes[...].triangleArrays[...].faces
is always initialized, either by a call tomalloc
(or whatnot), or by setting it toNULL
. Otherwise it can (and likely will) be a random garbage pointer that you don't have permission tofree
.self.meshes[...].triangleArrays[...].faces
pointers are distinct pointers. You are only allowed to callfree
exactly once on amalloc
'd pointer. For example, something like this:can cause a crash.
不会,它不会因为向
free()
传递 NULL 指针而崩溃。如果您传入 NULL 指针,则不会发生任何情况,请参阅文档。什么错误正在被抛出?也向我们展示您的初始化代码,即您如何分配
faces
及其上面的所有内容?您可能会向free()
传递一些错误/未初始化的数据。顺便说一句,由于您提出这个问题的方式,我相信您认为简单地声明一个数组就会用
NULL
填充每个元素。事实并非如此,它们可能充满了任何东西,如果你将其传递给free
,你就会崩溃(如果你幸运的话)。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 tofree()
.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 tofree
you will crash (if you're lucky).首先,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?