๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

๐Ÿฐ42์„œ์šธ/libft

[42์„œ์šธ] libft ์ •๋ฆฌ: calloc

๐Ÿ“Œcalloc

โœ”๏ธ ํ•จ์ˆ˜ ํ”„๋กœํ† ํƒ€์ž…

void	*calloc(size_t count, size_t size);

โœ”๏ธ ํ•จ์ˆ˜์˜ ์—ญํ•  : size ๋ฐ”์ดํŠธ์˜ ๋ณ€์ˆ˜๋ฅผ count ๊ฐœ ์ €์žฅํ•  ์ˆ˜ ์žˆ๋Š” ๊ณต๊ฐ„์„ ํ• ๋‹นํ•˜๊ณ  0์œผ๋กœ ์ดˆ๊ธฐํ™”

โœ”๏ธํ•จ์ˆ˜ ๋ฐ˜ํ™˜๊ฐ’ 

โœ๏ธ ์„ฑ๊ณต์‹œ, ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ์ธํ„ฐ

โœ๏ธ ์‹คํŒจ์‹œ, NULL

   

โœ”๏ธ man page์— ๋‚˜์˜จ ์„ค๋ช…!

๐Ÿ‘€ Description
The calloc() function contiguously 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.
โžฟํ•ด์„ : calloc() ํ•จ์ˆ˜๋Š” ์—ฐ์†์ ์œผ๋กœ size๋ฐ”์ดํŠธ์ธ count๊ฐœ์˜ object๋ฅผ ์œ„ํ•œ ์ถฉ๋ถ„ํ•œ ๊ณต๊ฐ„์„ ํ• ๋‹นํ•œ๋‹ค. ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ๋Š” 0์˜ ๊ฐ’์„ ๊ฐ€์ง„ byte๋“ค๋กœ ์ฑ„์›Œ์ง„๋‹ค.

๐Ÿ‘€ Return Values
If successful, calloc() function returns a pointer to allocated memory.  If there is an error, they return a NULL pointer and set errno to ENOMEM.
โžฟํ•ด์„ : ๋งŒ์•ฝ ์„ฑ๊ณต์ ์ด๋ผ๋ฉด, calloc() ํ•จ์ˆ˜๋Š” ํ• ๋‹น๋œ ๋ฉ”๋ชจ๋ฆฌ์˜ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๋งŒ์•ฝ ์—๋Ÿฌ๊ฐ€ ์žˆ๋‹ค๋ฉด NULLํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ENOMEM์— errno๋ฅผ ์„ค์ •ํ•œ๋‹ค.

calloc์€ malloc๊ณผ ๋‹ฌ๋ฆฌ 0์œผ๋กœ ์ดˆ๊ธฐํ™”๋ฅผ ํ•ด์คŒ.