Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ delete syntax

Tags:

c++

I came across this rather unusual usage of 'delete'. Just wanted to know if the following line deletes both pointers or only the first?

delete ptr1, ptr2 
like image 704
source.rar Avatar asked Jun 14 '10 13:06

source.rar


People also ask

What is remove () in C?

C library function - remove() The C library function int remove(const char *filename) deletes the given filename so that it is no longer accessible.

What is the syntax of delete operator?

Syntax of delete operatordelete pointer_variable; // delete ptr; It deallocates memory for one element.

Does C have Delete function?

There's no new / delete expression in C. The closest equivalent are the malloc and free functions, if you ignore the constructors/destructors and type safety.

When to use delete [] and delete?

delete is used for one single pointer and delete[] is used for deleting an array through a pointer. This might help you to understand better.


1 Answers

This is undoubtedly an error. The comma here is the comma operator, not a separator. Only the first pointer, ptr1 is deleted.

The second pointer, ptr2, is just a do-nothing expression.

The delete operator has higher precedence than the , operator, so the expression is parsed as if it were written:

(delete ptr1) , (ptr2) 

and not as if it were written:

delete (ptr1 , ptr2) 

If , had higher precedence than delete, then only the second pointer would be deleted.

like image 99
James McNellis Avatar answered Oct 04 '22 06:10

James McNellis