I have the following code:
#include <iostream>
using namespace std;
int *growArray (int* p_values, int cur_size)
{
int *p_new_values = new int[ cur_size * 2 ];
for ( int i = 0; i < cur_size; ++i )
{
p_new_values[ i ] = p_values[ i ];
}
delete p_values;
return p_new_values;
}
int main ()
{
int next_element = 0;
int size = 10;
int *p_values = new int[ size ];
int val;
cout << "Please enter a number: ";
cin >> val;
while ( val > 0 )
{
if ( size == next_element + 1 )
{
cout<< "Im in If"<<endl;
p_values = growArray( p_values, size );
}
p_values[ next_element ] = val;
cout << "Please enter a number (or 0 to exit): ";
cin >> val;
}
}
This code is allocating memory dynamiclly . I can understand everything in this code, exept one thing. What is the purpose of this if ? No matter how many inputs I give, program will never go in that if.