Re: dynamic array resizing
Your for loop will only allow 2 numbers to be entered!
Your resize function also has issues. Why are using size * 2? As listDyn and size are passed by value, any new value assigned to them during the function isn't passed back to the calling code.
Quote:
I have to do this without using vectors.
Shame! Boo! :D
Quote:
I was thinking of a do-while?
Your loop in main() needs to loop until either a -1 is entered or an error occurs on the input stream.
Re: dynamic array resizing
Honestly, your assignment doesn't make sense, even if you can't use vector.
You're creating a dynamic array in main, but then you just call resize() which basically boils down to doing nothing. Yes, you're allocating memory in resize, but then you deallocate the memory at the end of resize. Basically a lot of wheel-spinning with nothing being accomplished. Are you sure you're supposed to deallocate the memory you just allocated, or is that a bug?
Code:
listDyn=listDynNew;
delete[] listDynNew;
?
Also, you have a memory leak, since the initial allocation of listDyn is never deallocated.
Regards,
Paul McKenzie
Re: dynamic array resizing
You should use a while loop that accepts an integer and terminates when that integer is -1. You can't input directly to the array is it probably isn't going to be big enough.
A dynamic array works by allocating more memory than the current array, copying the current array into the new memory and deleting the old. Of course, the pointer to the start of the array will change each time you do this. You'll need a function to add your int to the array, resizing it if necessary.
Re: dynamic array resizing
I can advise you to read Data Programming in C++,
this will provide you a good understanding for how to manage the memory during run time of your program..
you can read also read Ivor horton book " VC++ Programming Published By Wrox"
Re: dynamic array resizing
Quote:
I can advise you to read Data Programming in C++,
Can you provide more details please as I can't find a book on Amazon with this exact title.
Quote:
Ivor horton book " VC++ Programming Published By Wrox"
Again in Amazon there does not appear to be a book by Ivor Horton with this title. The latest ones by him are
Beginning Modern c++
http://www.amazon.co.uk/Beginning-C-...horton+c%2B%2B
beginning Visual c++ 2013
http://www.amazon.co.uk/Ivor-Hortons...horton+c%2B%2B
Neither of which I have seen so can't comment upon them.