Latest web development tutorials

C library functions - free ()

C standard library - <stdlib.h> C standard library - <stdlib.h>

description

Call calloc, malloc or realloc allocated memory space before C library functionsvoid free (void * ptr) release.

statement

Here is the free () function's declaration.

void free(void *ptr)

parameter

  • ptr - pointer to a block of memory to free up memory, before the memory block is allocated memory by calling malloc, calloc or realloc of.If the parameter passed is a null pointer, then no action is taken.

return value

This function does not return a value.

Examples

The following example demonstrates the free () function is used.

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

int main()
{
   char *str;

   /* 最初的内存分配 */
   str = (char *) malloc(15);
   strcpy(str, "w3cschool");
   printf("String = %s,  Address = %u\n", str, str);

   /* 重新分配内存 */
   str = (char *) realloc(str, 25);
   strcat(str, ".cc");
   printf("String = %s,  Address = %u\n", str, str);

   /* 释放已分配的内存 */
   free(str);
   
   return(0);
}

Let's compile and run the above program, which will result in the following:

String = w3cschool, Address = 355090448
String = w3cschool.cc, Address = 355090448

C standard library - <stdlib.h> C standard library - <stdlib.h>