Use of realloc() Last Updated : 28 May, 2017 Comments Improve Suggest changes Like Article Like Report Size of dynamically allocated memory can be changed by using realloc(). As per the C99 standard: C void *realloc(void *ptr, size_t size); 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(). Program 1: C #include <stdio.h> #include <stdlib.h> int main() { int arr[2], i; int *ptr = arr; int *ptr_new; arr[0] = 10; arr[1] = 20; // incorrect use of new_ptr: undefined behaviour ptr_new = (int *)realloc(ptr, sizeof(int)*3); *(ptr_new + 2) = 30; for(i = 0; i < 3; i++) printf("%d ", *(ptr_new + i)); getchar(); return 0; } Output: Undefined Behavior Program 2: C #include <stdio.h> #include <stdlib.h> int main() { int *ptr = (int *)malloc(sizeof(int)*2); int i; int *ptr_new; *ptr = 10; *(ptr + 1) = 20; ptr_new = (int *)realloc(ptr, sizeof(int)*3); *(ptr_new + 2) = 30; for(i = 0; i < 3; i++) printf("%d ", *(ptr_new + i)); getchar(); return 0; } Output: 10 20 30 Comment More info K kartik Follow Improve Article Tags : C Language C-Dynamic Memory Allocation c-memory-management Explore C BasicsC Language Introduction6 min readFeatures of C Programming Language3 min readC Programming Language Standard6 min readC Hello World Program1 min readCompiling a C Program: Behind the Scenes4 min readC Comments3 min readTokens in C4 min readKeywords in C2 min readVariables and ConstantsC Variables4 min readConstants in C4 min readConst Qualifier in C6 min readDifferent ways to declare variable as constant in C2 min readScope rules in C5 min readInternal Linkage and External Linkage in C4 min readGlobal Variables in C3 min readData TypesData Types in C5 min readLiterals in C4 min readEscape Sequence in C5 min readbool in C5 min readInteger Promotions in C2 min readCharacter Arithmetic in C2 min readType Conversion in C4 min readInput/OutputBasic Input and Output in C4 min readFormat Specifiers in C5 min readprintf in C5 min readscanf in C3 min readScansets in C2 min readFormatted and Unformatted Input/Output in C6 min readOperatorsOperators in C11 min readArithmetic Operators in C5 min readUnary Operators in C5 min readRelational Operators in C4 min readBitwise Operators in C6 min readC Logical Operators4 min readAssignment Operators in C4 min readIncrement and Decrement Operators in C4 min readConditional or Ternary Operator (?:) in C3 min readsizeof operator in C3 min readOperator Precedence and Associativity in C7 min readControl Statements Decision-MakingDecision Making in C (if , if..else, Nested if, if-else-if )7 min readC - if Statement4 min readC if else Statement3 min readC if , else if ladder4 min readSwitch Statement in C5 min readUsing Range in switch Case in C2 min readC - Loops6 min readC for Loop4 min readwhile Loop in C5 min readdo...while Loop in C4 min readFor vs. While4 min readContinue Statement in C4 min readBreak Statement in C5 min readgoto Statement in C4 min readFunctionsC Functions6 min readUser-Defined Function in C6 min readParameter Passing Techniques in C3 min readFunction Prototype in C4 min readHow can I return multiple values from a function?3 min readmain Function in C5 min readImplicit Return Type int in C2 min readCallbacks in C4 min readNested Functions in C4 min readVariadic Functions in C5 min read_Noreturn function specifier in C2 min readPredefined Identifier __func__ in C2 min readC Library math.h Functions6 min readArrays & StringsArrays in C6 min readProperties of Array in C7 min readMultidimensional Arrays in C - 2D and 3D Arrays8 min readInitialization of Multidimensional Array in C4 min readPass Array to Functions in C3 min readHow to pass a 2D array as a parameter in C?3 min readWhat are the data types for which it is not possible to create an array?2 min readHow to pass an array by value in C ?2 min readStrings in C6 min readArray of Strings in C3 min readWhat is the difference between single quoted and double quoted declaration of char array?2 min readC String Functions6 min readPointersC Pointers9 min readPointer Arithmetics in C with Examples10 min readC - Pointer to Pointer (Double Pointer)5 min readFunction Pointer in C6 min readHow to Declare a Pointer to a Function?2 min readPointer to an Array | Array Pointer5 min readDifference between constant pointer, pointers to constant, and constant pointers to constants3 min readPointer vs Array in C1 min readDangling, Void , Null and Wild Pointers in C6 min readNear, Far and Huge Pointers in C4 min readrestrict Keyword in C3 min readC User-Defined Data TypesC Structures9 min readdot (.) Operator in C2 min readC typedef3 min readStructure Member Alignment, Padding and Data Packing8 min readFlexible Array Members in a structure in C4 min readC Unions4 min readBit Fields in C8 min readDifference Between Structure and Union in C4 min readAnonymous Union and Structure in C3 min readEnumeration (or enum) in C5 min read Like