본문 바로가기
Code/C

[C언어] 내장함수 비교 - strdup, malloc, calloc, free

by 코드포휴먼 2020. 10. 7.

<string.h>에 정의된 strdup 함수와, 

<stdlib.h>에 정의되어 있는 동적 메모리(dynamic memory) 할당 및 해제 함수 몇 가지를 정리한다.

리퍼런스는 man 가이드다.

 

 

strdup

strdup 함수는 동적 메모리를 할당하여 문자열을 복사하므로 malloc 함수를 사용한다.

PROTOTYPE #include <string.h>
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 may subsequently be used as an argument to the function void free(void *ptr). 
RETURN a pointer to a new copied string
CONSIDERATIONS If insufficient memory is available, NULL is returned and errno is set to ENOMEM.

 

 

malloc

힙(Heap) 메모리 영역에 동적 메모리를 할당하는 함수다.

할당한 공간에는 쓰레기값이 들어간다.

PROTOTYPE #include <stdlib.h>
void *malloc(size_t size);
NAME allocate dynamic memory
DESCRIPTION - The  malloc() function allocates size bytes and returns a pointer to the allocated memory.  
- The allocated memory is aligned such that it can be used for any data type, including AltiVec- and SSE-related types.
- The memory is not initialized.  
RETURN - a pointer to the allocated memory, which is suitably aligned for any built-in type
- On error, it returns NULL and sets errno to ENOMEM.
CONSIDERATIONS If size is 0, then malloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().

 

 

calloc

calloc은 malloc과 비교하면 사용법과 메모리 초기화 여부가 다르다.

할당한 공간에는 0이 초기화 되어 들어간다.

CONSIDERATIONS 사항에 있는 nmemb는 number of members를 의미하는데, 함수 프로토타입에서 받는 첫 번째 인자 count와 같은 뜻이다. 

PROTOTYPE #include <stdlib.h>
void *calloc(size_t count, size_t size);
NAME allocate dynamic memory
DESCRIPTION - The calloc() function allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the  allocated  memory.   
- The allocated memory is aligned such that it can be used for any data type, including AltiVec- and SSE-related types. 
- The  memory is set to zero. The allocated memory is filled with bytes of value zero.
RETURN - a pointer to the allocated memory, which is suitably aligned for any built-in type
- On error, it returns NULL and sets errno to ENOMEM.
CONSIDERATIONS - If nmemb(number of members) or size is 0, then calloc() returns either NULL, or a unique pointer value that can later be successfully passed to free().  
- If the multiplication of nmemb and size would result in integer overflow, then calloc() returns an error. 
- By contrast, an integer overflow would not be detected in the following call to malloc(), with the result that an incorrectly sized block of memory would be allocated: malloc(nmemb * size);

 

 

free

할당했던 동적 메모리를 해제하는 함수다.

PROTOTYPE #include <stdlib.h>
void free(void *ptr);
NAME free dynamic memory
DESCRIPTION - The free() function frees the memory space pointed to by ptr, which must have been returned by a previous call to malloc(),  calloc(), or  realloc().
RETURN No value
CONSIDERATIONS - If free(ptr) has already been called before, undefined behavior occurs.
- If ptr is NULL, no operation is performed.

 

댓글