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.
- How to Read and Print an Integer value in C
- Nested switch case
- Inline function in C
- Find the Nth term of the series 14, 28, 20, 40,.....
- Loader in C/C++
- Sum of array Elements without using loops and recursion
- Program to Convert Hexadecimal to Octal
- Is there any equivalent to typedef of C/C++ in Java ?
- Find amount to be added to achieve target ratio in a given mixture
- Program to Print Mirrored Hollow Parallelogram
- Memory leak in C++ and How to avoid it?
- “static const” vs “#define” vs “enum”
- Why are elementwise additions much faster in separate loops than in a combined loop?
- Difference between Static and Shared libraries