delete pointers

This is a discussion on delete pointers within the C++ Programming forums, part of the General Programming Boards category; Hi, my question is what is wrong with this code?
Code:
class T;
int main(){
T *p = 0;
delete ...

If the type (class T) isn't complete, it will not have the destructor or overridden operators called on the object [although, technically, it is probably not at all defined what happens in this case - but most compilers will just treat it as a non-class destruction, just like if you use new + delete to allocate and destroy a plain data struct].

Actually, assigning a pointer of a class doesn't allow for operator overrides, since you can't actually override based on "pointer to type X is assigned a value" - you can override "X is assigned a value" [assuming X isn't a basic type].

Whether delete is overridden or not, it should cope with a 0 delete without doing anything wrong, and it shouldn't make any difference to this code. Delete of "0" is a valid case and should be allowed to pass through without doing anything.

"Conceptually wrong" doesn't mean the code will generate an error. It means "WTF were you thinking writing that code?". Why would you call delete on a pointer to an object that you haven't defined completely? Or that you know to be null?

"Conceptually wrong" doesn't mean the code will generate an error. It means "WTF were you thinking writing that code?". Why would you call delete on a pointer to an object that you haven't defined completely? Or that you know to be null?

True indeed. But then most examples provided in class, at job interviews, discussion forums, etc, are usually "distilled down" so that the code is very specifically doing what it needs to show a concept, but nothing beyond that. I take the above code as such - no, it's not something you would ever actually write, but throw in a few if's, for/while-loops and some "real code" in the middle - where some of those conditions may mean that the code passes all the way through without modifying p, and we have a real world scenario where this would happen.

By the way, I'm wondering if the original poster's "expert" is confusing delete with free - it is NOT valid to call free with a NULL value - it will attempt to free address zero, with usually immediate dire consequences.

By the way, I'm wondering if the original poster's "expert" is confusing delete with free - it is NOT valid to call free with a NULL value - it will attempt to free address zero, with usually immediate dire consequences.

It is perfectly valid to call free() with a null pointer, and the result is exactly the same as deleting a null pointer. See the specification of free(), 7.20.3.2/2 in the C99 standard. See also the specification of free() in the Single Unix Specification.

Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

It is perfectly valid to call free() with a null pointer, and the result is exactly the same as deleting a null pointer. See the specification of free(), 7.20.3.2/2 in the C99 standard. See also the specification of free() in the Single Unix Specification.

Maybe it's not valid in C90.

I don't know if it's simply my memory, but I'm 100% sure I've had problems trying to free a NULL-pointer. It may of course be pre-C90 even, since I generally try not to even get to the free when the pointer is NULL.

I had a quick look at google "man free" before I posted, the place I found didn't say anything [either direction] about using free with a NULL pointer, so I assumed that this was still a problem.

I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.

It is perfectly valid to call free() with a null pointer, and the result is exactly the same as deleting a null pointer. See the specification of free(), 7.20.3.2/2 in the C99 standard. See also the specification of free() in the Single Unix Specification.

Perfectly valid in the spec maybe, but this is one of those cases where you can't really rely on the spec. Using C++ delete on a null pointer is safe, but calling C free() on a NULL pointer crashes on about half the machines I have access to, specifications notwithstanding.