Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can pointers be of different sizes? [duplicate]

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?

like image 224
Luchian Grigore Avatar asked Apr 05 '13 11:04

Luchian Grigore


People also ask

Are pointers all the same size?

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.

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.

Is pointer size constant?

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.

Is Size Of pointer always 8?

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.

What is the size of a pointer to a pointer?

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.

What are the different types of pointers?

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

What is the size of a double-pointer in C?

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.

How do I change the size of the mouse pointer?

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), ...


Video Answer


3 Answers

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.

like image 112
Mats Petersson Avatar answered Oct 10 '22 18:10

Mats Petersson


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).

like image 36
Bo Persson Avatar answered Oct 10 '22 18:10

Bo Persson


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).

like image 27
Lightness Races in Orbit Avatar answered Oct 10 '22 18:10

Lightness Races in Orbit