C/C++ 라이브러리 함수 void free(void *ptr) calloc, malloc 또는 realloc에 대한 호출에 의해 이전에 할당된 메모리를 할당 해제합니다. 다음은 free() 함수에 대한 선언입니다.
void free(void *ptr)
이 함수는 포인터 ptr을 취합니다. 이것은 할당 해제될 malloc, calloc 또는 realloc으로 이전에 할당된 메모리 블록에 대한 포인터입니다. null 포인터가 인수로 전달되면 작업이 발생하지 않습니다.
예시
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main () {
char *str;
/* Initial memory allocation */
str = (char *) malloc(15);
strcpy(str, "tutorialspoint");
cout << "String = "<< str <<", Address = "<< &str << endl;
/* Reallocating memory */
str = (char *) realloc(str, 25);
strcat(str, ".com");
cout << "String = "<< str <<", Address = "<< &str << endl;
/* Deallocate allocated memory */
free(str);
return(0);
} 출력
String = tutorialspoint, Address = 0x22fe38 String = tutorialspoint.com, Address = 0x22fe38