16

This answer comes with an interesting statement - "on machines where int* is smaller than a char*". (let's exclude pointers to functions)

Is it possible for pointers to different types to have different sizes? Why would this be useful?

Community
  • 1
  • 1
Luchian Grigore
  • 253,575
  • 64
  • 457
  • 625
  • 3
    This may be useful: http://stackoverflow.com/questions/12006854/why-the-size-of-a-pointer-to-a-function-is-different-from-the-size-of-a-pointer – Kiril Kirov Apr 05 '13 at 11:23
  • 2
    @KirilKirov that is interesting (I guess I immediately excluded function pointers from my train of thought). Good info! – Luchian Grigore Apr 05 '13 at 11:27
  • 1
    @KirilKirov I was lazy enough not to check your link, but it seems that after all, my answer is redundant -.- –  Apr 05 '13 at 11:29
  • If I'm not wrong, there are very specific platforms, where `char*` and `int*` have different sizes. It's been a while, since I read about this, but if I remember correctly - this is very rare and is more relevant for some embedded systems. – Kiril Kirov Apr 05 '13 at 11:29
  • All answers mentioned the size is not guaranteed. But [`uintptr_t`](http://en.cppreference.com/w/cpp/types/integer) is fixed size for all pointer types!? – masoud Apr 05 '13 at 11:40
  • As I said elsewhere `uintptr_t` is guaranteed to be "large enough to hold all pointer types", similar to `void *`. – Mats Petersson Apr 05 '13 at 12:05

5 Answers5

14

Yes, it's entirely possible. On some machines, a pointer to a byte contains two values: A pointer to the WORD address of the memory word containing the byte, and a "byte index" that gives the position of the byte within the word. E.g. on a 32-bit machine, the "byte index" is 0..3.

This would require more storage space than a "int *", which is just a pointer to the relevant word.

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
6

On word addressed machines a char* might need to contain part-word info, making it larger than an int*.

The standard allows this, not to rule out implementations on such hardware (even though that is even more rare now than when C89 was designed).

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
  • Linked your answer [here](http://stackoverflow.com/a/29035972/1708801) do you know of other cases where `char*` has a different size than `int*`? – Shafik Yaghmour Mar 13 '15 at 15:40
5

The tag means that you are asking about C++ and its compliant implementations, not some specific physical machine.

I'd have to quote the entire standard in order to prove it, but the simple fact is that it makes no guarantees on the result of sizeof(T*) for any T, and (as a corollary) no guarantees that sizeof(T1*) == sizeof(T2*) for any T1 and T2).

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
4

Yes, pointers aren't guaranteed to have the same size, although on most modern architectures they are, in practice.

One point where this can be useful is when one concerns data vs. function pointers. Historically, function pointers (that are used to essentially jump to certain parts in the executable code) needed so-called "far pointers" which are wider than data pointers.

2

I can imagine a machine where it makes sense to assume that the memory needs for int arrays are going to be far less than the memory needs for char arrays.

One may specify, for instance, that an implementation is not going to use more than 10 dynamically allocated integers, but is free to allocate many char arrays. In this case, one byte may suffice for an int*, while a char* needs to be four bytes or more.

That's a theoretical vision.

xtofl
  • 40,723
  • 12
  • 105
  • 192