mallocmalloc() and() andfree()free()
void *void *malloc(size_t size);malloc(size_t size);
void free(void free(void *void *ptr);ptr);
mallocmallocrequires one argument requires one argument --the number of the number of
bytes you want to allocate dynamically.bytes you want to allocate dynamically.
If the memory allocation wasIf the memory allocation wassuccessful,successful,mallocmallocwill return awill return avoidvoidpointerpointer --youyou
can assign this to a pointer variable, which willcan assign this to a pointer variable, which willstore the address of the allocated memory.store the address of the allocated memory.
If memory allocation failed (for example, if If memory allocation failed (for example, if you're out of memory),you're out of memory),mallocmallocwill return a NULLwill return a NULL
Passing the pointer intoPassing the pointer intofreefreewill release thewill release the
allocated memoryallocated memory --it is good practice to freeit is good practice to free
memory when you've finished with it.memory when you've finished with it.