-1

Why is that all the pointers in C have same size? I am on 64 bit arch.

#include<stdio.h>

int main(){
printf("int\t%ld\n",sizeof(int*));
printf("char\t%ld\n",sizeof(char*));
printf("void\t%ld\n",sizeof(void*));
printf("float\t%ld\n",sizeof(float*));
}

OP :
int     8
char    8
void    8
float   8
4bh1
  • 182
  • 2
  • 13
  • 1
    Why wouldn't they have the same size? – Baum mit Augen Jul 17 '16 at 14:59
  • They are not guaranteed to be same size but generally always are: http://stackoverflow.com/questions/1241205/are-all-data-pointers-the-same-size-in-one-platform-for-all-data-types – SurvivalMachine Jul 17 '16 at 15:01
  • 1
    Suppose one of them had a smaller size, that would put a big limit on where things of that type are allowed to exist in memory. – harold Jul 17 '16 at 15:06
  • @harold: Which is typical on some architectures. There is more to C than x86/ARM. – too honest for this site Jul 17 '16 at 15:51
  • @Olaf it is a consequence, I didn't say that makes it impossible. It sure makes it inconvenient though. – harold Jul 17 '16 at 15:55
  • @harold: Only if you intend to exploit undefined behaviour. This is **one** reason, C has the effective type (aka strict aliasing) rule! To say it clearly: There are possibly more systems out which **don't** have the same sizes for all pointers. Considering the still widely used 8051 and the various PIC lines. – too honest for this site Jul 17 '16 at 15:57
  • @Olaf I don't see how that inconvenience is at all related to UB. We'd be in a situation where some objects can go everywhere but others can not. Anything we want to do in "high memory" must explicitly forgo the use of whatever type it is that's annoying in this way. There would have to be two types of memory allocation, one that promises to allocate only low memory and one that can use high memory. It's annoying all over, I haven't even touched bad aliasing yet. – harold Jul 17 '16 at 16:05
  • @harold: Please read the standard. It is not about a specific object, but about a type! Pointers to the same type are obvously not allowed to differ in length. And unless you use illegal casting, there is not necessarily an "inconvenience" (whatever you mean with that). Au contraire, it is used to enhance code quality (memory usage, code size). Please read about embedded systems programming! – too honest for this site Jul 17 '16 at 16:10
  • @Olaf Forget illegal casting. We'd have a situation where, for example, an `int` can be anywhere but a `float` (and by extension anything that includes a float) only in low memory, so that the smaller `float*` (assuming it's smaller than an `int*`) can still point to it. How is that *not* inconvenient? I don't even know any embedded systems that work that way, but hey maybe they exist and they would be inconvenient. – harold Jul 17 '16 at 16:13
  • @harold: I will not discuss this further. Before you know the basics of embedded systems and the wide spectrum of architectures, this leads no where. Note that this is also a matter on harvard architectures e.g. with 64 bit data memory and 32 bit code storage. There is no necessary nuissance/inconvenience. You don't know PIC? 8051? Embedded is not ARMv6M/7M. – too honest for this site Jul 17 '16 at 16:18
  • @Olaf please, I know about harvard architectures. That's not what this was about. The assumption was that, eg `sizeof(int*) != sizeof(float*)`. But this does lead nowhere, as this is *obviously* inconvenient but you want to deny it with nonsequiturs. – harold Jul 17 '16 at 16:19
  • @harold: How does a question about the sizeof a pointer not also cover function **pointer**s? Don't just pick the raisins! – too honest for this site Jul 17 '16 at 16:20
  • @Olaf because it's not listed by OP. Function pointers of different size are of course a non-issue. – harold Jul 17 '16 at 16:21

1 Answers1

4

Because all you need is 64 bits to index a memory address and all the data types you've listed only need to index a memory address (where the given data 'starts' in memory). Note that this is true for C, but in C++ e.g., member function pointers might be more than that.

lorro
  • 10,687
  • 23
  • 36
  • Yes they all point to a memory address. The reason they are still of different datatype like int char etc is for C compiler to do Pointer Arithmetic. For example adding a 'char' pointer would increment by one signifying next byte and four for a 'int'. – Nishant Jul 17 '16 at 15:04
  • 2
    @Nishant: that's a consequence of the reason, but not the reason. Had it been the reason, for any `T` and `U` where `sizeof(T) == sizeof(U)`, we could've said that `T*` is the same as `U*` - which is clearly not the case for e.g. different structs. The reason is type safety and the need for dereference operator to have a definite, specific return type. A consequence is that the compiler can now do meaningful pointer arithmetics. – lorro Jul 17 '16 at 16:01