0

Small code snippets in C.

#include<stdio.h>

int main()
{
    printf("%zu\n%zu\n", sizeof(char*), sizeof(double*));
}

Output:(GCC 64 bit)

8
8

Is sizeof(char*) always equal to sizeof(double*)?

What C standard say about this?

msc
  • 33,420
  • 29
  • 119
  • 214
  • 1
    No there is no such guarantee. Historically there have been platforms where pointers to different things could have been different sizes (IIRC). On modern PC-type systems they are all the same though. – Some programmer dude Sep 07 '18 at 07:11
  • 2
    By the way, why are you asking? Plain curiosity, or is there an underlying problem you need to solve? – Some programmer dude Sep 07 '18 at 07:12
  • A pointer is a pointer is a pointer? They have to point somewhere in memory. Unless your processors stores different variable types in different kinds of memory, it should be identical. @Someprogrammerdude IIRC? Can you remember where you might recall this from? Sounds strange, but there ***were*** some funky processors, back in the day. For historical processors, OP might want to ask at https://retrocomputing.stackexchange.com/ – Mawg says reinstate Monica Sep 07 '18 at 07:13
  • @Mawg types can also be in the same memory, but pointers to chars wider than pointers to words because pointers to word can omit the few least significant bits. – Pascal Cuoq Sep 07 '18 at 07:15
  • Could you explain that, please, as if to a five year old? – Mawg says reinstate Monica Sep 07 '18 at 07:16
  • @Mawg Pointers to words are generally required to be word-aligned, so on a 32-bit system the least significant two bits of any pointer-to-word will be zero. The number of bits in the pointer-to-word that carry useful information is therefore smaller than the number of bits in a pointer-to-byte that do so, and so it is effectively narrower. I've never seen a system that attempted to use a different width of storage for them though! – cooperised Sep 07 '18 at 07:33
  • @cooperised Intriguing point. The information theory enthropy of a pointer to a 4 byte type is definitly smaller than of a pointer to a one byte type. However, this "size" difference of those pointers is in the bits and not in the bytes. The size of a pointer only changes effectively if it changes by more than a byte, usually even by more than a power of two bytes (1,2,4,8...). You cannot store a 31 bit pointer in anything differently sized than a 32bit pointer, at least not on the widely available machines, which use byte-organised memory. – Yunnosch Sep 07 '18 at 07:39
  • @Yunnosch: There have been computers with memory accessible only in multibyte word units, in which the native addressing on such a computer is merely the number of the word in memory. Byte operations had to be constructed via shift and other bit or arithmetic operations. As I recall hearing in the dark ages, some C implementation on such a computer used the word number for pointers to word-size types and used word number plus byte-within-word for pointers to bytes. – Eric Postpischil Sep 07 '18 at 12:14
  • @Eric Also interesting. Did I get you right, that those implementations would use a full sized "pointer-to-word" (not going in to details here) plus a separate byte-within-word information? "Seperate" as "cannot be packed into one thing of pointer-to-word-size"? If yes it is another argument for not assuming that pointers to all types are same, not even within one system. – Yunnosch Sep 07 '18 at 12:22
  • 1
    @SouravGhosh Could you review your dupe decision? I feel that this question has a new aspect, two pointers (i.e. not one) to different types (i.e. not the same) on one system. The proposed dupes discuss guarantees on one pointer size (there are none) and differences of pointer sizes to the same type across systems (they can vary) and the difference of size-of-type to size-of-pointer-to-sametype (totally different thing, size comparing meaningless). They do not discuss "in one system/implementation can two pointers be of different size?" (this is my understanding of the core of this OP here). – Yunnosch Sep 07 '18 at 12:28
  • @Yunnosch: That is my understanding, from decades-old memory of something I heard, not anything I worked with personally. – Eric Postpischil Sep 07 '18 at 12:30
  • @Yunnosch I think the top two answers in the first dupe talks about different pointers in same system, am I missing something? – Sourav Ghosh Sep 07 '18 at 12:31
  • @SouravGhosh Let me double check. "Top" as in "most upvoted"? – Yunnosch Sep 07 '18 at 12:32
  • @Yunnosch Ah yes, vote count. – Sourav Ghosh Sep 07 '18 at 12:34
  • 1
    The FryGy answer just is "can be 8", which is off. (You probably do not mean this one...) The answer by David does state the non-guarantee on two pointer sizes, I agree. But I feel that it does not make this question here a dupe of that question, because that question is basically "Are all pointers size 4?" which is a much more restrictive question than this one here. The answer has a detail which answers this question here, too. But my understanding of a duplicate is "You ask the same question." with a possibly perceived undertone of "You could have found that one yourself." @SouravGhosh – Yunnosch Sep 07 '18 at 12:40
  • @SouravGhosh I do not imply any rudeness on your part, just to make that clear. – Yunnosch Sep 07 '18 at 12:41
  • @SouravGhosh Eclipse answer is the same. It basically answers this question. But still, the question is relevantly different, isn't it? – Yunnosch Sep 07 '18 at 12:47
  • @Yunnosch no offense taken. As I was this, both the questions were variants of "can different pointers have different size?"- well, the answer is - yes and no - it depends on the compiler - C standard has nothing to say about this, – Sourav Ghosh Sep 07 '18 at 12:47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/179636/discussion-between-yunnosch-and-sourav-ghosh). – Yunnosch Sep 07 '18 at 12:47
  • 1
    I see a similar and relevant difference between the questions as between "Can two humans be of different color?" and "Can two human sisters be of different color?" The correct answer in both cases is "Yes." but to give that answer to the second, more reasoning is needed. The answers to the dupe proposal do that reasoning, but the questions remain different. Both questions are a variant of "Can human color differ?" @SouravGhosh – Yunnosch Sep 07 '18 at 12:52

2 Answers2

1

Both, sizeof(char*) and sizeof(double*) are the size of a pointer. So they are very likely identical on any imaginable system.

It is however theoretically possible that an implementation has different methods for referencing different datatypes. I could imagine a system wiht a small size optimisation memory scheme doing e.g. single byte plain old data in a special place with implicitly "only" 32 bit pointers, while it does larger compound constructs in a larger area, which needs 64 bit pointers.
So there is no guarantee of this seemingly "obvious" assumption.

This does of course assume that everything is on the same system, pointer sizes can and do vary between different systems.

Yunnosch
  • 26,130
  • 9
  • 42
  • 54
-4

not only this whenever you print a pointer variable you will get the same value. Because pointer variable is used to hold the address of the variable.

  • 1
    (a) Pointers of different types are not required by the C standard to be the same size. (b) Pointers to the same location may have different representations, and the result of printing pointers with `%p` is implementation-defined and does not necessarily produce the same string for all pointers to the same location. – Eric Postpischil Sep 07 '18 at 12:10