Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

The problem with stackoverflow.com

Name: Anonymous 2010-06-06 7:54

And most other programming communities, really. (♥ /prog/)

http://stackoverflow.com/questions/2425728/delete-operator-in-c/
Question:: What is the difference between delete and delete[] operators in C++?

stackoverflow.com answer:
The delete[] operator is used to delete arrays. The delete operator is used to delete non-array objects. It calls operator delete[] and operator delete function respectively to delete the memory that the array or non-array object occupied after (eventually) calling the destructors for the array's elements or the non-array object.

The following shows the relations:

typedef int array_type[1];

// create and destroy a int[1]
array_type *a = new array_type;
delete [] a;

// create and destroy an int
int *b = new int;
delete b;

// create and destroy an int[1]
int *c = new int[1];
delete[] c;

// create and destroy an int[1][2]
int (*d)[2] = new int[1][2];
delete [] d;


For a new that creates an array (so, either the new type[] or new applied to an array type construct), the Standard looks for a operator new[] in the array's element type class or in the global scope, and passes the amount of memory requested. It may request more than N * sizeof(ElementType) if it wants (for instance to store the number of elements, so it later when deleting knows how many destructor calls to done). If the class declares an operator new[] that additional to the amount of memory accepts another size_t, that second parameter will receive the number of elements allocated - it may use this for any purpose it wants (debugging, etc...).

For a new that creates a non-array object, it will look for a operator new in the element's class or in the global scope. It passes the amount of memory requested (exactly sizeof(T) always).

For a delete[], it looks into the arrays' element class type and calls their destructors. The operator delete[] function used is the one in the element type's class, or if there is none then in the global scope.

For a delete, if the pointer passed is a base class of the actual object's type, the base class must have a virtual destructor (otherwise, behavior is undefined). If it is not a base class, then the destructor of that class is called, and a operator delete in that class or the global operator delete is used. If a base class was passed, then the actual object type's destructor is called, and the operator delete found in that class is used, or if there is none, a global operator delete is called. If the operator delete in the class has a second parameter of type size_t, it will receive the number of elements to deallocate.


Proper answer: The delete[] operator is used to delete arrays. The delete operator is used to delete non-array objects. Look it up in the documentation/FAQ/textbook/google.

Name: Anonymous 2010-06-06 17:48

>>29
You're rather more upset and are just perpetuating this. I am rather enjoying your tactical avoidance. Let's begin, anyway.
Replacing whitespace
Oh, so it's about being easier to type now? What about being easier to make mistakes? See how this conversation began.

why [b][b]this[/b][/b] should make sense
I'm glad you picked [b]. Let's have a look at some CSS.
p.normal {font-weight:normal}
p.thick {font-weight:bold}
p.thicker {font-weight:900}

Every extra [b] could easily add 100 to the font-weight without using deprecated HTML such as <b>.

you might as well complain
You might as well twist it so that it appears that I am.

Xarn actually got off his ass and implemented something
He didn't really need to. BBCode works just fine. This just appears to be a hobby project.

Grow up.
This one's pure gold. Are you implying jealously? What attention would I get if I did something? If I wanted attention, I'd give myself a name and set up a blog documenting a load of FIOC toy programs I'd have written. But no, I'm completely anonymous. To a bystander, we could be the same person. You wouldn't be able to identify me in a different thread.

The amount of typing here has increased exponentially. It's also my bedtime.

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List