I also got rid of your use of realloc (a C function) in favor of new (a C++ operator) and removed the part that tries to free the array if arr isn't NULL (we actually say nullptr in C++). That is not necessary since the object is just now being constructed so it can't have any members. In fact, arr would have an arbitrary value at that point, so the free() may in fact be called with an arbitrary value. That's a problem!
I just now went back and added a destructor since the allocated memory needs to be deleted when you are done with it. I also changed unsigned to size_t (which is more appropriate) and used member initialization lists in the constructors.
speaking on that realloc() used times must be igual free() used times?
realloc();
free() - realloc();
free() - realloc();
free() - realloc();
end:
free()
????
don't forget: using the 'new'(yes i must 'delete[]'), we lose the previous values
Does free need to be used an equal number of times as realloc?
Absolutely not! Realloc "reallocates" an already existing array. Since it doesn't create a new one, even if you used realloc 100 times you would only need to call free once.
But that is C, not C++. You need to make up your mind which language you are using. In C++, if we want to reallocate an array, we create a new one first, copy the old one to the new one, then delete the old one.
Don't forget: using the 'new'(yes i must 'delete[]'), we lose the previous values
You totally missed my point. THERE ARE NO PREVIOUS VALUES. How could there be? The constructor is where the array is created.
some arrays can be resized without losing the previous values.
ok.. for that we have 'insert'(or similar). and true theres STATIC and DYNAMIC arrays.
thank you so much for all