【发布时间】:2011-05-16 13:25:43
【问题描述】:
我的项目中有一个 c 函数,它创建一个结构并返回指针。
typedef struct object
{
float var1;
float var2;
}
Object;
Object *createObject(float newVar1, float newVar2)
{
Object *object; //create new structure
object = (Object*)malloc(sizeof(Object)); //malloc size for struct object
if(object != NULL) //is memory is malloc'd
{
object->var1 = newVar1; //set the data for var1
object->var2 = newVar2; //set the data for var2
return object; //return the pointer to the struct
}
return NULL; //if malloc fails, return NULL
}
现在这个结构被使用了,过了一会儿我想删除这个结构,我做了这个函数:
void deleteMarnix(Object *objectPointer)
{
free(objectPointer); //free the memory the pointer is pointing to
objectPointer = NULL; //stop it from becomming a dangling pointer
}
最后一段代码 sn-p 显示了我如何制作、使用它并尝试删除它,但是,它似乎并没有完全释放内存。我做错了什么?
Object *object = createObject(21.0f, 1.87f);
//do things here with object.
deleteMarnix(object);
【问题讨论】:
-
您需要表明您如何知道它正在泄漏,您可能只是看到 CRT 的预分配。
-
“似乎没有完全释放内存”是什么意思?
标签: c memory-management struct malloc free