Help with heap corruption

Hey,

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;
....
....
....

}
Why do you think that size is correct in the following snip of code

1
2
	for (int i=0;i<size;i++)
		delete accounts[i]; //delete all cells (size is correct) 


As it follows from the beginning of your code size was increased

size++;

So maybe in the loop above you should use size - 1
Last edited on
Because, accounts[size-1] contains the last account that was added...
so i need to delete it also before delete[] accounts.

And besides that... the first iterate works fine...
also in the second iterate everything works fine until
delete[] accounts...

and i'm still don't understand what it's trying to write to the heap...



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?
Last edited on
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.
Last edited on
Ok i'm sorry for the confusion
the code looks like this:

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
int main()
{
	int size = 0; // size of accounts
	basicAccount **accounts;
	accounts = new basicAccount*[size+1];
...
...
code goes on
...
...

then i have a switch for 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);
			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[] accounts;

		accounts = temp;

..
rest of the code
..

return 0;
}


hope it's more clear now...
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;


So in any case this code is senseless.

Last edited on
Wow.. you are so right...

I feel so ashamed.. =\

I took basicAccount[size+1] as if size = 1 ... so stupid...

thank you very much!!!

the solve was just declared size as 1 (size = 1)
and then everywhere change size to size-1 ...


thanks again for enlighten me.
Topic archived. No new replies allowed.