how to reallocate memory in c++? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

how to reallocate memory in c++?

int *a; a = new int[5]; a[0] = 5; cout<< a << " " << a[0] << endl; a = new int[10]; cout<< a << " " <<a[0]; In the above code both the output of a and a[0] are different for both of the outputs. In C, we use realloc function to dynamically reallocate memory. As I used new int[10] second time it didn't reallocate memory, it allocates memory in different location instead.

17th Dec 2021, 6:52 PM
Kashyap Kumar
Kashyap Kumar - avatar
0 Réponse