✅calloc
size
크기의 변수를 count
개 만큼 저장할 수 있는 메모리 공간을 할당.
- 할당된 메모리 공간을
0
으로 초기화
💾함수 원형
void *calloc(size_t count, size_t size);
💾함수 구현
💻VER 1.
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
void *tmp;
tmp = malloc(size * count);
if (tmp == 0)
return (0);
ft_memset(ptr, 0, size * count)
return (tmp);
}
💻VER 2.
#include "libft.h"
void *ft_calloc(size_t count, size_t size)
{
void *tmp;
if (!(tmp = malloc(count * size)))
return (NULL);
ft_bzero(tmp, (count * size));
return (tmp);
}
💡malloc
과 free
malloc
: 메모리를 동적으로 할당하는 함수(heap 영역)
free
: heap 영역에 할당된 메모리를 해제하는 함수