0

Anyone explain the output of my code. Thank you

Please explain the output addresses of my code. I know that my code is a little faulty. But for me, the output addresses seem meaningless. https://code.sololearn.com/cZptrMSmmDRP/?ref=app

26th Jun 2021, 3:11 AM
Rishi
Rishi - avatar
5 Respuestas
+ 3
Looking at the addresses, it seems <ptr2> takes the address of memory block that was allocated for <ptr> earlier on, after <ptr> had been deallocated. I guess we understood that address of a deallocated pointer is no longer valid, so we can no longer trust its usability.
26th Jun 2021, 3:36 AM
Ipang
+ 1
Ipang and Aysha Simra , actually here, the differences between ptr2, ptr3 and ptr4 should be 4 bytes right? Because integers are 4 bytes long. But in my code, the difference is 20 bytes. Please explain this
27th Jun 2021, 2:41 AM
Rishi
Rishi - avatar
+ 1
Rishi, If you mean to ask why there is 0x20 byte distance between <ptr2>, <ptr3> and <ptr4>, then I can't quite explain why. For the record, 0x20 in hexadecimal is 32 in decimal, so distance there is 32 byte not 20. But if you print the address of the pointers; std::cout << &ptr2 << std::endl; std::cout << &ptr3 << std::endl; std::cout << &ptr4 << std::endl; Then you'll see there are 8 bytes distance between address of <ptr2>, <ptr3> and <ptr4>, that's the size of a pointer in 64bit machines. I hope somone more knowledgeable can enlighten us here.
27th Jun 2021, 5:06 AM
Ipang
+ 1
Sammy Raven oh okay, I'll try to understand it as I couldn't😅
27th Jun 2021, 3:48 PM
Rishi
Rishi - avatar
0
Aysha Simra what did you fix in my code? I can't find where the difference is
27th Jun 2021, 2:43 AM
Rishi
Rishi - avatar