deleting array elements seperately

Hello,

is it possible to replace a

delete[] myarray;

call with a

1
2
3
for(int i = 0; i < arraySize; i++) {
    delete myarray[i];
}


Thanks in advance!
As far as I know, no. Every new[] must be followed by a delete[], and every new must be followed by a delete.
Typically, this is what you'd use STL containers for.
If you add a reference operator to myarray[i], like delete &myarray[i], the code seems to be fine.
@The illusionist mirage: sure, ┬┐who cares for undefined behaviour?

@OP:
nolyc wrote:
You're trying to do X, and you thought of solution Y. So you're asking about solution Y, without even mentioning X. The problem is, there might be a better solution, but we can't know that unless you describe what X is.
Last edited on
Topic archived. No new replies allowed.