How to free memory assigned using malloc?

Memory is not wiped when you free it - that would be a waste of processor time. It is just allocated to the "free list". That is why your data is still there.

Memory is not wiped when you free it - that would be a waste of processor time. It is just allocated to the "free list". That is why your data is still there Whenever you free a block, you should set the corresponding pointer to NULL, so you don't accidentally reference it - it could be reused at any time.

The behaviour of malloc() and free() depends heavily on the operating system and C library that you are using. In most implementations there are actually two memory allocators in play: The OS memory allocator, which uses the virtual memory facilities of the processor to provide a process with its own address space and maps physical memory pages into that address space for use. The C library memory allocator, which is in fact part of the application code and uses the pages provided by the OS to provide fine-grained memory management facilities, as provided by malloc() and free().

In general, calling free() does one or more of the following: It marks the pointed-to memory area as free in the C memory allocator. This allows that memory to be reused. Free() does not zero-out freed URL1 may return memory to the OS, depending on the settings of the C memory allocator and whether it is actually possible to free that part of the heap.

If memory is not returned to the OS, it can be reused by future malloc() calls by the same application. If you try to access memory that has been freed, usually one of three things will happen: The memory has been returned to the OS, and your program, typically, crashes. If you ask me, that is probably the best scenario - you have a problem, sure, but you know it.

The memory has not been reused, therefore your old data is still there. Your program goes on as if nothing was wrong. This is in my opinion the worst case scenario.

Your code appears to work correctly and, if Murphy has a say in this, it will continue to do so until it reaches your end users - then it will fail spectacularly. The memory has been reused by your program, and your code will start messing around with its own data. If you are careful (and lucky?

), you will probably notice that the results are off. If not, well... If you are on Linux/Unix Valgrind is a good tool to catch memory management problems like this. There are also replacement libraries for the C memory allocator, such as DUMA that will also allow you to detect such issues.

Actually free doesn't delete anything, it just tells the OS it can use that memory again, for example next time you call malloc() it could overwrite some of your nodes.

I am not sure the memory is reclaimed by the OS. More like it stays on the program. – Muggen Mar 22 '11 at 13:40.

Freeing the memory releases it for reuse. It doesn't necessarily destroy the data that was at that location. It is up to you not to access a memory region that has been released because the behavior is undefined (i.e.

Usually very bad). If you want the data destroyed for some strange reason, then overwrite the memory area prior to freeing it (e.g. Memset(buf, 0, len)).

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions