Is the Sizeof(Some Pointer) Always Equal to Four

Is the sizeof(some pointer) always equal to four?

The guarantee you get is that sizeof(char) == 1. There are no other guarantees, including no guarantee that sizeof(int *) == sizeof(double *).

In practice, pointers will be size 2 on a 16-bit system (if you can find one), 4 on a 32-bit system, and 8 on a 64-bit system, but there's nothing to be gained in relying on a given size.

size of pointer is always equal to size of int in C compiler . Is it correct?

My senior has told that in C program size of any pointer is always equal to size of int . Is it correct?

NO, that is not correct.

There are multiple different memory models possible, often on the same system at the same time. Often systems support running ILP32 and LP64 processes at the same time. While an int is 4 bytes (32 bits) in both ILP32 and LP64, pointers are 8 bytes (64 bits) in LP64.

How many bytes do pointers take up?

There is no fixed answer; it depends entirely on the architecture, the compiler implementation, and even the type of the pointer itself. Pointers to different types are not guaranteed to have the same size and/or representation.

For example, assume a word-addressed architecture, where the smallest addressable unit of storage is 16 bits wide (or wider). Each word can hold multiple char values; all other types take up a full word or more. On such an architecture, a char * and void * would need some extra bits to offset into the word compared to other pointer types.

Note also that a pointer type may be wider than the number of bits actually required to store an address. The original Macintosh ran on a Motorola 68000 CPU, which had a 32-bit word size, but only 24 bits on the address bus. Pointer types were 32 bits wide, leaving the upper 8 bits unused. Enterprising MacOS programmers took advantage of that to store some data to the uppermost byte of a pointer type, making the most of that precious 128 KB of RAM. Of course, Motorola eventually released a CPU with 32 address lines (the 68020), meaning all that code had to be rewritten.

On modern, commodity desktop and server hardware (read: x86), it's reasonably safe to assume that all pointer types are the same size as the native word size (32- or 64-bit), and that all pointer types have the same size and representation. Just be aware that this doesn't have to be true.

Does sizeof(pointer) depend on the object type?

Please explain why sizeof(B*) return 4?

It returns 4 because size of a pointer variable is 4 in your system.

What exactly pointer to object represents in memory?

Objects reside in memory, and pointer to an object contains the starting memory address of that object. For example, if your B object has size 100 Byte, and it is placed in 1024-1123(100 Bytes) memory location, then a pointer to that object will hold the value `024 (starting address).

Is pointer to sizeof depends on the object type?

I guess you meant does pointer size depends on object type?. No, since pointers contain an address, it's size depends on address space of your system, not type of object it points to.

What is the size of a pointer?

Pointers generally have a fixed size, for ex. on a 32-bit executable they're usually 32-bit. There are some exceptions, like on old 16-bit windows when you had to distinguish between 32-bit pointers and 16-bit... It's usually pretty safe to assume they're going to be uniform within a given executable on modern desktop OS's.

Edit: Even so, I would strongly caution against making this assumption in your code. If you're going to write something that absolutely has to have a pointers of a certain size, you'd better check it!

Function pointers are a different story -- see Jens' answer for more info.

sizeof pointer to structure surprise

All the comments are hugely instructive. Thank you. User Dxiv gave the response I was looking for.
The "correct" syntax for what I was trying to do was:

sizeof(*pfoo);

or

sizeof(footype);

Credit to user Yunnosch for pointing out my question phrasing was off. I hope I am forgiven since my knowledge at the time of the post lacked what it now has after asking the question. The question should have been "how can I determine the size of the datatype pointed to by pfoo?"



Related Topics



Leave a reply



Submit