tags:

views:

423

answers:

8

Hi

On the 32-bit machine, why the size of a pointer is 32-bit? Why not 16-bit or 64-bit? What's the cons and pros?

+5  A: 

16 bit would obviously be insufficient - you could only address 64K then.

Why not emulate 64 bit on 32 bit systems - I guess because the performance of pointer arithmetic would degrade.

Developer Art
Well 64K actually. And it were good enough for me back when I programmed t' CP/M boxes using t' Z80 assembler. Nay, kids these days - don't know they are born.
anon
I'm not a kid - I had Z80 too!
Developer Art
But you seem to have forgotten how much memory a Z80 could address?
anon
Why would you go to the effort when you could not address 64bits worth of memory anyway.
tyranid
"32Kb of RAM. It was enough to go to the moon, and it was enough to play Zaxxon."
Paul Tomblin
@tyranid Had to do somat to pass t' time. Not much entertainment to be had in a hole in t' motorway, tha knows.
anon
I've programmed on embedded chips with 8bit addresses - the addresses were all relative, so you could hop around the few K of memory but you couldn't always get there from here !
Martin Beckett
@Martin, that's where you end up with JMPs that go to other JMPs, right?
Paul Tomblin
Weirder than that - the address of the next instruction was the result of the previous one. It was a Altera waveform/sequence generator.
Martin Beckett
I've got a Z80 -- in my Ti-84 calculator!
Billy ONeal
A: 

The size of the pointer has little to do with the architecture(32bit, 64bit). 32bit usually refers to the fact that the register size is 32bit. As a result, the maximum possible number of address that you can address using one register is 2^32. So, it boils down to efficiency of addressing the memory slots using a register.

AraK
Not just the memory slots. If that was the only reason, int pointers could just have different sizes. Also, the registers themselves are holding 32-bit values.
Brian
A: 

With a 32-bit pointer you can point to a wider range of memory than with 16-bit pointers. When 32-bit pointers were standardized, 64-bit CPUs were not very popular (or even existent?). Therefor a pointer would not be able to fit inside the CPU register, which is a very important factor for speed.

Ztyx
+17  A: 

Because it mimics the size of the actual "pointers" in assembler. On a machine with a 64 bit address bus, it will be 64 bits. In the old 6502, it was an 8 bit machine, but it had 16 bit address bus so that it could address 64K of memory. On most 32 bit machines, 32 bits were enough to address all the memory, so that's what the pointer size was in C++. I know that some of the early M68000 series chips only had a 24 bit memory address space, but it was addressed from a 32 bit register so even on those the pointer would be 32 bits.

In the bad old days of the 80286, it was worse - there was a 16 bit address register, and a 16 bit segment register. Some C++ compilers didn't hide that from you, and made you declare your pointers as near or far depending on whether you wanted to change the segment register. Mercifully, I've recycled most of those brain cells, so I forget if near pointers were 16 bits - but at the machine level, they would be.

Paul Tomblin
At last one answer that brings the hardware sizes into account. The language does not define the size of pointers, and it does not make any sense at all having a pointer size in the language differ from the actual address register size
David Rodríguez - dribeas
It's still there - Since the PentiumPro x86 has a 36bit address - it's just that Windows doesn't use it so nobody knows about it.
Martin Beckett
Actually, both Windows and Linux do use segmented memory in the kernel... but even device driver writers can mostly ignore that, because the kernel itself fits in 32 bits. This is (part of) the reason for copy_to_user and copy_from_user in Linux.
Andrew McGregor
Pentium Pro does *not* have 36 bit virtual addresses - PAE stands for *Physical* Address extension.This doesn't affect the pointer size in any language at all. If you are crazy enough wanting to use it, you need to map in the memory pages manually with an awkward API called AWE into your 32 bit virtual address space.
Axel Gneiting
+6  A: 

The size of a pointer in C++ is implementation-defined. C++ might run on anything from your toaster's chip up to huge mainframes. Different architectures require different sizes of the data types.

If on your implementation a pointer is 32bit, then that's very likely an architecture which can address 2^32 bytes. (Note that even the size of bytes might be different depending on the implementation.) 64bit architectures generally can address 2^64 bytes, so implementations on these architectures will likely have a pointer size of 64bit.

sbi
A: 

To answer your question: C++ itself says very little about the size of a pointer, and certainly not that it has to be 32 bits or anything. The size of a pointer should be the natural one for the machine architecture.

David Thornley
A: 

Why not 16-bit? Because, presuming a flat 32-bit address space, you cannot address every byte. Far from it: you can only address 216 unique locations with a 16-bit pointer. Even if your pointers only point to dwords and not bytes, this still leaves 1073676288 dwords unaddressable.

Assuming a flat 32-bit address space, you can already address every single byte with a 32-bit pointer. At this point, 64-bit pointers are just wasting space, unless you want to add additional information to each pointer. For example, on 32-bit PowerPC, a function descriptor is actually a 96-bit entity, with one third pointing to the executable code and the rest being data that helps make relocating modules easier.

In a segmented address space, having larger-than-32-bit pointers to data could be useful. Windows NT on the DEC Alpha was a 32-bit operating system, but the Alpha hardware was 64-bit capable. Your ordinary address space was still 32-bit, but there were special APIs to allow 32-bit programs to access 64-bit addresses, as if they were in otherwise-inaccessible segments.

ephemient
+2  A: 

As mentioned in many other answers, the size of a pointer need not be 32-bits - the implementation will set the size of a pointer to be whatever the architecture of the platform dictates. On a system with 64-bit addressing, the size of a pointer will generally be 64-bits.

However, you should also note that even on a single implementation, different types of pointers might have different sizes. In particular, pointer-to-member types (which I'll grant are odd-ball pointers) may have different sizes than plain-old pointers to objects.

The same is true about pointers to plain old functions - they might have a different size than pointers to objects (this applies to C as well as C++). However on modern desktop systems you'll usually find that pointers to functions are the same size as pointers to objects.

Here's a short example of fun with pointer-to-member-functions:

#include <stdio.h>

class A {};

class B {};

class VirtD: public virtual A, public virtual B {

public:

   virtual int Dfunc() { return 5; };

};

typedef int (VirtD::* Derived_mfp)();

int main()

{

   VirtD virtd;

   Derived_mfp mfp = &VirtD::Dfunc;

   printf( "sizeof( mfp) == %u\n", (unsigned int) sizeof( mfp));

}

Displays: sizeof( mfp) == 12 on MSVC.

Michael Burr