简体   繁体   English

如果我们释放已经免费或没有malloc的内存,会发生什么?

[英]What happen if we free memory which are already free or not malloc?

When I free the malloc ed memory which are not allocated using malloc than valgraind tool gives me error like: 当我释放没有使用malloc分配的malloc ed内存比valgraind工具给我错误时:

Invalid free() / delete / delete[]

Is it dangerous in embedded C programming? 嵌入式C编程有危险吗?

In code whenever malloc fails than i cannot judge which malloc ed pointer i have to release. 在代码中,每当malloc失败时,我都无法判断我必须释放哪个malloc ed指针。

This will lead to undefined behavior, most likely heap corruption and/or crashing your program. 这将导致未定义的行为,很可能是堆损坏和/或崩溃您的程序。 It's dangerous in any kind of system. 它在任何类型的系统中都很危险。

Your program could crash (as everyone replied), and even worse, it could crash much latter than when you have incorrectly called malloc . 你的程序可能崩溃(正如所有人都回复的那样),更糟糕的是,它可能会比你错误地调用malloc时崩溃得多。 So finding these kind of bugs is hard. 所以找到这些错误很难。

A possible solution, assuming you have access to all your source code and can change it (ie assuming that you don't use third parties libraries which you cannot alter) might be to use Boehm conservative garbage collector , by replacing every occurrence of malloc , calloc etc ... with GC_MALLOC or GC_CALLOC after having #include d <gc/gc.h> (and then, you won't bother calling free anymore). 一个可能的解决方案,假设您可以访问所有源代码并且可以更改它(即假设您不使用您无法更改的第三方库)可能是使用Boehm保守垃圾收集器 ,通过替换每次出现的malloccalloc等等与GC_MALLOCGC_CALLOC有后#include d <gc/gc.h>然后,你不会打扰呼吁free了)。

Boehm's garbage collector being conservative, it does have a small probability to leak (because it handles every word on your call stack as a possible pointer, even if that word in fact don't carry a dereferencable pointer, but eg an integer or a float). Boehm的垃圾收集器是保守的,它泄漏的可能性很小(因为它将调用堆栈中的每个字都作为可能的指针处理,即使该字实际上不带有可解除引用的指针,但是例如整数或浮点数)。

And you did not explain what embedded C programming means for you. 而且您没有解释嵌入式C编程对您来说意味着什么。 In some industries and contexts, critical embedded C code (like the one making your aircraft flying) is not even allowed to call malloc or to use dynamically allocated heap data. 在某些行业和环境中,关键的嵌入式C代码(如使您的飞机飞行的代码)甚至不允许调用malloc或使用动态分配的堆数据。

如果你在没有用malloccallocrealloc分配的内存上调用free ,或者你在已经释放的内存上调用它,你将得到未定义的行为。

Yes, it is dangerous for embedded programming. 是的,它对嵌入式编程很危险。 It is almost always crash you software. 它几乎总是让你的软件崩溃。

You will get undefined behaviour, typically memory corruption by freeing memory that hasn't been allocated. 您将通过释放尚未分配的内存来获取未定义的行为,通常是内存损坏。

I don't see how you can say that you don't know which pointer result from a malloc operation has failed? 我不知道你怎么能说你不知道malloc操作的哪个指针结果失败了?

It's really quite simple. 这真的很简单。

Declare your pointers and set them to null. 声明你的指针并将它们设置为null。

Use malloc to allocate your memory. 使用malloc分配内存。 Only use malloc on pointers that are already null (to prevent memory leaks due to reassigning a pointer to a memory block to another one). 仅对已经为空的指针使用malloc(以防止由于将指向内存块的指针重新分配给另一个内存而导致的内存泄漏)。 If the malloc fails then the pointer will remain null. 如果malloc失败,则指针将保持为null。

When it comes to freeing memory allocated by a malloc operation, only perform the free operation if the pointer is not null and once freed set that pointer to null. 当释放由malloc操作分配的内存时,如果指针不为null,则仅执行自由操作,并且一旦释放,则将该指针设置为null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM