delete [] operator giving debug errors...

Started by
17 comments, last by Orpheum 22 years, 1 month ago
A quick question on delete[]...

I''ve always made it a habit of keeping track of the size of the array I create, and passing that size to the delete operator, like so:

delete [iSize] map;

Or something like that. No errors were given, does this actually make a difference?

Jonathan
Advertisement
Thanks for the hint with delete[]. Its funny but I have used the normal delete for years and never had any troubles with that. Now I looked a little bit closer in a reference manual and had to see that I was wrong all the time. Now I don't understand why it worked (even in release), I'm using VC++ 6.0.

Edited by - VolkerG on 5/4/00 1:06:18 PM
This thread has terribly confused me. Please tell me which one of the following is correct:

char *MyArray = new char[80];

delete [] MyArray;
delete[] MyArray;
delete MyArray[];
delete MyArray;

--------------------


You are not a real programmer until you end all your sentences with semicolons;

Yanroy@usa.com

Visit the ROAD Programming Website for more programming help.

--------------------

You are not a real programmer until you end all your sentences with semicolons; (c) 2000 ROAD Programming
You are unique. Just like everybody else.
"Mechanical engineers design weapons; civil engineers design targets."
"Sensitivity is adjustable, so you can set it to detect elephants and other small creatures." -- Product Description for a vibration sensor

Yanroy@usa.com

delete [] MyArray and delete[] MyArray are both the same and the correct method to deallocate the memory that MyArray points to.

delete MyArray[] doesn''t exist.

delete MyArray is incorrect in this case, because the memory was allocted with new char[..].

In short:
use delete if you allocate memory for a single instance. Example: string *str = new string("hello");

use delete[] if you allocate an array of instances. Example: string *str_array = new string[10];

Erik
Jonathan: It should work, but it''s an old syntax. C++ now keeps track of the arrays anyway, so it ignores the number you give it.


- null_pointer
Sabre Multimedia
OK i had the exact same problem. My solution was:

before i used:
char *str;
long size = 100
str = new char [ size ];
for (long i = 0; i <=size ;i++){str = 'F';};<br>delete [] str;<br><br>what is wrong?<br>i found out that it was the<br>str[size] = 'F'… by some reasone, you are not alowed to accses the last data in a arreya?<br><br>char[100] = char[0-99]//realy?<br><br>ewerything was solved, only by changing one line!<br><br>str = new char [ size + 1];<br><br>WOW :-)<br>I think the error acures when you create a 'new' arreya, and then write data to it outside the arreya, and then delete it!<br>But how could you guess that char p[10], dont give you accses to p[10] (at least not in pointers???? Im no expert, im only asuming!<br><br>New question:<br>can you accses p[10] in char p[10];? <br><br><SPAN CLASS=editedby>[edited by - uncutno on March 16, 2002 8:36:00 AM]</SPAN>
-Anders-Oredsson-Norway-
Argh!!

Of course not
when you pass 10 to your new operator
''10'' is the size of the array.
so if you count from first element:
p[0], p[1].. .. p[9]
if you count on your fingers
that make exactly 10 elements no more
no less.

LEGREG
quote:Original post by uncutno
char[100] = char[0-99]//realy?

Yes, really.
quote:
I think the error acures when you create a ''new'' arreya, and then write data to it outside the arreya, and then delete it!

The error happens when you write past the array bounds in debug mode, but that''s only checked when you try to delete the block.

I''ll explain what''s happening in the debug builds using MSVC. When you request a memory block of 10 bytes, the compiler actually allocates say 18 bytes, filled with this pattern:
FD FD FD FD CD CD CD CD CD CD CD CD CD CD FD FD FD FD
The pointer returned to you points to the first CD. You can look at this in your memory window and see this is correct. Note again that this only happens in debug mode.

The purpose of these fields are as follows:
- The first 4 FD bytes are for detecting damage before the block.
- The CDs are the allocated memory, and NOT initialized to 0 so you can tell easily if you''re using uninitialized memory.
- The last 4 FD bytes are for detecting damage after the block.

When you finally decide to delete this block, the compiler creates code to check that the first 4 bytes are still FD FD FD FD, and that the last 4 bytes are still FD FD FD FD. If they are not, you get the "damage before/after normal block" error message. This is to tell you that you have somehow written outside your allocated memory, and to give you an idea of approximately which memory was damaged. Usually, if you have damage after normal block, that means you''ve overrun array bounds on that block.

quote:
But how could you guess that char p[10], dont give you accses to p[10] (at least not in pointers???? Im no expert, im only asuming!

Because you know in C that arrays start at 0, not 1, and that the new and array commands give you the number of elements, not the upper index bound.

quote:
New question:
can you accses p[10] in char p[10];?

No. char p[10] gives you 10 chars:
p[0], p[1], ... p[9]. If you try to access p[10], you could potentially stomp on some other local variables because you''re outside your range.
Here's a really simple program to demonstrate that you have n elements, but address them from 0 to (n-1):

      #include <iostream>using namespace std;int main(){	int myArray[5];		memset(myArray, 0, sizeof(myArray));	myArray[5] = 69; // <-- wrong, outside of array	// get the number of array elements (will be 5 here)	int length = sizeof(myArray) / sizeof(myArray[0]);	for (int i = 0; i < length; i++)			cout << "Array " << i << " equals " << myArray[i] << endl;		cin.get();	return 0;}          

Notice two things here:
1) there are exactly *five* cout statements - i.e. you're getting five elements, as requested
2) you'll get an access violation when the program finishes (in debug mode). Change the "myArray[5] = 69;" to "myArray[4] = 69;" and that will vanish.

Alimonster

There are no stupid questions, but there are a lot of inquisitive idiots.

[edited by - Alimonster on March 16, 2002 6:57:24 PM]

This topic is closed to new replies.

Advertisement