MALLOC() / FREE()
these functions gives developers full control of memory requirements by the program. these are typically used when the size of the data to be processed is not known in advanced.
MALLOC
This function requests and allocates a memory block of a specified size dynamically.
void *malloc(int size);
* its only parameter provides information about the size of the requested memory and
is expressed in bytes;
* the function returns a pointer of type void * which points to the newly allocated
memory block, or is equal to NULL to indicate that the allocation requested could
not be granted;
* the function doesn’t have a clue as to what we want to use the memory for and
therefore the result is of type void *; we’ll have to convert it to another
usable pointer type;
* the allocated memory area is not filled (initiated) in any way, so you should
expect it to contain garbage.
FREE
Deallocate memory and returns it to the OS. This is required IOT prevent memory leaks.
void free(void *pointer);
* the function name doesn't require any comments;
* the function does not return any results so its type is defined as void;
* the function expects one parameter – the pointer to the memory block that is to be
released; usually it’s a pointer previously received from the malloc or its
kindred; using another pointer value may cause some kind of disaster;
* the function doesn't need to know the size of the freed block; you can only release
the entire allocated block, not a part of it;
* after performing the free function, all the pointers that point to the data inside
the freed area become illegal; attempting to use them may result in abnormal
program termination.
Last updated