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
C library function - remove() The C library function int remove(const char *filename) deletes the given filename so that it is no longer accessible.
Syntax of delete operatordelete pointer_variable; // delete ptr; It deallocates memory for one element.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With