delete of functions and void * accepted without warning by compiler

Program:

[code]extern "C" int sync();

void f() { delete sync; }[/code]

compiles fine.

This also compiles fine (CC -c s.cc +w2):

[code]void f(void *p) { delete p; }

void g(void *p) { delete [] p; }[/code]

gcc4 gives for first program:

t.cc: In function 'void f()':

t.cc:2: error: cannot delete a function. Only pointer-to-objects are valid arguments to 'delete'

For the second program it gives:

s.cc: In function 'void f(void*)':

s.cc:1: warning: deleting 'void*' is undefined

s.cc: In function 'void g(void*)':

s.cc:2: warning: deleting 'void*' is undefined

I am not a language lawyer, but gcc treatment seems better.

Some refactoring of our code had left a delete sync; in our code after the pointer sync had been removed. It was some work to find out why the program crashed.

[892 byte] By [willoch] at [2007-11-26 10:24:26]
# 1

According the the C++ Standard, the program results are undefined if you attempt to delete a pointer that was not obtained from a corresponding "new". The compiler is not required to detect errors.

But attempting to delete a function pointer can never be correct, so gcc is being more helpful than Sun C++ in telling you about it.

If you file a report at bugs.sun.com, you can track progress on getting this enhancement into the compiler. If you don't want to file the bug, let me know, and I'll file one myself.

clamage45 at 2007-7-7 2:26:30 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 2
But deleting a null function pointer is allowed so a compiler cannot reject the code unless it can prove that the pointer is in fact non-null.
sebor@roguewavecom at 2007-7-7 2:26:30 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 3
In this example, there is no question that the address being deleted is non-null and is a function.
clamage45 at 2007-7-7 2:26:30 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 4
Yes, in the first example it's a function.Btw., Mike Miller believes the intent for the code to be ill-formed and has opened an issue to correct the spec to that effect.
sebor@roguewavecom at 2007-7-7 2:26:30 > top of Java-index,Development Tools,Solaris and Linux Development Tools...
# 5
I have filed Change Request 6478260 for this issue, which should be visible at bugs.sun.com by tomorrow.
clamage45 at 2007-7-7 2:26:30 > top of Java-index,Development Tools,Solaris and Linux Development Tools...