what is the C++ code doing? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

what is the C++ code doing?

int* ptr = new int(27); *ptr = 34; ... delete ptr; so 1. ptr is a pointer with a value 27 2. value at ptr is changed to 34 3. delete ptr

4th Jul 2018, 11:33 AM
Antony O. Onyango
Antony O. Onyango - avatar
2 Answers
+ 7
You're right. If you write cout << ptr; after the delete ptr; instruction, you'll get the place of the pointer in the memory, like 0x681640, or 0x351640 (check this code: https://code.sololearn.com/cZb0fb74VU11/?ref=app).
4th Jul 2018, 12:23 PM
Mattéo
+ 2
yeee! ... I'm abit confused about this part *ptr=34; I thought it's changing value at ptr to 34.when I do cout << *ptr <<endln; the value is 8327888
5th Jul 2018, 7:29 AM
Antony O. Onyango
Antony O. Onyango - avatar