tags:

views:

289

answers:

11
+1  A: 

Pointer is a memory address - and hence should be the same on a specific machine. 32 bit machine => 4Bytes, 64 bit => 8 Bytes.

Hence irrespective of the datatype of the thing that the pointer is pointing to, the size of a pointer on a specific machine would be the same (since the space required to store a memory address would be the same.)

Assumption: I'm talking about near pointers to data values, the kind you declared in your question.

Gishu
-1, wrong. For starters it assumes pointers point to data (and a datatype) which misses function pointers. And C on DOS had a memory model with 16 bits function pointers and 20/32 bits data pointers.
MSalters
Yeah I forgot to add 'for all practical purposes' to the answers. Technically the answer might be wrong - C/C++ always has exceptions to the rule. However from the context given by the OP in the question (since I was just thinking of near (normal) pointers),I guess it answers him alright. Thanks for making me go back and read up though!
Gishu
A: 

Pointers are always the same size on the same arch, regardless of datatype it points to. Otherwise things like casting between different pointer types be useless, and break a lot of things.

Many common coding techniques depend on casting between for instance a void pointer (or a char) to various structs, depending on size.

Take even the standard printf(), is must be able to take pointers to various kinds of data. If pointers were of different size, implementing printf could get very messy.

Amigable Clark Kant
@Amigable Clark Kant: not necessarilly. Casting could work and memory addresses be of different types, provided the "missiing" information is kept somewhere in context. In old x86 days, this type of information was typically current "segment".
kriss
printf already takes arguments of different sizes - `int`, `long long` and `double` are unlikely to be the same. Varargs handles this. There's no reason varargs couldn't likewise handle pointer arguments of different sizes, provided that the function reading the varargs correctly specifies the type of each one as it reads it. This is why the `va_arg` macro takes a type as a parameter.
Steve Jessop
+1  A: 

Yes, the size of a pointer is platform dependent. More specifically, the size of a pointer depends on the target processor architecture and the "bit-ness" you compile for.

As a rule of thumb, on a 64bit machine a pointer is usually 64bits, on a 32bit machine usually 32 bits. There are exceptions however.

Since a pointer is just a memory address its always the same size regardless of what the memory it points to contains. So a pointer to a float, a char or an int are all the same size.

Johannes Rudolph
Aehm this was not the question, the question was whether pointer to different types (float*, int*, void*) can have different sizes (on the same platform), which is not the case.
Nils
+1 Yes, in ancient 16-Bit windows there where near (16 bit) and far pointer (32 bit) depending on memory modell and usage.
stacker
+10  A: 

Pointers are not always the same size on the same arch.

You can read more on the concept of "near", "far" and "huge" pointers, just as an example of a case where pointer sizes differ...

http://en.wikipedia.org/wiki/Intel_Memory_Model#Pointer_sizes

kotlinski
One could argue, that what you describe is not "C". But since DOS once ruled the earth and was the de facto standard, I gave you a vote.
Amigable Clark Kant
Yes but also in this case type does not have an influence on the size?
Nils
It can differ because different types can be placed in different memory segments.
kotlinski
@Amigable - Did you mean "amicable"? Nice nick though.
Vulcan Eager
@Vulcan, not there is an "Amiga" in the name. And Clark Gable, Clark Kent (almost) and maybe one more. :-)
Amigable Clark Kant
+7  A: 

In days of old, using e.g. Borland C compilers on the DOS platform, there were a total of (I think) 5 memory models which could even be mixed to some extent. Essentially, you had a choice of small or large pointers to data, and small or large pointers to code, and a "tiny" model where code and data had a common address space of (If I remember correctly) 64K.

It was possible to specify "huge" pointers within a program that was otherwise built in the "tiny" model. So in the worst case it was possible to have different sized pointers to the same data type in the same program!

I think the standard doesn't even forbid this, so theoretically an obscure C compiler could do this even today. But there are doubtless experts who will be able to confirm or correct this.

Carl Smotricz
The standard didn't forbid it outright, but the particular details were non-standard. However, you correctly point out that you could independently choose between small and large pointers for code and data.
MSalters
+2  A: 

Pointers to data must always be compatible with void* so generally they would be nowadays realized as types of the same width.

This statement is not true for function pointers, they may have different width. For that reason in C99 casting function pointers to void* is undefined behavior.

Jens Gustedt
All that means is that `void *` has to be at least the size of the largest other pointer type.
caf
@caf: you probably mean "other pointer to data type" and "width" instead of "size", yes I think this is what I said in my first para. That doesn't force other pointer data types to be of the same width, but it makes it very likely, since casting back from `void*` must still have all information to address the object. But you are right, a hypothetical architecture could save the low order byte of pointers for all data types that are aligned at 256 byte boundaries ;-)
Jens Gustedt
Exactly - converting from `void *` to `double *` can be lossy, but not vice-versa.
caf
+2  A: 

As I understand it there is nothing in the C standard which guarantees that pointers to different types must be the same size, so in theory an int * and a float * on the same platform could be different sizes without breaking any rules.

There is a requirement that char * and void * have the same representation and alignment requirements, and there are various other similar requirements for different subsets of pointer types but there's nothing that encompasses everything.

In practise you're unlikely to run into any implementation that uses different sized pointers unless you head into some fairly obscure places.

Nigel Harper
+1  A: 

Yes. It's uncommon, but this would certainly happen on systems that are not byte-addressable. E.g. a 16 bit system with 64 Kword = 128KB of memory. On such systems, you can still have 16 bits int pointers. But a char pointer to an 8 bit char would need an extra bit to indicate highbyte/lowbyte within the word, and thus you'd have 17/32 bits char pointers.

This might sound exotic, but many DSP's spend 99.x% of the time executing specialized numerical code. A sound DSP can be a bit simpler if it all it has to deal with is 16 bits data, leaving the occasional 8 bits math to be emulated by the compiler.

MSalters
A: 

There are platforms where function pointers are a different size than other pointers.

I've never seen more variation than this. All other pointers must be at most sizeof(void*) since the standard requires that they can be cast to void* without loss of information.

blucz
+1  A: 

Can I assume that pointers of different types have the same size (on one arch of course)?

For the platforms with flat memory model (== all popular/modern platforms) pointer size would be the same.

For the platforms with segmented memory model, for efficiency, often there are platform-specific pointer types of different sizes. (E.g. far pointers in the DOS, since 8086 CPU used segmented memory model.) But this is platform specific and non-standard.

You probably should keep in mind that in C++ size of normal pointer might differ from size of pointer to virtual method. Pointers to virtual methods has to preserve extra bit of information to not to work properly with polymorphism. This is probably only exception I'm aware of, which is still relevant (since I doubt that segmented memory model would ever make it back).

Dummy00001
+1  A: 

I was going to write a reply saying that C99 has various pointer conversion requirements that more or less ensure that pointers to data have to be all the same size. However, on reading them carefully, I realised that C99 is specifically designed to allow pointers to be of different sizes for different types.

For instance on an architecture where the integers are 4 bytes and must be 4 byte aligned an int pointer could be two bits smaller than a char or void pointer. Provided the cast actually does the shift in both directions, you're fine with C99. It helpfully says that the result of casting a char pointer to an incorrectly aligned int pointer is undefined.

See the C99 standard. Section 6.3.2.3

JeremyP
Exactly right - the standard goes out of its way to allow this.
caf
Could u provide a link to where exactly u read that?
Nils
thx :) (Section 6.3.2.3 starts on page 47 for those interested)
Nils