Advertisement

Resizing arrays

Started by November 07, 2000 12:18 PM
10 comments, last by Gaiiden 24 years, 1 month ago
quote: Original post by Gaiiden

Notice here I was able to put a value into the 125th spot!! How did that happen?? I thought I would get 12 spots (6*2 right (I counted the zero)). What heppened here? Was it the way I multiplied inside the brackets or what?


Actually you''d only get 10 spots. Not sure were the 6*2 is from . Your first array will have elements 0..4 or 5 elements. The next array will go from 0..9 or 10 elements.


-------
Andrew
I don't know what you are doing here, your code should not even compile. First you are creating an array, five, and then you are doing this
*five = new int[2*size];  

This would mean that you are assigning an adress to the value of five. The same as:
five[0] = new int[2*size];  


Instead of doing that you should do this
    int *five=new int[size]; //Create the first array;delete [] five;          //Delete itfive=new int[size*2]     //Create a new array of the double sizedelete [] five;          //Delete it    


The fact that you can access the 125th element of the array is that c++ has no size checking for arrays. This is a problem that will make your program crash later, if you have a sligtly bigger program. making it a quite hard to find bug.

Edited by - fredizzimo on November 13, 2000 4:49:54 PM

This topic is closed to new replies.

Advertisement