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?
delete is not a statement, it's an operator (which is part of the delete-expression). The parentheses are redundant though.No it is not the right way. You have to call delete [] on each of the pointers separately.
The standard form of operator delete[] will take only one parameter.
delete [] rdf1;
delete [] rdf2;
delete [] rdf3;
I always follow one principle that the code I write should be easily understandable by one who works on it after me. So rather than doing this with any fancy constructs I would do it the more commonly known way(which is above).
std::vector<double> rdf1(n), rdf2(n), rdf3(n);