Code/C

[C언어] 내장함수 비교 - memset, bzero

코드포휴먼 2020. 10. 6. 08:57

C언어의 함수를 정리하는데 man 가이드를 참고했다.

그런데 man 가이드가 컴퓨터 환경별로 달랐다.

Mac 터미널과 윈도우에서 띄운 Ubuntu 터미널의 내용이 다른 것을 알게 되었다.

따라서 내용을 복합적으로 종합하며 정리해본다.

 

 

memset

PROTOTYPE #include <string.h>
void *memset(void *b, int c, size_t len);
NAME fill a byte string with a byte value
(fill memory with a constant byte)
DESCRIPTION - The memset() function writes len bytes of value c (converted to an unsigned char) to the string b.
RETURN its first argument
CONSIDERATIONS  

 

bzero

PROTOTYPE #include <strings.h>
void bzero(void *s, size_t n);
NAME write zeroes to a byte string
(zero a byte string)
DESCRIPTION - The bzero() function writes n zeroed bytes to the string s.
- The bzero() function erases the data in the n bytes of the memory starting at the location pointed to by s, by writing zeros (bytes containing '\0') to that area.
RETURN None
CONSIDERATIONS - If n is zero, bzero() does nothing.

bzero()는 두 번째 인자로 전달받은 n만큼 zeroed bytes로 바꾸는 함수다.

zeroed bytes는 0을 의미하며, bzero의 경우 *s을 unsigned char 형으로 캐스팅하기 때문에 0대신 '\0'으로 바꿔도 똑같은 의미가 된다.

문자열에서 0과 '\0'은 동일한 NUL 문자이기 때문이다. (자세한 내용은 이 글에서 참조할 수 있다.) 

 

 

memset과 bzero의 공통점

memsetbzero는 모두 unsigned char로 캐스팅하는 과정이 있다는 것이 두 함수의 공통점이다.