Search code examples
c++pointersdouble32bit-64bit

How can a pointer to double that is 4 Bytes point to double that is 8 Bytes?


I have this naive question:

A double is 8 Bytes even on 32 bit machines, also long long, and we know that the pointer size on that implementation is just 4 Bytes. Because that has a relationship with the processor's register size. So a processor register must be able to address any data type.

Here is my code, run with compiler flag -m32:

std::cout << "size of double: " << sizeof(double) << '\n'; // 8
std::cout << "size of double*: " << sizeof(double*) << '\n'; // 4
  • So how can a pointer to double of 4 Bytes point to 8 Bytes (double object)?

  • On 64 bit systems the size of a pointer is 8 Bytes so it is OK. Does this mean double works more effectively on 64 bit systems than on 32 bit ones?


Solution

  • So how can a pointer to double of 4 Bytes point to 8 Bytes (double object)?

    Because the "pointer" is different from what's being "pointed to".

    Think about it: Your "pointer" can point to a double ... a float ... a char.

    Does this mean double works more effectively on 64 bit systems than on 32 bit ones?

    No. It merely means that the 64-bit machine can direct access a larger address space.

    Here's a good tutorial on pointers:

    Pointers in C Programming: What is Pointer, Types & Examples

    Here's a good article on "memory addressing":

    Virtual address spaces