views:

322

answers:

8

Are char*, int*, long* or even long long* of same size (on a given platform)?

Thanks

+6  A: 

Not necessarily. The standard does not guarantee sizeof(double*) == sizeof(int*). For instance, assume a processor with two address buses with different widths (like on some Harvard architecture processors) it could have pointers with different sizes.

Mehrdad Afshari
Wow, really? I didn't realize that.
JaredPar
However, note that a void * has to be able to hold any data pointer.
David Thornley
Hmmm. Then why can a void* point to anything, and be properly dereferenced? After all, no information is carried along with a pointer.
xcramps
`void*` cannot be dereferenced directly - you have to cast it back to the original type. Presumably, even if representation of pointers actually differs between types, the original representation will be restored when you cast back.
Pavel Minaev
WRONG. The size of the pointer is not the same thing as the size of the block of memory the pointer "points" to...
Charles Bretana
Charles, you misunderstand what is talked about. It has nothing to do with size of `int` and `double` (it could be any other two types). The point is that sizes of _pointers_ themselves can differ.
Pavel Minaev
@Pavel Minaev... obviously. "Properly dereferenced" means just what it says.
xcramps
@Pavel, sorry, you are obviously correct, I read sizeof() and assumed you were measuring the size of the object the pointer is pointing to.... As I have never seen an use of the sizeof operator on a pointer, it would seem to make no sense to me to do that...)
Charles Bretana
+3  A: 

There is no such guarantee in either C or C++ ISO standards, but in practice, I've yet to see a platform where this doesn't hold.

Note that regardless of this, reinterpret_cast'ing one pointer to another will more often than not lead to U.B., with a few exceptions (void*, and unsigned char* for PODs). So would any union tricks. So the obvious question is: why would you care?

Pavel Minaev
+1  A: 

Generally yes, All pointers to anything, whether they point to a int or a long or a string or an array of strings or a function, point to a single memory address, which is the same size on a machine. That is because the Processer on a machine has a an address register that these pointers are loaded into, and the size of that address register controls the size of the pointers.

The only exception might be in cases like old Intel 8088 16-bit machines where there was a two step process to determine the memory address, involving a 16 bit segment pointer, (which identified a 64K block of memory within the 1MByte address space), and then a second 16 bit memory address to identify the specific memory address within that segment. These two 16 bit addresses were then combined to get the complete 20 bit memory address. In that scenario, then, I imagine it might be possible to distinquiah between the individual 16 bit addresses, and the combined 20-bit address.

Charles Bretana
There's no requirement for a C/C++ implementation to actually store a raw memory address in the storage location of the pointer. It can be a handle, for example (e.g. to implement pointer bounds checking).
Pavel Minaev
@Pavel, I confess that I'm not a C++ person, and I only know about Handles periphally, but as a Handle is a type of pointer, than I would venture that it has to be the same size as all other pointers on the machine.
Charles Bretana
A handle is not a type of pointer. A handle is an opaque identifier. It can be absolutely anything - for example, an index into some array elsewhere.
Pavel Minaev
Especially true of member function pointers. I got schooled on this topic recently: there is no guarantee a pointer is actually an address. It's just the best representation on most systems we use.
quark
A: 

In the protected mode DOS days, a function pointer and a data pointer could have different lengths because data could be in a different section.

Blindy
+1  A: 

When programming x86 real mode with Watcom C you could have a mixed memory model using 16-bit near pointers and 32 bit far pointers.

Karl Voigtland
+15  A: 

They're not guaranteed to be the same size, although on the platforms I have experience with they usually are.

6.2.5.27:

A pointer to void shall have the same representation and alignment requirements as a pointer to a character type.39) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.

John Bode
+1 for a reference to the Standard that really nails it down and covers all edge cases.
Pavel Minaev
A: 

On 16-bit embedded processors which have banked (paged) RAM and/or flash, the use of pages may lead to pointers being different sizes - though this is independent of the size of the data they point to.

For example, on Freescale's HCS12 processor which has banked flash, data pointers are all 16 bits.

However, function pointers are 16 bits for near pointers (for code in the same page as the calling function, or in unbanked flash), or 24 bits for far pointers (for code in a different page), incorporating the page number in the address.

Things are complicated if you want to store constant data in paged flash, as due to the limitation on data pointer size, the function using the data has to be in the same page as the data being accessed.

It is conceivable that a 16-bit processor with banked RAM would likewise have different sizes for near and far data pointers.

Steve Melnikoff
+2  A: 

Note what the C standard says - as quoted by John Bode. Note, too, that the C standard says nothing about the sizes of pointers to functions at all.

The POSIX standard lays down some extra requirements:

2.12.3 Pointer Types

All function pointer types shall have the same representation as the type pointer to void. Conversion of a function pointer to void * shall not alter the representation. A void * value resulting from such a conversion can be converted back to the original function pointer type, using an explicit cast, without loss of information.

Note: The ISO C standard does not require this, but it is required for POSIX conformance.

Jonathan Leffler