Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

C Reallocate Memory

Reallocate Memory

If the allocated memory is insufficient, you can reallocate it to increase its size.

Reallocating reserves a different amount of memory, typically larger, while retaining the data stored within it.

You can adjust the size of allocated memory using the realloc() function.

The realloc() function requires two parameters:

 

int *ptr2 = realloc(ptr1, size);

 

  1. The first parameter is a pointer indicating the memory being resized.
  2. The second parameter denotes the new size of allocated memory, measured in bytes.

The realloc() function attempts to resize the memory at the address specified by ptr1 and returns the same address if successful. If resizing at the current address is not possible, it allocates memory at a different address and returns this new address instead.

Important to note: If realloc() returns a different memory address, the memory previously reserved at the original address is no longer guaranteed and should not be accessed. To ensure safety, after reallocation, it’s advisable to assign the new pointer to the previous variable, preventing inadvertent use of the old pointer.

Example

Expand the size of allocated memory.

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 fails to allocate more memory, although such occurrences are rare. However, it’s prudent to consider this possibility when aiming for robust code.

The subsequent example demonstrates a check for realloc()’s success by inspecting for a NULL pointer.

Example

Verify the existence of a NULL pointer.

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 sucessful
  printf(“Success. 8 bytes reallocated at address %p \n”, ptr2);
  ptr1 = ptr2;  // Update ptr1 to point to the newly allocated memory

 

Remember to incorporate error checking (e.g., if pointer == NULL) when allocating memory.

Additionally, it’s crucial to release allocated memory once it’s no longer needed. This practice not only ensures expected program behavior but also enhances maintainability and efficiency.

To release memory, employ the free() function.

Example

Release allocated memory.

// Free allocated memory
free(ptr1);

In the next chapter, you’ll delve deeper into the process of freeing allocated memory and understand its importance.