C50. C Memory Allocation Functions


Memory Allocation Functions
To allocate storage dynamically, we'll need to call one of the three memory alloca­tion functions declared in the header.
malloc-->Allocates a block of memory but doesn't initialize it.
calloc-->Allocates a block of memory and clears it.
realloc-->Resizes a previously allocated block of memory.

Of the three, malloc is the most used. It's more efficient than calloc, since it doesn't have to clear the memory block that it allocates.
When we call a memory allocation function to request a block of memory, the function has no idea what type of data we're planning to store in the block, so it can't return a pointer to an ordinary type such as int or char. Instead, the func­tion returns a value of type void *. A void * value is a "generic" pointer essentially, just a memory address.

No comments: