ft_calloc
Subject
CALLOC(3) (simplified)
NAME
calloc -- memory allocation
SYNOPSIS
void *calloc(size_t count, size_t size);
DESCRIPTION
The calloc() function allocates memory.
The allocated memory is aligned such that it can be used for any data type.
The calloc() function contigously allocates enough space for count objects that are size bytes of memory each and returns a pointer to the allocated memory.
The allocated memory is filled with bytes of value zero.
RETURN VALUES
If successful, calloc() returns a pointer to allocated memory. If there is an error, they return a NULL pointer and set errno to ENOMEM.
Understandable explanation
By now you should have understand what the malloc()
function does, at least I hope. Otherwise, understand how malloc()
works and come back here.
I will mainly base my explanation on comparing calloc()
to malloc()
.
calloc()
works in the same way as malloc()
does, but the difference is that calloc()
sets all the memory bytes are set to 0
instead of staying as the gibberish that was there in memory before we allocated it.
Hints
void *ft_calloc(size_t count, size_t size)
{
/* declare a tmp unsigned char pointer */
/* use malloc to allocate count * size in tmp */
/* loop over tmp and set each byte to 0 */
/* return tmp */
}
Commented solution
Last updated
Was this helpful?