Replying to your latest post:
If you tried to test your code, you'll probably very quickly see that it isn't correct.
First, you need to check whether or not you need to resize the array in the first place. Your if-statement on line 5 is doing this. But everything else you do is bizarre.
Let's say the initialize size of your m_rectangles array is 10, right?
And the initial value of m_nextFree should be 0, because start with 0 actual logical elements in your array. (I am assuming this, not 100% if that's actually how you want it to work.)
m_nextFree = 0 --> insert 1st item --> m_nextFree = 1
m_nextFree = 1 --> insert 2nd item --> m_nextFree = 2
m_nextFree = 2 --> insert 3rd item --> m_nextFree = 3
...
m_nextFree = 9 --> insert 10th item --> m_nextFree = 10
So, what happens when you try to insert the 11th item in the array? 10 is no longer a valid array index.
When this happens, you need to resize your dynamic array. When you resize an array, what you actually need to do is
(1) allocate a new array with a larger size (in this case 2x as many elements)
(2) copy over the existing elements of the old array into the new array
(3) delete the old array
(4) assign the old array's pointer to point to the new data.
I'll show an example of this with ints. You should be able to apply it to CRectangles as well.
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 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
|
// Example program
#include <iostream>
#include <string>
class Array {
public:
Array()
: capacity(5), current_size(0)
{
arr = new int[capacity];
}
~Array()
{
delete [] arr;
}
void add(int value)
{
if (current_size == capacity)
{
// need to re-allocate array with a bigger capacity, twice as big
// I did not do this, but once you go above 100, you only add 100 each time you need to resize.
// You need to implement that part yourself (it's a very simple if check)
int new_capacity = capacity * 2;
int* new_arr = new int[new_capacity];
for (int i = 0; i < current_size; i++)
{
// copy over each existing element
new_arr[i] = arr[i];
}
// delete old array, and make it point to new array
delete [] arr;
arr = new_arr;
// update capacity
capacity = new_capacity;
}
// add to next free index
arr[current_size] = value;
current_size++;
}
public: // making public just for demonstration purposes
int capacity;
int current_size;
int* arr;
};
std::ostream& operator<<(std::ostream& os, const Array& array)
{
for (int i = 0; i < array.current_size; i++)
{
os << array.arr[i] << " ";
}
return os;
}
int main()
{
Array arr;
for (int i = 0; i < 21; i++)
{
int value = 2 + i*i; // just some arbitrary value
arr.add(value);
std::cout << "added " << value << " to arr... ";
std::cout << " new size == " << arr.current_size << ", new capacity == " << arr.capacity << "\n";
std::cout << " new arr = " << arr << "\n\n";
}
}
|
added 2 to arr... new size == 1, new capacity == 5
new arr = 2
added 3 to arr... new size == 2, new capacity == 5
new arr = 2 3
added 6 to arr... new size == 3, new capacity == 5
new arr = 2 3 6
added 11 to arr... new size == 4, new capacity == 5
new arr = 2 3 6 11
added 18 to arr... new size == 5, new capacity == 5
new arr = 2 3 6 11 18
added 27 to arr... new size == 6, new capacity == 10
new arr = 2 3 6 11 18 27
added 38 to arr... new size == 7, new capacity == 10
new arr = 2 3 6 11 18 27 38
added 51 to arr... new size == 8, new capacity == 10
new arr = 2 3 6 11 18 27 38 51
added 66 to arr... new size == 9, new capacity == 10
new arr = 2 3 6 11 18 27 38 51 66
added 83 to arr... new size == 10, new capacity == 10
new arr = 2 3 6 11 18 27 38 51 66 83
added 102 to arr... new size == 11, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102
added 123 to arr... new size == 12, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123
added 146 to arr... new size == 13, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146
added 171 to arr... new size == 14, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171
added 198 to arr... new size == 15, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198
added 227 to arr... new size == 16, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227
added 258 to arr... new size == 17, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258
added 291 to arr... new size == 18, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 291
added 326 to arr... new size == 19, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 291 326
added 363 to arr... new size == 20, new capacity == 20
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 291 326 363
added 402 to arr... new size == 21, new capacity == 40
new arr = 2 3 6 11 18 27 38 51 66 83 102 123 146 171 198 227 258 291 326 363 402
|