Aspects


vs


Free

Free is a function to deallocate memory obtained from malloc and other functions in C. Do not use this tag to refer to free software

Malloc

The malloc function performs dynamic memory allocation in C and is part of the standard library

Char p bytes

Quality Example
N more

"Suppose char p malloc n assigns more than n say n bytes of memory are allocated and free p is used to free the memory allocated to p"

from question "What happens when tried to free memory allocated by heap manager, which allocates more than asked for?"

Earlier

"How does free know how much memory to be free d which was earlier allocated by malloc or calloc"

from question "How does free know the size of memory to be freed?"

More

"Now if you free some values on the heap and allocate more with malloc you may be given back some of that dirtied memory that you scribbed on earlier"

from question "Uninitialised values of heap and stack space"

More memory

"One interesting experiment you can try is to try and malloc more memory after you free d that pointer"

from question "C - Accessing data AFTER memory has been free()ed?"

Others

Quality Example
Bigger buffer

"Here is a solution which encapsulates the call to malloc allocates a bigger buffer for alignment purpose and stores the original allocated address just before the aligned buffer for a later call to free"

from question "How to allocate and free aligned memory in C"

Safer

"In addition to the previous answers the strncpy char pointer which seems unsafe for my opinion and the malloc which is safer but you need to remember to free it outside of the function and its inconsistent with the hierarchy of the program you can do the following"

from question "Error trying to change contents of string pointer in C"

Quicker

"In this context free store is different and incompatible with heap because the new delete free store library is simpler and quicker than the malloc free realloc calloc heap library and thus provides huge memory usage gains to the c++ embedded programmer in a context where you have only 512 bytes of ram"

from question "C++, Free-Store vs Heap"

Much easier

"Malloc is much easier to implement if there is no free"

from question "C: how does this code result in the freeing of malloc-ed space?"

More

"If your .so are statically linked to c++ runtimes - you should free objects in the same module where they were allocated since new delete is something more than malloc free and need some extra info to work properly"

from question "Separate Heap on Linux?"

"Malloc finds 10 more free bytes and marks them as used"

from question "What happens when you call malloc on an existing variable?"

"Remember malloc is quite expensive action and free costs even much more than malloc"

from question "Sorting of an array using merge sort"

More memory

"Most likely malloc allocates more memory and puts so-called guard values that happen to contain null bytes or it puts some metadata to be used by free later and this metadata happens to contain a null byte right at that position"

from question "String doesn't end at NULL but still behaves normally, why?"

Bigger

"Presumably the mac malloc aligns to bigger boundaries and so it s spotting the pointer you re passing to free can t be correct since it has the wrong alignment"

from question "Non-aligned pointer being freed on mac"

Back to Home
Data comes from Stack Exchange with CC-BY-SA-3.0