tags:

views:

2481

answers:

13

For example: sizeof(char*) returns 4. As does int*, long long*, everything that I've tried. Are there any exceptions to this?

+10  A: 

if you are compiling for a 64-bit machine, then it will be 8.

FryGuy
+5  A: 

From what I recall, it's based on the size of a memory address. So on a system with a 32-bit address scheme, sizeof will return 4, since that's 4 bytes.

Will Mc
There is no such requirement. There is not even a requirement that sizeof(unsigned int) == sizeof(signed int). The size of a pointer to an int will always be, by definition, sizeof(int *), to a char sizeof(char *) etc. Relying on any other assumption is a bad idea for portability.
Mihai Limbășan
Ah, I see now. Thanks for the info.
Will Mc
Could still return 2, if CHAR_BIT is 16. sizeof() counts in number of chars, not octets.
MSalters
+1  A: 

The reason the size of your pointer is 4 bytes is because you are compiling for a 32-bit architecture. As FryGuy pointed out, on a 64-bit architecture you would see 8.

Will Bickford
+2  A: 

In general, sizeof(pretty much anything) will change when you compile on different platforms. On a 32 bit platform, pointers are always the same size. On other platforms (64 bit being the obvious example) this can change.

Sean Reilly
+1  A: 

A pointer is just a container for an address. On a 32 bit machine, your address range is 32 bits, so a pointer will always be 4 bytes. On a 64 bit machine were you have an address range of 64 bits, a pointer will be 8 bytes.

Ed Swangren
On a 32-bit machine with 32-bit bytes, sizeof(char *) could be 1.
Robert Gamble
"...with 32-bit bytes". I didn't know such things existed...fancy that.
Ed Swangren
On a 32 bit duck, sizeof(char *) returns PI
Adriano Varoli Piazza
+27  A: 

The guarantee you get is that sizeof(char) == 1. There are no other guarantees, including no guarantee that sizeof(int *) == sizeof(double *).

In practice, pointers will be size 2 on a 16-bit system (if you can find one), 4 on a 32-bit system, and 8 on a 64-bit system, but there's nothing to be gained in relying on a given size.

David Thornley
And 3 bytes on a 24-bit system. Yes, I've worked on one. Welcome to the world of embedded devices.
dwj
I've worked on 16-bit systems with 20-bit pointers as well. I should go see what sizeof returns in that case...
Judge Maygarden
@monjardin: IIRC, the 8086 was like that. There was a 16 bit address and a 4 bit segment register. I believe a normal "NEAR" pointer was 16 bits and a pointer declared as "FAR" was more, probably 24, though I'm not sure.
rmeador
I worked on a (32-bit) system where the 'char *' to something had a different bit representation to the 'anthing_else *' to the same memory location. You learned to ensure that 'malloc()' was declared (because in those days of yore, malloc() returned 'char *' and not 'void *'). Beware!
Jonathan Leffler
(cont): The size of the pointers was the same in all cases; it was just the bit pattern that differed. I've not seen systems where pointers to different data types have different sizes. I have seen systems where code (function) pointers are different sizes from data pointers.
Jonathan Leffler
In certain memory models, a pointer on an 8086 was 32 bits. A 16-bit segment and a 16-bit offset. The processor would combine these by shifting the segment 4 bits to the left and adding the offset, resulting in a 20-bit address. Two different pointers could point to the same physical address! fun!
Ferruccio
16 bit systems are easy to find. Any code that runs in THUMB mode on an ARM chip.
ApplePieIsGood
Sorry, @ApplePieIsGood: ARM THUMB code is still 32-bit code. It's just a technique to compress the instruction size from 32-bits to 16-bits, but the address space is still the same.
Ben Combee
another guarantee is that sizeof(char*) == sizeof(void*) , because they have to have the same representationss (object [size] and value [set of bits relevant for their value] representation)
Johannes Schaub - litb
Ferruccio: It's not old. My Core 2 Quad processor also supports real mode ;-P
Mehrdad Afshari
Of course if you have a pointer to a member function of a class that uses multiple or virtual inheritance, then you'll likely get completely different pointer sizes.
Eclipse
itanium sometimes uses 2word (2 pointers) for representing 1 function pointer.
osgx
+1  A: 

No, the size of a pointer may vary depending on the architecture. There are numerous exceptions.

Judge Maygarden
+2  A: 

In addition to the 16/32/64 bit differences even odder things can occur.

There have been machines where sizeof(int *) will be one value, probably 4 but where sizeof(char *) is larger. Machines that naturally address words instead of bytes have to "augment" character pointers to specify what portion of the word you really want in order to properly implement the C/C++ standard.

This is now very unusual as hardware designers have learned the value of byte addressability.

Darron
A: 

It will always be equal to sizeof size_t.

On 32-bit machines this tends to be 4. On 64-bit machines this tends to be 8.

Max
http://www.embedded.com/columns/programmingpointers/200900195Thanks for downmodding me.
Max
size_t is tied to the size of the _biggest possible single object_. char* must be able to address _all bytes in all objects_. So, size_t can be 2 byte (objects <64KB) even if char* is 4 byte (sufficient for e.g. 65536 objects of 65535 bytes each)
MSalters
MSalters, you are right: sort of. On closer inspection, the only guarantee is that size_t can address any point in memory. Thus it will always be equal to or greater than sizeof(char *). I will leave my answer up for now so others may learn from it.
Max
+7  A: 

Just another exception to the already posted list. On 32-bit platforms, pointers can take 6, not 4, bytes:

#include <stdio.h>
#include <stdlib.h>

int main() {
    char far* ptr; // note that this is a far pointer
    printf( "%d\n", sizeof( ptr));
    return EXIT_SUCCESS;
}

If you compile this program with Open Watcom and run it, you'll get 6, because far pointers that it supports consist of 32-bit offset and 16-bit segment values

dmityugov
Good example! +1
osgx
+1  A: 

In addition to what people have said about 64-bit (or whatever) systems, there are other kinds of pointer than pointer-to-object.

A pointer-to-member might be almost any size, depending how they're implemented by your compiler: they aren't necessarily even all the same size. Try a pointer-to-member of a POD class, and then a pointer-to-member inherited from one of the base classes of a class with multiple bases. What fun.

Steve Jessop
+5  A: 

Technically speaking, the C standard only guarantees that sizeof(char) == 1, and the rest is up to the implementation. But on modern x86 architectures (e.g. Intel/AMD chips) it's fairly predictable.

You've probably heard processors described as being 16-bit, 32-bit, 64-bit, etc. This usually means that the processor uses N-bits for integers. Since pointers store memory addresses, and memory addresses are integers, this effectively tells you how many bits are going to be used for pointers. sizeof is usually measured in bytes, so code compiled for 32-bit processors will report the size of pointers to be 4 (32 bits / 8 bits per byte), and code for 64-bit processors will report the size of pointers to be 8 (64 bits / 8 bits per byte). This is where the limitation of 4GB of RAM for 32-bit processors comes from -- if each memory address corresponds to a byte, to address more memory you need integers larger than 32-bits.

Joseph Garvin
+3  A: 

Even on a plain x86 32 bit platform, you can get a variety of pointer sizes, try this out for an example:

struct A {};

struct B : virtual public A {};

struct C {};

struct D : public A, public C {};

int main()
{
    cout << "A:" << sizeof(void (A::*)()) << endl;
    cout << "B:" << sizeof(void (B::*)()) << endl;
    cout << "D:" << sizeof(void (D::*)()) << endl;
}

Under Visual C++ 2008, I get 4, 12 and 8 for the sizes of the pointers-to-member-function.

Raymond Chen talked about this here.

Eclipse
Pointers to member functions are a real pain. It is unfortunate that not all compilers does it like the Digital Mars C++ compiler, which return 4 in all cases.
dalle