Syntax of delete operator

B

bintom

Why is the [ ] while freeing dynamically allocated memory with the
delete operator before the pointer name and not after, as when using
the new operator ?

type* ptrName; // --> declaration of pointer
ptrName = new type [SIZE];
delete [] ptrName;
 
A

Alf P. Steinbach

* bintom:
Why is the [ ] while freeing dynamically allocated memory with the
delete operator before the pointer name and not after, as when using
the new operator ?

type* ptrName; // --> declaration of pointer
ptrName = new type [SIZE];
delete [] ptrName;

"type[SIZE]" is a type.

"ptrName[]" would be a syntactically invalid indexing expression (lacking the
index).

Consider:

typedef int* IntPtr;
IntPtr x[5];

x[3] = new int[20];
...
delete[] x[3];

versus in the last line having to write

delete x[3][]; // Looks like a double indexing.


Cheers, & hth.,

- Alf
 
B

bintom

Is there any reason why

delete x[];

wouldn't have worked? I'm trying to figure why Stroustrup opted for

delete []x;

instead.

Thanks in advance,
- Bintom
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top