Jan 18, 2016 at 7:29pm
In the longer function you need to allocate a new bigger array if you want to be able to store more elements.
Jan 18, 2016 at 7:42pm
Oh so i need to delete array a and make it again with new length ?
I fixed it like this:
1 2 3 4 5 6 7 8 9 10
|
void longer(int length)
{
if(this-> length + length > maxlength)
cout << "Array can't be longer than 10." << endl;
else
this-> length += length;
delete [] a;
a = new double[this->length];
}
|
Do i need to delete it or i can just say :
|
a = new double[this->length];
|
Last edited on Jan 18, 2016 at 7:44pm
Jan 18, 2016 at 9:14pm
Yes, you need to delete it, otherwise you'll have a memory leak.
Note that if you don't want to lose the values that was stored in the old array you need to copy them to the new array before deleting the old array.