Hey everyone I have a question thats ben bugging me for over a month now and I seem to be stuck on this chapter of my c++ book (alex allains jumping into c++)
so the chapter is Dynamic memory allocation I'm so confused as to how this code actually works here is the description of what he wants the code to do this is a snippet taken from the book with his explanation
"int count_of_numbers;
cin >> count_of_numbers;
int *p_numbers = new int[count_of_numbers];
this code asks the user how many numbers are needed and then uses that variable to determine the size of the dynamically allocated array.in fact,we don't even need to know up-front the exact number-we can just reallocate the memory as the values grow.this means we'll be doing some extra copying,but it's possible.let's look at a program that demonstrates this technique ,let's have this program read in numbers from the user,and if the user enters more numbers than can fit in the array,we'll resize it."
so heres the code I'll comment what parts confuse me and what I think I understand comments with the star are mine
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
|
#include <iostream>
using namespace std;
int *growArray (int* p_values, int *size);
void printArray (int* p_values, int size, int elements_set);
int main ()
{
int next_element = 0;
int size = 10;
int *p_values = new int[ size ]; // sets array size to 10 understand
int val;
cout << "Please enter a number: ";
cin >> val;
while ( val > 0 )
{
if ( size == next_element + 1 ) // *don't understand
{ // *how can size = next element plus one?
// now all we need to do is implement growArray
// notice that we need to pass in size as a pointer
// since we need to keep track of the size of the array as
// it grows!
p_values = growArray( p_values, & size );
}
p_values[ next_element ] = val;
next_element++;
cout << "Current array values are: " << endl;
printArray( p_values, size, next_element );
cout << "Please enter a number (or 0 to exit): ";
cin >> val;
}
delete [] p_values;
}
void printArray (int *p_values, int size, int elements_set)
{
cout << "The total size of the array is: " << size << endl;
cout << "Number of slots set so far: " << elements_set << endl;
cout << "Values in the array: " << endl;
for ( int i = 0; i < elements_set; ++i )
{
cout << "p_values[" << i << "] = " << p_values[ i ] << endl;
}
}
int *growArray (int* p_values, int *size)
{
*size *= 2;
int *p_new_values = new int[ *size ];
for ( int i = 0; i < *size; ++i )
{
p_new_values[ i ] = p_values[ i ];
}
delete [] p_values;
return p_new_values;
}
|
to be honest I really have very little clue as to whats happening here i'm lost,I must have read the example literally hundreds of times but I can't figure out how to make sense of all this could someone try break this down with me step by step? I would be very thankful and appreciate it more than anything it's stressing me out bad and my exams are fast approaching for college =( I'm sorry if it's a long question or long to break down