Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the size of pointers vary in C? [duplicate]

Tags:

c

pointers

Possible Duplicates:
Can the Size of Pointers Vary Depending on what’s Pointed To?
Are there are any platforms where pointers to different types have different sizes?

Is it possible that the size of a pointer to a float in c differs from a pointer to int? Having tried it out, I get the same result for all kinds of pointers.

#include <stdio.h> #include <stdlib.h>  int main() {     printf("sizeof(int*): %i\n", sizeof(int*));     printf("sizeof(float*): %i\n", sizeof(float*));     printf("sizeof(void*): %i\n", sizeof(void*));     return 0; } 

Which outputs here (OSX 10.6 64bit)

sizeof(int*): 8 sizeof(float*): 8 sizeof(void*): 8 

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

like image 701
Nils Avatar asked Aug 19 '10 08:08

Nils


People also ask

Are pointers all the same size in C?

Generally yes, All pointers to anything, whether they point to a int or a long or a string or an array of strings or a function, point to a single memory address, which is the same size on a machine.

Is the size of a pointer always the same?

Well, it depends on your platform. Most implementations share a same size for every kind of pointer on a specific platform. Please ask for either C or C++, not for both in the same question.

What does pointer size depend on?

Usually it depends upon the word size of underlying processor for example for a 32 bit computer the pointer size can be 4 bytes for a 64 bit computer the pointer size can be 8 bytes. So for a specific architecture pointer size will be fixed. It is common to all data types like int *, float * etc.

What is the size of double pointer in C?

Size of double pointer = 4 bytes.


2 Answers

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

like image 184
Johan Kotlinski Avatar answered Oct 18 '22 01:10

Johan Kotlinski


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.

like image 26
Carl Smotricz Avatar answered Oct 17 '22 23:10

Carl Smotricz