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
7 Antworten
+ 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.
+ 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
+ 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.
+ 1
Sammy Raven oh okay, I'll try to understand it as I couldn't😅
0
Aysha Simra what did you fix in my code? I can't find where the difference is