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?
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.
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.
Function Pointers can have very different sizes, from 4 to 20 bytes on an x86 machine, depending on the compiler. So the answer is no - sizes can vary.
The size of any pointer is always 8 on your platform, so it's platform dependent. The sizeof operator doesn't care where the pointer is pointing to, it gives the size of the pointer, in the first case it just gives the size of the array, and that is not the same.
So, the size of a pointer to a pointer should have the usual values, that is, 2 bytes for a 16-bit machine, 4 bytes for a 32-bit machine, and 8 bytes for a 64-bit machine. Let us confirm our theoretical knowledge with a program.
Types of Pointers 1 Null pointer 2 Void pointer 3 Wild pointer 4 Dangling pointer 5 Complex pointer 6 Near pointer 7 Far pointer 8 Huge pointer
As we already know, the size of pointer in C is machine-dependent, so the size of the double-pointer should be the same as that of the character-pointer. Let us confirm that with the following code. What is a Pointer to a Pointer? We already know a pointer holds the value of the address of a particular variable.
To change the pointer’s size, drag the slider under “Change the Pointer Size.”. By default, the mouse pointer is set to 1—the smallest size. You can choose a size from 1 to 15 (which is very large). Choose a new color in the “Change Pointer Color” section. There are four options here: white with a black border (the default), ...
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.
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).
The language-lawyer 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
).
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