C-standard-library-c-function-free

提供:Dev Guides
移動先:案内検索

Cライブラリ関数-free()

説明

Cライブラリ関数 void free(void ptr)*は、calloc、malloc、またはreallocの呼び出しによって以前に割り当てられたメモリの割り当てを解除します。

宣言

以下はfree()関数の宣言です。

void free(void *ptr)

パラメーター

  • ptr -これは、malloc、callocまたはreallocで以前に割り当て解除されたメモリブロックへのポインタです。 NULLポインターが引数として渡された場合、アクションは発生しません。

戻り値

この関数は値を返しません。

次の例は、free()関数の使用法を示しています。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main () {
   char *str;

  /*Initial memory allocation*/
   str = (char *) malloc(15);
   strcpy(str, "finddevguides");
   printf("String = %s,  Address = %u\n", str, str);

  /*Reallocating memory*/
   str = (char *) realloc(str, 25);
   strcat(str, ".com");
   printf("String = %s,  Address = %u\n", str, str);

  /*Deallocate allocated memory*/
   free(str);

   return(0);
}

次の結果を生成する上記のプログラムをコンパイルして実行しましょう-

String = finddevguides, Address = 355090448
String = finddevguides.com, Address = 355090448