You are on page 1of 1

Dear Sir,

I am beginner in C++, and your book has been greatly helpful in learning this
programming language. In your book, I have found an example which seems to
have flaws.
Under the topic Dynamic Memory Allocation, there is an example to show a
technique of reallocating memory as the values grow.
You say that the program reads in numbers from the user, and if the user enters
more numbers than can fit in the array, the function int *growArray (int*
p_values, int cur_size) will resize it.
In my attempt to understand the program I found that condition if (size ==
next_element + 1) never becomes true as next_element always remains 0, and so
there is no chance of calling the function. But the program repeatedly prints the first
element deceiving the user to make them think what the program is not doing.
We can, however, call function by incrementing next_element (i.e next_element++)
and then check the if condition, but doing that also doesnt solve the problem since
the function doesnt return the new value of cur_size (to size ) when it does
return p_new_values;, but program does allow the user to enter more new values
as next_element now exceeds the value of size and hence If condition never
becomes true again.
I appreciate the effort you have put into the book to teach C++ to novice
programmers like me. I hope to hear suggestions on writing the program correctly.
Thank you.
Bikash Kunwar