2

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?

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Itachi Uchiwa
  • 3,044
  • 12
  • 26
  • 8
    A pointer contains the memory address of something else. The size of something else doesn't change its address or the ability for a pointer of any size to point to it. – Retired Ninja Aug 29 '21 at 22:16
  • 11
    As an analogy: an address of a house is not as big as the house itself – Lala5th Aug 29 '21 at 22:18
  • 5
    @Lala5th Perhaps [Taumatawhakatangihangakoauauotamateaturipukakapikimaungahoronukupokaiwhenuakitanatahu](https://en.wikipedia.org/wiki/List_of_long_place_names) might not fit on a small house? – chux - Reinstate Monica Aug 29 '21 at 22:20
  • 2
    Does this answer your question? [Why is sizeof(int) different than sizeof(int\*)?](https://stackoverflow.com/questions/10984659/why-is-sizeofint-different-than-sizeofint) – kjhayes Aug 29 '21 at 22:21
  • @chux-ReinstateMonica But 32 bit pointers don't fit in `char` either ;) – Lala5th Aug 29 '21 at 22:21
  • @Lala5th Unless the `char` is 32 or 64-bit. C does support such rare machines. – chux - Reinstate Monica Aug 29 '21 at 22:25
  • 2
    @Lala5th • sizeof(char) will = 1 and can be 64-bit on an architecture where each byte is 64-bit. – Eljay Aug 29 '21 at 22:33

1 Answers1

5

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
paulsm4
  • 114,292
  • 17
  • 138
  • 190
  • "A: No. It merely means that the 64-bit machine can direct access a larger address space." Does it mean that direct access is more effective than on 32 bit implementations? – Itachi Uchiwa Aug 29 '21 at 22:27
  • 2
    Please read the articles I cited. Short answer: No. An 8-bit CPU can address at most 256 bytes of memory at a time. The earliest PCs were 8-bit; they could only accommodate tiny programs. The original IBM PC was 16-bit, and supported "segments". It allowed DOS programs to use a entire megabyte of memory. Today's processors can accommodate gigabytes - or exabytes of memory. That's the difference... – paulsm4 Aug 29 '21 at 22:32
  • @paulsm4 most 8-bit MCUs can address **more than 256 bytes of memory**, at least in the instruction space [How can 8-bit processor support more than 256 bytes of RAM?](https://electronics.stackexchange.com/q/57950/27052) – phuclv Aug 30 '21 at 01:07
  • 2
    @phuclv - I was trying not to "overcomplicate" the answer. But you're correct. The "early PCs" I mentioned (for example, Apple II and Commodore PET) used 8-bit Motorola 6502 processors. But they had a 16-bit address bus, hence could access 64K of RAM. Different "address modes" allowed the CPU to support 16 bit addressing. Look [here](https://en.wikipedia.org/wiki/MOS_Technology_6502#Addressing) or [here](https://people.cs.umass.edu/~verts/cmpsci201/spr_2004/Lecture_02_2004-01-30_The_6502_processor.pdf) for details. – paulsm4 Aug 30 '21 at 01:18