I'm trying to enlarge an array of pointers
for the first time it's working well.. but from the seconde on
after i'm deleting the old array it's alert of a heap corruption that i'm trying to write at end of heap...
I just can't understand it!! i try to delete so how come it's trying to write something??
please help me...
size++; // enlarge the accounts + 1
basicAccount **temp;
temp = new basicAccount*[size];
businessAccount *bp;
partnerAccount *pp;
creditAccount *cp;
for(int i=0;i<size;i++)
{
bp = dynamic_cast<businessAccount*>(accounts[i]);
pp = dynamic_cast<partnerAccount*>(accounts[i]);
cp = dynamic_cast<creditAccount*>(accounts[i]);
if (bp)
temp[i] = new businessAccount(*bp);
else if (pp)
temp[i] = new partnerAccount(*pp);
else if (cp)
temp[i] = new creditAccount(*cp);
else
temp[i] = new basicAccount(*accounts[i]);
}
for (int i=0;i<size;i++)
delete accounts[i]; //delete all cells (size is correct)
delete[] accounts; // heap corruption from the second time...
accounts = temp;
....
....
....
}
I do not see any sense. Your array accounts had exactly size elements. Then you create a temporary array temp, copy all elements of accounts into temp and then do the assignment
accounts = temp;
So what is the sense of this operation?!
In other words where is the new element that you shall append to your array?
The goal is that after i'm adding a new account i want the accounts array will be ready to receive another account...
so when i'm adding a new account, accounts array will have a new cell to fill with the new account...
I do not see this new cell. Your array accounts had size elements. Then you allocate array temp with the same size elements. Then you copy all elements from accounts to temp, delete accounts and again assign temp to accounts. Where is this new element?
So either accounts had size - 1 elements in realty. or you do some nonsense.
int main()
{
int size = 0; // size of accounts
basicAccount **accounts;
accounts = new basicAccount*[size+1];
...
...
code goes on
...
...
then i have a switchfor the accounts:
switch(choice)
{
case 1:
..
accounts[size] = new basicAccount(ID,name);
..
break;
case 2:
..
accounts[size] = new creditAccount(ID,name,credit);
..
break;
..
and so on...
and just after the switch ends comes:
size++; // enlarge the accounts + 1
basicAccount **temp;
temp = new basicAccount*[size];
businessAccount *bp;
partnerAccount *pp;
creditAccount *cp;
for(int i=0;i<size;i++)
{
bp = dynamic_cast<businessAccount*>(accounts[i]);
pp = dynamic_cast<partnerAccount*>(accounts[i]);
cp = dynamic_cast<creditAccount*>(accounts[i]);
if (bp)
temp[i] = new businessAccount(*bp);
elseif (pp)
temp[i] = new partnerAccount(*pp);
elseif (cp)
temp[i] = new creditAccount(*cp);
else
temp[i] = new basicAccount(*accounts[i]);
}
for (int i=0;i<size;i++)
delete accounts[i];
delete[] accounts;
accounts = temp;
..
rest of the code
..
return 0;
}
I do not see any sense in your code. Let consider it from the very beginning.
So you allocated array accounts with 1 element. At the same time size is equal to 0.
int size = 0; // size of accounts
basicAccount **accounts;
accounts = new basicAccount*[size+1];
Then you fill this single element
switch(choice)
{
case 1:
..
accounts[size] = new basicAccount(ID,name);
..
break;
Now the nonsense starts.
size++; // enlarge the accounts + 1
basicAccount **temp;
temp = new basicAccount*[size];
You increase size and allocate array temp with the same number of elements as accounts has Only early you use size + 1 to allocate accounts and now you substitute size + 1[/b with equivalent expression [b]size++]
So you have two arrays with equal number of elements.
Thus there is no any sense to perform this operation.
But if the control is passed anew to the switch then you can overwrite memory because you are trying to allocate an alement outside acceptable range of the array [0, size - 1]
1 2 3 4 5 6 7 8
switch(choice)
{
case 1:
..
accounts[size] = new basicAccount(ID,name);
..
break;