본문 바로가기

Code/C11

[C언어] 내장함수 비교 - strdup, malloc, calloc, free 에 정의된 strdup 함수와, 에 정의되어 있는 동적 메모리(dynamic memory) 할당 및 해제 함수 몇 가지를 정리한다. 리퍼런스는 man 가이드다. strdup strdup 함수는 동적 메모리를 할당하여 문자열을 복사하므로 malloc 함수를 사용한다. PROTOTYPE #include void *strdup(const char *s1); NAME duplicate a string (save a copy of a string) 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.. 2020. 10. 7.
[C언어] NULL, 0, NUL, '\0', undefined의 차이 malloc(), bzero() 등의 C언어 내장함수를 공부하면서 NULL, 0, NUL, '\0'을 접하게 된다. 공통점과 차이점을 정확하게 정리하고자 한다. 우선 NULL과 NUL은 구분되며, 비교항목을 NULL, 0 과 NUL, '\0' 과 undefined 로 나눌 수 있다. NULL, 0 비교 NULL은 널 포인터로서, (void *)0을 가리킨다. NULL 매크로는 , 등 여러 헤더파일에서 이미 정의돼있다. C언어에서 pointer와 정수는 서로 형 변환될 수 있는데, 두 데이터 타입 모두 4byte다. Null pointer는 정수 0으로 변환될 수 있고, 정수 0은 Null pointer로 변환될 수 있다. 즉, Null pointer는 메모리 주소 0을 가르키는 pointer라는 뜻이다... 2020. 10. 6.
[C언어] 내장함수 비교 - memset, bzero C언어의 함수를 정리하는데 man 가이드를 참고했다. 그런데 man 가이드가 컴퓨터 환경별로 달랐다. Mac 터미널과 윈도우에서 띄운 Ubuntu 터미널의 내용이 다른 것을 알게 되었다. 따라서 내용을 복합적으로 종합하며 정리해본다. memset PROTOTYPE #include 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 it.. 2020. 10. 6.
[C언어] 내장함수 비교 - strlcpy, strlcat 앞서 의 strcpy, strncpy, strlcpy의 차이를 정리했다. 그 중 strlcpy는 strlcat과 유사하다. 실제로 man 가이드는 이 둘을 같이 안내한다. 간단히 차이점을 정리해본다. strlcpy PROTOTYPE #include size_t strlcpy(char *restrict dst, const char *restrict src, size_t dstsize); DESCRIPTION - strlcpy() takes the full size of the destination buffer - strlcpy() copies up to dstsize - 1 characters from the string src to dst, NUL-terminating the result if dstsi.. 2020. 10. 1.
[C언어] 내장함수 비교 - strcpy, strncpy, strlcpy C언어의 에 있는 str 관련 함수들은 공부해도 항상 헷갈린다. 필요한 순간마다 블로깅하여 확실하게 정리한다. man 가이드의 원문을 활용했다. cpy strcpy PROTOTYPE #include char *strcpy(char *dst, const char *src); DESCRIPTION - The strcpy() function copies the string src to dst (including the terminating `\0' character.) RETURN dst SECURITY CONSIDERATIONS - The strcpy() function copies the string src to dst (including the terminating `\0' character.) The .. 2020. 10. 1.