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.
GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details
- Dynamic Memory Allocation in C using malloc(), calloc(), free() and realloc()
- Format specifiers in different Programming Languages
- C program to find square root of a given number
- C program to print odd line contents of a File followed by even line content
- Getting System and Process Information Using C Programming and Shell in Linux
- Program to calculate Electricity Bill
- Program to print half Diamond star pattern
- C/C++ program for calling main() in main()
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- How to use make utility to build C projects?`
- How to call function within function in C or C++
- Role of SemiColon in various Programming Languages
- C program to display month by month calendar for a given year
- Difference between Type Casting and Type Conversion