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

C Tutorial

C HOME C Intro C Get Started C Syntax C Output C Comments C Variables C Data Types C Type Conversion C Constants C Operators C Booleans C If...Else C Switch C While Loop C For Loop C Break/Continue C Arrays C Strings C User Input C Memory Address C Pointers

C Functions

C Functions C Function Parameters C Pass By Reference C Scope C Function Declaration C Functions Challenge C Math Functions C Inline Functions C Recursion C Function Pointers

C Files

C Create Files C Write To Files C Read Files

C Structures

C Structures C Structs Challenge C Nested Structures C Arrays of Structs C Structs & Pointers C Unions C typedef C Struct Padding

C Enums

C Enums

C Memory

C Memory Management C Allocate Memory C Access Memory C Reallocate Memory C Deallocate Memory C Structs and Memory C Memory Example C Memory Mistakes

C Errors

C Errors C Error Challenge C Debugging C NULL C Error Handling C Input Validation

C Problem Solving

C Problem Solving C Build the Logic C Edge Cases & Errors C Test Cases

C More

C Date C Random Numbers C Macros C Organize Code C Storage Classes C Bitwise Operators C Fixed-width Integers C Command-Line Args

C DSA

C DSA Intro C Searching C Sorting C Stacks C Queues C Linked Lists C Trees C Hash Tables C Big O

C Projects

C Projects

C Cert

C Certificate

C Reference

C Reference C Keywords C <stdio.h> C <stdlib.h> C <string.h> C <math.h> C <ctype.h> C <time.h>

C Examples

C Examples C Real-Life Examples C Exercises C Quiz C Code Challenges C Practice Problems C Compiler C Syllabus C Study Plan C Interview Q&A

C Memory Mistakes


Share

Common Memory Mistakes

In the previous chapters, you learned how to allocate, use and free memory. C gives you a lot of control over memory, but it does not check if you use it correctly.

Memory mistakes are some of the most common bugs in C. The compiler usually does not warn you, and the program may even seem to work. But later, it can print wrong values, or suddenly crash.

In C, this is called undefined behavior: the result cannot be predicted, and it can be different every time you run the program.

In this chapter, you will learn the most common memory mistakes, and how to avoid them.


Writing Outside an Array

An array has a fixed size. If an array has 3 elements, the indexes are 0, 1 and 2. C does not stop you if you use index 3, so the program writes to memory that does not belong to the array:

Example

int numbers[3];
int i;

for (i = 0; i <= 3; i++) {
  numbers[i] = i * 10;  // numbers[3] is outside the array
}

This is called a buffer overflow. It can change the value of other variables, or crash the program.

The fix: make sure the index stays inside the array. Use i < 3, not i <= 3:

Example

int numbers[3];
int i;

for (i = 0; i < 3; i++) {
  numbers[i] = i * 10;
}

for (i = 0; i < 3; i++) {
  printf("%d\n", numbers[i]);
}
Try it Yourself »

The same can happen with strings. "Christopher" needs 12 characters (11 letters and the \0), so it does not fit in a string of 5:

Example

char name[5];
strcpy(name, "Christopher");  // Too long for name

Make sure the string is big enough for all the characters and the \0:

Example

char name[20];
strcpy(name, "Christopher");
printf("%s\n", name);
Try it Yourself »

Using Memory After free()

After you free memory, it no longer belongs to your program. The pointer still holds the old address, but the memory may already be used for something else:

Example

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

free(ptr);
printf("%d\n", *ptr);  // The memory is already freed

A pointer that points to memory that is no longer valid is called a dangling pointer.

The fix: set the pointer to NULL after free(), like you learned in the Deallocate Memory chapter. Then you can check if the pointer is NULL before you use it:

Example

int *ptr = malloc(sizeof(*ptr));
*ptr = 20;
printf("%d\n", *ptr);

free(ptr);
ptr = NULL;

if (ptr != NULL) {
  printf("%d\n", *ptr);
} else {
  printf("The memory is freed\n");
}
Try it Yourself »


Returning the Address of a Local Variable

A local variable only exists while its function runs (see Scope). When the function ends, the variable is removed, so its address is no longer valid:

Example

int *getNumber() {
  int number = 10;
  return &number;  // number is removed when the function ends
}

The pointer that getNumber() returns is a dangling pointer.

The fix: if you need the memory after the function ends, allocate it with malloc(). Remember to free it when you are done:

Example

int *getNumber() {
  int *ptr = malloc(sizeof(*ptr));
  if (ptr != NULL) {
    *ptr = 10;
  }
  return ptr;
}

int main() {
  int *number = getNumber();

  if (number != NULL) {
    printf("%d\n", *number);
    free(number);
  }
  return 0;
}
Try it Yourself »

Tip: If you only need the value, return the value itself (int instead of int *).


Freeing Memory Twice

Calling free() two times on the same memory can crash the program:

Example

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

free(ptr);
free(ptr);  // The memory is already freed

The fix: set the pointer to NULL after free(). Calling free() with NULL does nothing, so the second call is safe:

Example

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

free(ptr);
ptr = NULL;

free(ptr);  // Safe: free(NULL) does nothing
printf("The program is still running\n");
Try it Yourself »

Summary

Mistake How to avoid it
Writing outside an array (buffer overflow) Make sure the index is between 0 and the size minus 1, and that strings have room for the \0
Using memory after free() Set the pointer to NULL after free(), and check for NULL before you use it
Returning the address of a local variable Use malloc(), or return the value itself
Freeing memory twice Set the pointer to NULL after free()
Forgetting to free memory (memory leak) Free memory when you no longer need it (see Deallocate Memory)

Free account

Track your progress

XP earned 0 Day streak 0 Spaces 0 League —

New

Learn in Adventure Mode W3Schools Adventure App

Coding fundamentals as bite-sized lessons and challenges.

×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

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, cookies and privacy policy.

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