views:

390

answers:

7

After I do, say

Foo* array = new Foo[N];

I've always deleted it this way

delete[] array;

However, sometimes I've seen it this way:

delete[N] array;

As it seems to compile and work (at least in msvc2005), I wonder: What is the right way to do it? Why does it compile the other way, then?

+5  A: 

delete[] array; is the correct form.

laura
+4  A: 

The right way is to do delete[] array;. I didn't even know delete[N] array; would compile (and I doubt it should).

sbi
It compiles on VC9 with a `warning C4208: nonstandard extension used : delete [exp] - exp evaluated but ignored` I don't what that means though
Naveen
It means the code is (1) not valid C++, and (2) the expression `N` is evaluated but ignored. That's fairly irrelevant, but it describes what happens if you wrote `delete[N++] array;` - N is first incremented, and then ignored.
MSalters
Really, it is first ignored and then incremented :)
Jaime Pardos
+5  A: 

Whether second case is right or not I'd recommend to use first one as it less error prone.

Visual Studio compiles a lot of things it shouldn't.

Mykola Golubyev
"Visual Studio compiles a lot of things it shouldn't." Amen.
sbi
+8  A: 

delete [N] array is invalid. It's not defined in the C++ standard: section 5.3.5 defines a delete expression as either delete expr or delete [] expr, and nothing else. It doesn't compile on gcc (version 4.1.2). As to why it compiles in Visual C++: ask Microsoft.

Mike Seymour
+11  A: 

You can check this MSDN link: delete[N] operator. The value is ignored.

EDIT I tried this sample code on VC9:

int test()
{
    std::cout<<"Test!!\n";
    return 10;
}

int main()
{
    int* p = new int[10];
    delete[test()] p;
    return 0;
};

Output is: Test!!

So the expression is evaluated but the return value is ignored. I am surprised to say the least, I can't think of a scenario why this is required.

Naveen
+1. useful info
Vivek Sharma
does it mean you can delete[2N] array; and that would compile
Vivek Sharma
Thank you, this is what I was looking for.
Jaime Pardos
My guess would be that this syntax was valid in a pre-standard dialect of C++, so the "evaluate and ignore" behaviour would be needed for a hypothetical bit of ancient code that depends on a side-effect of the expression. I'm more surprised that support for this syntax would be enabled by default in any modern compiler.
Mike Seymour
A: 

if it works, it's a nonstandard extension.

delete [] array;

is the correct form.

delete array;

will sometimes also work but is implementation dependent (thus wrong).

Nicholaz
The behavior of the second form (without the brackets) is clearly defined for non-array pointers. Its behavior for arrays is not. Anyway, I guess that's the reason why somebody voted you down.
RaphaelSP
`delete array` doesn't "work" in any sense of the word, unless you mean it doesn't crash the program. It won't reclaim all the memory allocated in the first place, and if it doesn't crash it will only run the destructor on the first object in the array. (Which is also an error if you did `new Foo[0]`.)
Bill
@Bill: I wish I could down-vote comments. Your statement might be true sometimes and at other times it might not. See here: http://stackoverflow.com/questions/1553382/1553407#1553407 and my comment here: http://stackoverflow.com/questions/1553382/1553391#1553391
sbi
@Bill: delete array; (without brackets) does delete all elements on *some* implementations (that's what implementation dependent means) and since the example in the question is about int types there are no destructors to be called.
Nicholaz
@sbi - I think you and I have different practical definitions of "work", but I see your point. @Nicholaz - Fair enough, I didn't realize that some implementations did this.
Bill
+3  A: 

First point: there's almost never a good reason to use the array form of new or delete to start with -- use std::vector (or some other container) instead.

Second: back in the dark ages of C++, you had to specify the size of the array you were deleting, so if you used x = new T[N], the matching delete was delete [N] x. The requirement to explicitly specify the size was removed long ago, but some compilers (especially those that care about backward compatibility with ancient code) still allow it.

Unless you really need to remain compatible with an ancient compiler (one that's 20 years old or so) you shouldn't use it. Then again, unless you need to remain compatible with a compiler so old it doesn't support any standard containers, you shouldn't be using the array form of new or delete in the first place. Just stop!

Jerry Coffin
Thanks for the history lesson.Then, if I had to pass a dynamic array of, say, floats, to a third party libray, which would be The Right Way to do it wih standard containers?
Jaime Pardos
Jerry Coffin
+1: In the sep '94 draft of the standard the syntax was already 'delete[]'. And in the same year, Design and Evolution (p218) had the comment that this syntax "... proved too error-prone, so the burden of keeping track of the number of elements was placed on the implementation instead."
Richard Corden