void CArray::resize( int p_Size )
{
dataType * newArray = new dataType[ p_Size ];
int min = _size > p_Size ? _size:p_Size;
for( int index = 0; index < min; index++ )
{
newArray[index] = _array[index];
}
delete [] _array;
_array = newArray;
}
NOTE:
''_size'' is a private variable that holds the integer size of the array.
''_array'' is a private variable in the class of CArray which is the actually array of type, template....dataType * _array
The part I don''t really understand is why create the variable ''min''? What if you want to resize the array to a larger size than before? It will count up to the previous, smaller size so it wouldn''t be a real resize. I don''t understand how this algorithm is legit.
-----------------------
"In reality God is a comedian but his audience are too afraid to laugh."
Someone explain this resize algorithm........
If you tried copying to the size of the larger array you would read invalid data. After all, there''s nothing to read past the size of the old array.
i believe min just check if the asked for is not smaller than the current size. if it is then, then it''ll stick with the original size, otherwise increase the array with p_Size.
i don''t understand how the algorithm isn''t legit.
it seems to do the job it''s asked of....
i don''t understand how the algorithm isn''t legit.
it seems to do the job it''s asked of....
It's broken. If _size > p_Size, then the copy will write past the end of the newArray array. The line should be something like
[edited by - JohnBolton on March 19, 2004 11:44:13 PM]
int min = _size < p_Size ? _size : P_Size;
Probably nobody caught the bug because resizing to a smaller size doesn't happen very often.[edited by - JohnBolton on March 19, 2004 11:44:13 PM]
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement