🔥Limited Offer: Get 50% OFFon AI & Full Stack Courses🔥
Back to C Notes
Topic #83

C Reallocate Memory


Reallocate Memory

If the amount of memory you reserved is not enough, you can reallocate it to make it larger.

Reallocating reserves a different (usually larger) amount of memory while keeping the data that was stored in it.

You can change the size of allocated memory with the realloc() function.

The realloc() function takes two parameters:

int *ptr2 = realloc(ptr1, size);
  • The first parameter is a pointer to the memory that is being resized.
  • The second parameter specifies the new size of allocated memory, measured in bytes.

The realloc() function tries to resize the memory at ptr1 and return the same memory address. If it cannot resize the memory at the current address then it will allocate memory at a different address and return the new address instead.

Note: When realloc() returns a different memory address, the memory at the original address is no longer reserved and it is not safe to use. When the reallocation is done it is good to assign the new pointer to the previous variable so that the old pointer cannot be used accidentally.

Example

  int *ptr1, *ptr2, size;

// Allocate memory for four integers
size =
  4 * sizeof(*ptr1);
ptr1 = malloc(size);

printf("%d bytes allocated
  at address %p \n", size, ptr1);

// Resize the memory to hold six
  integers
size = 6 * sizeof(*ptr1);
ptr2 = realloc(ptr1, size);

  printf("%d bytes reallocated at address %p \n", size, ptr2);

NULL Pointer & Error Checking

The realloc() function returns a NULL pointer if it is not able to allocate more memory. This is very unlikely, but it is worth keeping in mind when you need your code to be failproof.

The following example checks whether realloc() is able to resize the memory or not, by checking for a NULL pointer:

Example

int *ptr1, *ptr2;

// Allocate memory
ptr1 = malloc(4);

//
Attempt to resize the memory
ptr2 = realloc(ptr1, 8);

// Check
whether realloc is able to resize the memory or not
if (ptr2 ==
NULL) {
  // If reallocation fails
  printf("Failed. Unable to resize memory");
} else {

// If reallocation is successful

printf("Success. 8 bytes reallocated at address %p \n", ptr2);
  ptr1 = ptr2;
// Update ptr1 to point to the newly allocated memory
}

Note: You should always include error checking (if pointer == NULL) when allocating memory.

Note: You should also always free, or release, allocated memory when you are done using it. This is important to make sure that your program behaves as expected, but it will also make it more maintainable and efficient.

To free memory, simply use the free() function:

Example

  // Free allocated memory
free(ptr1);

You will learn more about how to free allocated memory and why this is important in the next chapter.

Want to go beyond the notes?

Join CodingNow 2.0's C course — live mentorship, real projects, and 100% placement support.

Enroll Now — Free Demo Available

C Reallocate Memory – FAQs

Quick answers about learning C Reallocate Memory in C.

This free note from CodingNow 2.0 explains C Reallocate Memory in C — concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every C topic on CodingNow 2.0, including C Reallocate Memory, is 100% free with no signup required.
With focused practice, most students grasp C Reallocate Memory in 1–3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) — expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now