if we do the following:
delete [] a;
it will delete the array after returning the address ‘xyz’. and if we later try to output the value b[0], we will get garbage.
is this correct?
Consequence of deleting 'a' at the end of fun()
Yes , if before deleting you store the address of a into another pointer say b, and delete a, then the values holded by b will be random (garbage).
But after the return statement will we not exit the function what sense it will make to write delete [] a after it writing return a?
Yes it makes no sense as lines written after return never executes.
So if you want to delete array, you should do it before return statement.
I hope I’ve cleared your doubt. I ask you to please rate your experience here
Your feedback is very important. It helps us improve our platform and hence provide you
the learning experience you deserve.
On the off chance, you still have some questions or not find the answers satisfactory, you may reopen
the doubt.