
Using this makes our code efficient and smart to handle different sizes of input during the execution of code and allocate memory accordingly.Ĭode : array_pointer = new int Īrray_pointer : Pointer to store the returned pointer to array. User can access the location (array) using the pointer. It returns a suitably typed and nonzero pointer to the object. Using ‘ new‘ operator to allocate memory for arrayĪ new operator allocates memory for an object or array of objects of mentioned data type from the free storage. ( Note: “\n” is used to print text on a new line for better presentation.

(1 less than loop count as ‘-1’ is used for termination and is not a data item) Total_user_entries : total entries are counted according to loop_count. If the condition is met, the input loop is terminated using break statement. ‘if()’ condition is used to check the input entered at every iteration and match with value ‘-1’. Loop_count : variable is used to count the iterations and store data in temporary locations. This array data is stored in temporary variable. We use a ‘while()’ loop to accept data from user. Accept array data entries from user and store it in temporary variable array. Temporary : Integer variable used to to temporarily store array values.įlag : used to check input termination condition.Ģ.

Loop_count : Integer variable used to count loop iterations. Total_user_entries : Integer variable used to store total number of entries accepted from user. Declaring VariablesĪrray_pointer : Integer pointer is used to store pointer to the array we want to store user input in. We will go through each step individually. C++ program to change array size dynamically I have provided step wise information later in the post. Using this technique will certainly be highly beneficial for programmers out there to write a clean and efficient code.

This technique makes the program flexible to act as per the requirement at runtime without any changes in the code. You come across such situations multiple times while programming where the size of the array depends on what the user enters at the time of execution. This is a very useful method used in programming as it ensures efficient memory usage. This tutorial focuses on the method to dynamically allocate memory and change array size according to the user input.
