Size of dynamically allocated memory can be changed by using realloc().
As per the C99 standard:
realloc deallocates the old object pointed to by ptr and returns a pointer to a new object that has the size specified by size. The contents of the new object is identical to that of the old object prior to deallocation, up to the lesser of the new and old sizes. Any bytes in the new object beyond the size of the old object have indeterminate values.
The point to note is that realloc() should only be used for dynamically allocated memory. If the memory is not dynamically allocated, then behavior is undefined.
For example, program 1 demonstrates incorrect use of realloc() and program 2 demonstrates correct use of realloc().
10 20 30
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.
- Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
- Difference between C and Dart
- Difference between Sentinel and Counter Controlled Loop in C
- Why do we need reference variables if we have pointers
- What are the differences between C and Embedded C?
- Macros and its types in C/C++
- Draw a smiley face using Graphics in C language
- NULL undeclared error in C/C++ and how to resolve it
- Draw a Chess Board using Graphics Programming in C
- Which will be faster while(1) or while(2)?
- what happens when you don't free memory after using malloc()
- Average of Cubes of first N natural numbers
- Top 10 Programming Languages for Blockchain Development
- How many levels of pointers can we have in C/C++