valgrind 正确吗?记忆丢失了吗?
typedef struct Model
{
int recordId;
char *name;
}Model;
typedef struct ModelArray
{
//keeps the size that the array was initially create with. When more elements are needed
//we use this to add that many more elements
int originalSize;
//total number of elements that can be used
int elements;
//total number of elements used
int count;
//the actual array is stored here
Model *source;
}ModelArray;
void initModelArray(ModelArray *array, int numberOfElements)
{
array->originalSize = numberOfElements;
array->elements = numberOfElements;
array->count = 0;
array->source = malloc(sizeof(Model)*numberOfElements);//0 bytes in 3 blocks are definitely lost in loss record 1 of 65
}
void deallocModelArray(ModelArray *array)
{
if(array == NULL)
return;
array->elements = 0;
array->count = 0;
free(array->source);
array->source = NULL;
free(array);
}
main(int argc, const char * argv[])
{
ModelArray *models = malloc(sizeof(ModelArray));
initModelArray(models, 10);
deallocModelArray(models);
}
失去了什么?代码对我来说看起来不错。我确信我可以先说 array->source = NULL 但不需要,对吗?
typedef struct Model
{
int recordId;
char *name;
}Model;
typedef struct ModelArray
{
//keeps the size that the array was initially create with. When more elements are needed
//we use this to add that many more elements
int originalSize;
//total number of elements that can be used
int elements;
//total number of elements used
int count;
//the actual array is stored here
Model *source;
}ModelArray;
void initModelArray(ModelArray *array, int numberOfElements)
{
array->originalSize = numberOfElements;
array->elements = numberOfElements;
array->count = 0;
array->source = malloc(sizeof(Model)*numberOfElements);//0 bytes in 3 blocks are definitely lost in loss record 1 of 65
}
void deallocModelArray(ModelArray *array)
{
if(array == NULL)
return;
array->elements = 0;
array->count = 0;
free(array->source);
array->source = NULL;
free(array);
}
main(int argc, const char * argv[])
{
ModelArray *models = malloc(sizeof(ModelArray));
initModelArray(models, 10);
deallocModelArray(models);
}
What is lost? Code looks fine to me. I'm sure I could say array->source = NULL first but it's not needed, right?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
要正确地释放这些结构,您需要按顺序执行以下操作:
如果您执行其他操作,则会泄漏内存。
编辑:
好的,看到 Model 结构后,您可能泄漏了名称,或者至少 valgrind 认为您这样做,因为您释放了 ModelArray 结构,该结构包含指向 Model 结构的指针,该结构包含您不知道的 char*先免费。
所以:
在首先分配 models->source 时,最好使用 calloc() 而不是 malloc()。这会将所有 name 指针设置为 0。如果没有这个,如果 name 碰巧包含一些垃圾,上面对 models->source[i]->name 为非 NULL 的测试可能会失败(因为使用未初始化的内存会产生未定义的行为.)
To deallocate these structures correctly, you need to do the following, in this order:
If you do anything else, you're leaking memory.
Edit:
OK, having seen the Model struct, you're probably leaking the names, or at least valgrind thinks you do because you deallocate the ModelArray structure, which contains a pointer to a Model structure, which contains a char* which you don't free first.
So:
And it would be a good idea to use calloc() instead of malloc() when allocating models->source in the first place. This will set all the name pointers to 0. Without this, the test for models->source[i]->name being non-NULL above might fail if name happens to contain some garbage (since using uninitialized memory produces undefined behavior.)
呃……是的,记忆丢失了。当然,它会丢失,因为您“遗漏了dealloc代码”!
当您“遗漏了释放代码”时,您怎么可能期望有人回答您的问题?您问题的本质是您的 dealloc 代码是否正确。然后你决定把它排除在外?
最重要的是,你的代码中有很多没有意义的东西。 应该是什么
意思?为什么将
struct ModelArray
类型定义为Model
?事实上,您的代码甚至无法编译,因为Model *
在结构内部使用,但尚未声明。您还在代码中使用ModelArray
类型,但实际上没有这种类型。您有struct ModelArray
,但不仅仅是ModelArray
。您发布的代码不是真正的代码。请发布真实代码。 (显然它应该是typedef struct ModelArray { ... } ModelArray;
,其中Model
在别处定义。)最后,作为一个不相关的注释,
//< /code> 注释是 C99 的一项功能。在 C99 中,函数返回类型不能省略(C99 中没有“隐式 int”规则),这意味着您必须将
main
函数声明为int main
。Er... Yes, the memory is lost. Of course, it is lost, since you "left out dealloc code"!
How could you possibly expect anyone to answer your question when you "left out dealloc code"? The very essence of your question is whether your dealloc code is correct or not. And you decided to leave it out?
On top of that, there quite a few thing that make little sense in your code. What is
supposed to mean? Why are you typedefing
struct ModelArray
asModel
? In fact, your code will not even compile, sinceModel *
is used inside the struct, where it is not declared yet. You also useModelArray
type in your code, while in reality there's no such type. You havestruct ModelArray
, but not justModelArray
. The code you posted is not real code. Post real code, please. (Apparently it was supposed to betypedef struct ModelArray { ... } ModelArray;
withModel
defined elsewhere.)Finally, as an unrelated note,
//
comments is a C99 feature. In C99 the function return type cannot be omitted (no "implicit int" rule in C99), meaning that you have to declare yourmain
function asint main
.