Menu
×
   ❮   
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS R TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI GO KOTLIN SASS VUE DSA GEN AI SCIPY AWS CYBERSECURITY DATA SCIENCE
     ❯   

C Deallocate Memory


Deallocate (free) Memory

When you no longer need a block of memory you should deallocate it. Deallocation is also referred to as "freeing" the memory.

Dynamic memory stays reserved until it is deallocated or until the program ends.

Once the memory is deallocated it can be used by other programs or it may even be allocated to another part of your program.


Free Memory

To deallocate memory, use the free() function:

free(pointer);

The pointer parameter is a pointer to the address of the memory to be deallocated:

int *ptr;
ptr = malloc(sizeof(*ptr));

free(ptr);
ptr = NULL;

It is considered a good practice to set a pointer to NULL after freeing memory so that you cannot accidentally continue using it.

If you continue using memory after it has been freed you may corrupt data from other programs or even another part of your own program.

Example

A working example including error checking and freeing:

int *ptr;
ptr = malloc(sizeof(*ptr)); // Allocate memory for one integer

// If memory cannot be allocated, print a message and end the main() function
if (ptr == NULL) {
  printf("Unable to allocate memory");
  return 1;
}

// Set the value of the integer
*ptr = 20;

// Print the integer value
printf("Integer value: %d\n", *ptr);

// Free allocated memory
free(ptr);

// Set the pointer to NULL to prevent it from being accidentally used
ptr = NULL;
Try it Yourself »


Memory Leaks

A memory leak happens when dynamic memory is allocated but never freed.

If a memory leak happens in a loop or in a function that is called frequently it could take up too much memory and cause the computer to slow down.

There is a risk of a memory leak if a pointer to dynamic memory is lost before the memory can be freed. This can happen accidentally, so it is important to be careful and keep track of pointers to dynamic memory.

Here are some examples of how a pointer to dynamic memory may be lost.

Example 1

The pointer is overwritten:

int x = 5;
int *ptr;
ptr = calloc(2, sizeof(*ptr));
ptr = &x;

In this example, after the pointer is changed to point at x, the memory allocated by calloc() can no longer be accessed.

Example 2

The pointer exists only inside a function:

void myFunction() {
  int *ptr;
  ptr = malloc(sizeof(*ptr));
}

int main() {
  myFunction();
  printf("The function has ended");
  return 0;
}

In this example, the memory that was allocated inside of the function remains allocated after the function ends but it cannot be accessed anymore. One way to prevent this problem is to free the memory before the function ends.

 Example 3

The pointer gets lost when reallocation fails:

int* ptr;
ptr = malloc(sizeof(*ptr));
ptr = realloc(ptr, 2*sizeof(*ptr));

If realloc() is unable to reallocate memory it will return a pointer to NULL and the original memory will remain reserved.

In this example, if realloc() fails then the NULL pointer is assigned to the ptr variable, overwriting the original memory address so that it cannot be accessed anymore.


Summary

In summary, when managing memory in C, use best practices:

  1. Remember to check for errors (NULL return values) to find out if memory allocation was sucessful or not
  2. Prevent memory leaks - always remember to free memory that is no longer used, or else the program might underperform or even worse, crash if it runs out of memory
  3. Set the pointer to NULL after freeing memory so that you cannot accidentally continue using it



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
[email protected]

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
[email protected]

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2024 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.