realloc() in C

Learn via video course
FREE
View all courses
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
C++ Course: Learn the Essentials
C++ Course: Learn the Essentials
by Prateek Narang
1000
5
Start Learning
Topics Covered

Overview

In the C programming language, along with static memory, there is a concept of dynamic memory. There are functions in C programming that are used for dynamic memory allocation, and one of them is the realloc() function.

Description

The realloc() function in the C programming language is used to resize the memory block pointed to a pointer that was previously allocated to the variable by the malloc() or calloc() function. Realloc stands for reallocation.

Malloc: malloc() is a function in C programming language that is used to dynamically allocate a single large block of memory with a specific size. Malloc stands for memory allocation.

Calloc: calloc() is a function in C programming language that is used to dynamically allocate a contiguous specific number of blocks of a specific size. Calloc stands for contiguous allocation.

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

Declaration

Parameters

The C realloc() Function accepts 2 parameters as an argument. The details of the parameters are given below

  • ptr − It is the pointer of the memory block which was previously allocated to the calloc(), malloc(), or realloc() function that is to be reallocated. If this pointer is NULL, then a new block is allocated and the pointer to it is returned by the realloc() function.

  • size − It is the new size of the memory block which is to be reallocated. It is passed in bytes. If the size is 0, then the memory block pointed by ptr is deallocated and a NULL pointer is returned by the realloc() function even if ptr points to an existing block of memory.

Return Value

If the realloc request is successful, then it will return a pointer to the block of newly allocated memory. If the request fails, it will return a NULL pointer.

Exceptions

The exception of the realloc() is that this function should only be used for allocating the memory dynamically. If the memory is not dynamically allocated, then it will show an undefined behavior and the program may contain garbage values.

Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Examples

Example 1

In this example, we will see how to use the realloc() function for int variables and pointers.

C:

Output

Scaler Placement Report and Statistics

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

Example 2

In this example, we will see how to use the realloc() function for char variables and pointers.

C:

Output

Example 3

In this example, we will see how to use the realloc() function for float variables and pointers.

C:

Output

Example 4

In this example, we will see how to use the realloc() function for double variables and pointers.

C:

Output

Turn Learning into Career Growth

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

How Does realloc() Function Work in C?

Case 1

If the pointer variable is NULL, then the realloc() function will work like the malloc() function.

C:

Output

Case 2

If the new size passed is smaller than the actual size, then the realloc() function will simply reduce the memory size of the block.

C:

Output

Case 3

If the new size passed is larger than the actual size, then the realloc() function will check whether the already available memory can be expanded or not. If yes, then it will simply reduce the memory size of the block.

C:

Output

Uses of realloc() Function in C

The realloc() function in the C programming language is mainly used for deallocating the old memory of the variable pointed to by the pointer and then it returns a pointer to a new variable that has the size specified by size. The contents of the new variable are the same as that of the old variable which was before deallocation. In simple words, we can say that it is used to resize the memory which is allocated by the malloc() or calloc() functions to a variable.

Conclusion

In this quick tutorial, we have discussed the realloc() function in the C programming language. We can extract the following conclusions from the article -

  • The syntax and parameters of the C realloc() function
  • The return value of the C realloc() function
  • Exception of the C realloc() function
  • Uses of the C realloc() function
  • Working of the C realloc() function for different data types with examples