+ 2

Basic understanding of Dynamic Memory

This is the program I created for my basic understanding of Dynamic Memory allocation in C++. Based on the output displayed, I have few queries which I"ll ask in further post on this. https://code.sololearn.com/cFlQGcKrkmO0/?ref=app

6th Oct 2017, 10:41 AM
Sagar Sonavane
2 Answers
+ 2
1) Though the pointers "p" and "q" are separate pointers, why value displayed for "*p" and "*q" is same in "Part 1" of the output? 2) Check several "delete" statements used before "Part 3" display in the code logic. Try altering the sequence of these "delete" statements or comment out some of those, you can observe difference in the output generated for "Part 3". E.g. If "delete" for "p" is placed before "q", then "*q" displays same value as that displayed in "Part 2". But if "delete q" is before "delete p", then reset happens for both of them. Is there any technical explanation for such behavior? 3) If new memory request for some pointers is done after "delete" is done for them (like that done before "Part 5" display), is there any rule what values these pointers will point to? (It can be observed that when memory request is done for "q" and "t" before "Part 5" display logic, "*q" and "*t" displays same value as that of "*p", not that of "*s". Why?)
6th Oct 2017, 10:42 AM
Sagar Sonavane
+ 1
Thanks kurwius. The explanation on the link helped improve my understanding. It seems we can not trust values pointed by pointers once they are deleted as using values of pointers after deletion works in mysterious ways.
6th Oct 2017, 6:39 PM
Sagar Sonavane