-6

I'm studing about how programs save data on their memory. So I made a simple program that contains a global double variable :

#include <iostream>
#include <conio.h>

using namespace std;

double b = 512;

int main(){
    getch();
    return 0;
}

when I want to search for this double variable in memory from any programs that read memory (in my case CheatEngine), I see something unclear.

CheatEngine Finds the 512 in memory :

1st Picture

When I convert it to hex it shows :

2nd Picture

And when I browse the location of this variable in memory , It is like :

3rd Picture

So I convert 512 from decimal to hex and it is 200, But there is nothing similar 200 in the second picture.

What is 4080000000000000 in the second picture and how it is equal to 512 ?

HazemGomaa
  • 1,620
  • 2
  • 14
  • 21
Mohammad Sina Karvandi
  • 1,064
  • 3
  • 25
  • 44

2 Answers2

9

4080000000000000 is the double representation of +512.0. double are represented with a sign, an exponent and a mantissa in memory.

       4   0   8         0     00       00       00       00       00        00
 0    100 00001000     0000 00000000 00000000 0000000 000000000 00000000 00000000
 ^    ------------     ----------------------------------------------------------
 |        
sign  exponent = 2^9   mantissa with implicit high bit = 1 (normal)

So the represented number is 2^9 * 1.0 = 512.0.

Note

  • (408)16 = 1032
  • exponent = 2^(1032 - 1023) = 2^9, where 1023 is the exponent bias
HazemGomaa
  • 1,620
  • 2
  • 14
  • 21
Franck
  • 1,635
  • 1
  • 8
  • 11
1

As pointed out by Jean-Francoise, Integer and floating point number representation in memory are different.

A double datatype is a 64-bit wide floating point number, meanwhile 0x200 is the integer representation of 512.

You can also use GDB to inspect the memory contents of your program, providing you enable debug information to be included in your program.

Jorge Bellon
  • 2,901
  • 15
  • 25