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.
- Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
- Why to use fgets() over scanf() in C?
- Thread functions in C/C++
- How to Compile and Run C/C++/Java Programs in Linux
- How to get the current position of cursor from output screen in C?
- Methods to concatenate string in C/C++ with Examples
- Compiling with g++
- Why learning C Programming is a must?
- Lex program to count the number of lines, spaces and tabs
- C program to check if a given string is Keyword or not
- Difference between scanf() and gets() in C
- Dividing a Large file into Separate Modules in C/C++, Java and Python
- C program to store Student records as Structures and Sort them by Name
- Similarities and Differences between Ruby and C language