If I have 3 pointers to double :
rdf1 = new double [n];
rdf2 = new double [n];
rdf3 = new double [n];
I want to delete them with a single delete statement. something like :
delete [] rdf1,rdf2,rdf3;
What's the right way to do it ?
Unfortunately, this is syntactically correct:
delete [] rdf1,rdf2,rdf3;
More unfortunately, it doesn't do what you think it does. It treats ,
as a comma operator, thus eventually deleting only rdf1
(since operator delete
has precedence over operator ,
).
You have to write separate delete []
expressions to get the expected behavior.
delete [] rdf1;
delete [] rdf2;
delete [] rdf3;
To be fair, you can do it as a single statement, just not as a single invocation of the delete []
operator:
(delete [] rdf1, delete [] rdf2, delete [] rdf3);
But why in the world do you care whether it is one statement or three?
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