St.Antario St.Antario - 1 month ago 7
C++ Question

Does sizeof(pointer) depend on the object type?

I'm trying to understand what a pointer to an object means. I'm wondering if

A
is an incomplete type, why is a pointer to
A
a complete type. Consider the following program:

#include <stdio.h>

class B; //B is incomplete type here

int main()
{
printf("%d\n",sizeof(B*));//4
}


Please explain why does
sizeof(B*)
return 4? What exactly does a pointer to an object represent in memory?

Answer

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.

Comments