Click to See Complete Forum and Search --> : delete [] problems
billfor
June 28th, 2002, 10:34 AM
My application is crashing when I try and delete an array I created.
I can't figure out why? Can anyone help?? See lines 5 and 13 below.
Also at line 4 should I initialize my pointer to null?
1 bool CNameDialog::OnPerformSelect(void)
2 {
3 bool Result=true;
4 int *iArrayOfIndex;
5 iArrayOfIndex = new int[iCount];
6 Result=CheckName();
7 if(Result)
8 {
9 // Use iArrayOfIndex as an array
10
11 }
12
13 delete [] iArrayOfIndex;
14 return Result;
15}
JMS
June 28th, 2002, 10:50 AM
Where does iCount get set?
Your problem isn't in this code that I can see. Your problem is almost certainly that you are walking on memory. Make sure that you're not writeing past the allocated memory buffer.
One thing I would try is to take away the square brackets on the delete statement. VC++ 5 required you to do that but I don't believe 6 or .net does. Could be wrong but it's worth a shot.
Like I said your issue is that your walking on memory. The way to find this issue is to memset your entire array to '/0' and then to watch the last byte as you step through your code. See where it gets written.
zdf
June 28th, 2002, 11:02 AM
I hope this helps:
int main()
{
const int c = 2;
int* p = new int[c];
for ( int i = 0; i <= c; i++ ) // trouble: <= instead of <
p[i] = i;
delete[] p; // crash: origin: <=
return 0;
}
PaulWendt
June 28th, 2002, 12:47 PM
Despite what one of the other posters above said, don't remove the square brackets on the delete statement. When you're allocating an array, you must call delete [] and not delete. I can never remember the exact reason and I'm sure I'll be corrected, but I seem to remember that if you leave off the [], the destructor for ONLY the first item in the array will get called.
--Paul
Paul McKenzie
June 28th, 2002, 03:28 PM
Originally posted by PaulWendt
Despite what one of the other posters above said, don't remove the square brackets on the delete statement. When you're allocating an array, you must call delete [] and not delete. I can never remember the exact reason and I'm sure I'll be corrected, but I seem to remember that if you leave off the [], the destructor for ONLY the first item in the array will get called.
--Paul The only reason is that using delete on something allocated with operator new [] (and vice-versa) leads to undefined behavior. What the behavior is, is up to the compiler. It could do a whole host of things from working flawlessly, to crashing your program. Therefore you never mix them up:
new --> delete
new[] --> delete[]
Regards,
Paul McKenzie
Paul McKenzie
June 28th, 2002, 03:38 PM
Originally posted by JMS
One thing I would try is to take away the square brackets on the delete statement. VC++ 5 required you to do that but I don't believe 6 or .net does. Could be wrong but it's worth a shot.
No, never do this. It is not a matter of a compiler requiring it or not. If you use new[] to allocate memory, you always use delete[], regardless of what may or may not work, or what compiler you're using. That is the rule of C++ dynamic memory management. Anything else, and you are invoking undefined behavior.
Regards,
Paul McKenzie
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.