Advertisement
Guest User

Untitled

a guest
Jun 27th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. You declare:
  2.  
  3. double d;
  4.  
  5. In memory :-
  6.  
  7. Address: 1234-00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F
  8. Data: CC CC CC CC CC CC CC CC CC CC CC CC CC CC CC
  9. ^
  10. |
  11. d points to here
  12.  
  13. A double, on a PC, in 32bit Windows, normally, is 8 bytes long
  14.  
  15. In debug mode, the memory is initialised to CC, both for the memory for the double, and immediately before and after.
  16.  
  17. When d goes out of scope (at the end of your function), _in debug mode_, a check is made on the memory immediately before and after (so addresses 00 and 01, and 0A and 0B - not sure exactly how many bytes are checked, could be more, could be less), that they still contain CC.
  18.  
  19. If they don't, then you've been bad and written over memory you weren't supposed to.
  20.  
  21. Like
  22.  
  23. *(&d - 1) = 0;
  24.  
  25. Whoops, you've just written over the 8 bytes _before_ those you were allocated. In release mode, it might work, it might wipe your files or kill your dog. In debug mode (at least with Visual C++), it will notice and warn you.
  26.  
  27. So, why is -9.2559631349317831e+061 the magic number, because that's CC CC CC CC CC CC CC CC as a double.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement