What is "malloc"
malloc
is a C function to obtain a memory block reserved in the heap. The only parameter indicated is the desired amount of bytes. The most direct way to obtain this amount is through the function sizeof
which allows to evaluate the size of any type in bytes.
The type of return of malloc
is void *
, ie a generic pointer, and pointing to the first byte of the allocated memory block.
A typical example of one of these allocations would be:
int *p1 = malloc(4 * sizeof(int));
After obtaining a certain memory block, it becomes the responsibility of the programmer to release it when they no longer need it, through the function free
.
Documentation: