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

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

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

๐Ÿ“Œstrdup

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

char	*strdup(const char *s1);

โœ”๏ธ ํ•จ์ˆ˜์˜ ์—ญํ•  : s1์„ ๋ณต์‚ฌํ•  ๋ฉ”๋ชจ๋ฆฌ์— ํ• ๋‹น ํ›„ ๊ทธ ๋ฉ”๋ชจ๋ฆฌ์— ๋ณต์‚ฌ

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

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

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

   

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

๐Ÿ‘€ Description
The strdup() function allocates sufficient memory for a copy of the string s1, does the copy, and returns a pointer to it. The pointer may subsequently be used as an argument to the function free(3).
If insufficient memory is available, NULL is returned and errno is set to ENOMEM.

โžฟํ•ด์„ : strudup() ํ•จ์ˆ˜๋Š” string s1์„ ๋ณต์‚ฌํ•  ์ถฉ๋ถ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํ• ๋‹นํ•˜๊ณ , ๋ณต์‚ฌํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ทธ ํฌ์ธํ„ฐ๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. ๊ทธ ํฌ์ธํ„ฐ๋Š” ์•„๋งˆ ๊ทธ ํ›„ free()ํ•จ์ˆ˜์˜ argument๋กœ ์“ฐ์ผ ๊ฒƒ์ด๋‹ค. ๋งŒ์•ฝ ์ถฉ๋ถ„ํ•œ ๋ฉ”๋ชจ๋ฆฌ๊ฐ€ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜์ง€ ์•Š๋‹ค๋ฉด, NULL์ด ๋ฐ˜ํ™˜๋˜๊ณ  errno๊ฐ€ ENOMEM์— ์„ค์ •๋œ๋‹ค.