Are char*
, int*
, long*
or even long long*
of same size (on a given platform)?
The size of a pointer in C/C++ is not fixed. It depends upon different issues like Operating system, CPU architecture etc. 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.
Thus, pointers to all types of data occupy the same size of memory because the value of a pointer is the memory address – an unsigned integer. However, the variables to which the pointers point to, of course, occupy different sizes of memory blocks according to their types.
Pointer data type is a special kind of variable which are meant to store addresses only, instead of values(integer, float, double, char, etc). It knows how many bytes the data is stored in. When we increment a pointer, we increase the pointer by the size of the data type to which it points.
On 32-bit machine sizeof pointer is 32 bits ( 4 bytes), while on 64 bit machine it's 8 byte.
They're not guaranteed to be the same size, although on the platforms I have experience with they usually are.
C 2011 online draft:
6.2.5 Types
...
28 A pointer tovoid
shall have the same representation and alignment requirements as a pointer to a character type.48) Similarly, pointers to qualified or unqualified versions of compatible types shall have the same representation and alignment requirements. All pointers to structure types shall have the same representation and alignment requirements as each other. All pointers to union types shall have the same representation and alignment requirements as each other. Pointers to other types need not have the same representation or alignment requirements.
48) The same representation and alignment requirements are meant to imply interchangeability as arguments to functions, return values from functions, and members of unions.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With