5

Here is what I've copied from MSDN about new operator:

The new operator cannot be used to allocate a function, but it can be used to allocate pointers to functions. The following example allocates and then frees an array of seven pointers to functions that return integers.

int (**p) () = new (int (*[7]) ());
delete *p;

Well there is nothing strange with first line, it allocates an array of pointers to functions, but I just don't understand how the second deletes that array? I think it should be:

delete[] *p;

Can anyone explain this?

9
  • 6
    No, it should be delete[] p (no asterisk). Commented Nov 17, 2011 at 6:36
  • and how "delete *p;" is valid? that's what I can't figure out Commented Nov 17, 2011 at 6:40
  • @codekiddy, *p in this case is 0 (i.e. NULL), therefore delete *p is a no-op. If you had initialized the first element of the array to point to a function, the behavior of the program would have been undefined (it would have probably crash your program). Commented Nov 17, 2011 at 6:44
  • yeah I screwed up(don't know how to edit lol) sorry :/ Commented Nov 17, 2011 at 7:57
  • If you quote something, use the quote-formatting and provide a link to the source (I did that for you). Commented Nov 17, 2011 at 8:37

2 Answers 2

5

Frankly speaking the right answer was written in the avakar's comment. The right code is

delete[] p;

delete *p; is incorrect for two reasons:

  1. we must use delete[] for all dynamically allocated arrays. Using delete will cause an undefined behaviour.
  2. pointers to static and member functions cannot be deleted
Sign up to request clarification or add additional context in comments.

3 Comments

I think your second point is worded unluckily, although you're right that delete *p is wrong on two accounts. Pointers to <whatever> can certainly be deleted, supposed they were allocated. A pointer is, after all, just that, a pointer. However delete *p does not delete the pointer, but tries to delete the pointed-to-function.
Though admittedly my wording is not much better... since of course not the pointer is deleted, but what's pointed too. So probably "delete takes a pointer and *p isn't a pointer" is the wording that should be correct.
thanks for answer Dmitry, also big thanks to others for answering!
2

If we add a typedef,

typedef int (*FPtr)();

the new statement can be rewritten as

FPtr *p = new FPtr[7];

so this is obvious that the resource should be released with

delete[] p;

as explained by others.


BTW, the MSDN page for VS 2008 and above does use the correct code.

int (**p) () = new (int (*[7]) ());
delete [] p;

4 Comments

This might be because I added the link by googling, and thus might have linked a different version than the one the OP was quoting. My bad.
@codekiddy: It says delete[] in the 2008 US version.
this version doesn't: link
let's try again link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.