ft_calloc

one·2021년 1월 15일
0

✅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)	//메모리가 부족한 경우에 대해서 NULL이 반환되는지 체크.
		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);
}

💡mallocfree

  • malloc : 메모리를 동적으로 할당하는 함수(heap 영역)
  • free : heap 영역에 할당된 메모리를 해제하는 함수
profile
늘 호기심을 갖고, 새로운 것에 도전할 것.

0개의 댓글