Sizeof(pointer)
1. The logic
the primary determinant of the value of sizeof(pointer) is the memory address width of the target architecture your program is compiled for.
In simpler terms: A pointer must be large enough to hold the largest possible memory address that the system can access.
The size of a pointer have nothing to do with the type of the pointer
-
32-bit Architecture: The CPU can "see" up to pow(2,32) bytes of RAM (approx. 4 Gigabytes). To write down a number that high, you need 32 bits (4 bytes).
-
64-bit Architecture: The CPU can "see" up to pow(2,64) bytes of RAM (approx. 18 Exabytes). To write down a number that massive, 32 bits is not enough. You need 64 bits (8 bytes).
2. The Critical Distinction: Hardware vs. Compiler
It is important to note that sizeof(pointer) is determined by how you compile the code, not just the physical computer you are holding.
-
Scenario A: You have a modern 64-bit computer. You compile your C code specifically for a 64-bit target.
-
sizeof(pointer) = 8 bytes.
-
Maximum RAM access: Huge (Exabytes).
-
-
Scenario B: You have that same 64-bit computer, but you compile your C code in "32-bit compatibility mode" (e.g., using the -m32 flag in GCC or selecting "x86" in Visual Studio).
-
sizeof(pointer) = 4 bytes.
-
Maximum RAM access: Limited to 4GB.
-
The Operating System and the Compiler agree on a "Data Model" (often called the ABI - Application Binary Interface). This model tells the program: "We are going to pretend the world is only 32-bits wide," or "We are going to use the full 64-bit width."
3. More
The 32-bit Limit
A pointer is just a variable storing a number.
If a pointer is 4 bytes, that is 32 bits (pow(2,32) = 4,294,967,296).
This means a 4-byte pointer can refer to byte #0 up to byte #4,294,967,295. (The computer assigns one address to one Byte of RAM.) This equals exactly 4 GB of RAM. This is why older computers (XP/Vista era) could not use more than 4GB of RAM. The pointers physically ran out of numbers.
The 64-bit Expansion
If a pointer is 8 bytes, it is astronomically large. It allows the pointer to address an amount of RAM so large we don't even have physical hardware for it yet.(16 Exabytes (16 billion Gigabytes)) [[32-bit & 64-bit | Learn more about 32 vs 64-bit systems]]