is Jumping Into C++'s sample code wrong??

Hi everyone. First of all, I am not really claiming that the code has to be wrong. I am not judging it, rather, learning it for the first time and it 'seems' wrong. I could be totally mistaken or perceived its concept in totally wrong way.
I am beginning pointers and the book gave the sample code for a program that extends the array's memory if we run out of array's pre-determined size.

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
The code as it is given in the book:

#include <iostream>

using namespace std;

int *growArray (int* p_values, int cur_size);

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 )
		{
			// now all we need to do is implement growArray 
			p_values = growArray( p_values, size );
		}
		p_values[ next_element ] = val;
		cout << "Please enter a number (or 0 to exit): ";
		cin >> val;
	} 
}

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;
}


It seems to me that the code tries to enter user's response everytime in the same "p_values[0]" array, re-writing its previous responses because the 'next_element' variable is set to zero and is not updated later.
Is the code really wrong or did I misunderstand the code?
Here is my code for it to achieve what I thought the problem was trying to say:

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

#include <iostream>
using namespace std;

int *grow_array(int *p_dummy_array, int cur_size)  ///////use of * in front of grow_array????
{
    int *p_new_array=new int[cur_size*2];
    for(int i=0;i<cur_size;i++)
    {
        p_new_array[i]=p_dummy_array[i];
    }
    delete p_dummy_array;
    return p_new_array;
}
int main()
{
    int array_size=3;
    int *array=new int[array_size];
    int input;
    cout<<"Enter a number(or 0 to exit): ";
    cin>>input;
    for(int i=0;input!=0;i++)
    {
        array[i]=input;
        if(i==(array_size-1))
        {
            array=grow_array(array, array_size);//to extend memory of array and save it in old array
            array_size*=2;
        }
        cout<<"Enter a number(or 0 to exit): ";
        cin>>input;
    }
    cout<<endl<<"The entered numbers are: "<<endl;
    for(int i=0;i<array_size;i++)
    {
        cout<<array[i]<<endl;
    }
}


1. I did not understand the part in my own code why the 'grow_array' function used '*' in front of it. I initially wrote the program without '*' but it didn't work and by comparing it with authors' code, tried using '*' and only then it worked.
Any help would be highly appreciated.
It looks like they forgot to increment next element.
change line 22 to p_values[ next_element++ ] = val;

grow_array returns array, i.e. pointer to first element.
@MiiNiPaa
change line 22 to p_values[ next_element++ ] = val;

That still wouldn't update it as line 22 is inside 'if' statement.
and I didn't quite understand
i.e. pointer to first element.
. could you please elaborate it?
The new new int[cur_size*2] allocates dynamically a block of memory from "free store". That expression returns the address of the block. The address is required when the block should be deallocated. (Neither program deallocates the latest allocated block so there is a minor "memory leak".)

The grow_array() returns the address. The type of the address is int*, an address of an integer. The block, however, is larger than one integer. The block has room for multiple integers.

An aspect of the pointer type is that if you have int* foo, then foo+1 is the address of next integer. You can access the consecutive values in the memory block with foo[0], foo[1], ...
(Those mean, "access the value at the address that is stored in foo", "access the value at the address that is right after the address stored in foo", ...)


You do update array_size on line 28. You have to do that, because array_size has to match to the size of the block that the "array" points to. However, how can the line 28 be sure that grow_array doubles the blocksize? The code of the function could be moved to an another file and an another person starts to maintain it. Out of sight, out of mind. The function becomes popular and is called from many places. Every caller has to update their "size" variable. Then grow_array is for some reason changed to triple, rather than double. A maintenance nightmare.

The grow_array should take the size parameter as reference and update it after the copy is complete.
that's bad code.
get a better book.
Topic archived. No new replies allowed.