Do all pointers have same size?

C

Cheng Mo

I tried in g++
sizeof(int*)
siezof(long*)
sizeof(Foo*)//Foo is a self-defined class

They all has same size. Do all pointers have same size in a given
platform with given compiler?

Thanks & Regards
 
K

Karthik Kumar

Cheng said:
I tried in g++
sizeof(int*)
siezof(long*)
sizeof(Foo*)//Foo is a self-defined class

They all has same size. Do all pointers have same size


Yes. Pointers essentially hold the address of an object.
And that is independent of what object it points to. Hence
sizeof (pointer to any object) is going to be a constant.
in a given
platform with given compiler?

You can probably use the term *implementation* to be
more specific about the above mentioned combination.
 
A

Andrew Koenig

I tried in g++
sizeof(int*)
siezof(long*)
sizeof(Foo*)//Foo is a self-defined class

They all has same size. Do all pointers have same size in a given platform
with given compiler?

Not necessarily.

However, all pointers to class objects have the same size.

Why do you ask?
 
P

Phil Staite

Cheng said:
I tried in g++
sizeof(int*)
siezof(long*)
sizeof(Foo*)//Foo is a self-defined class

They all has same size. Do all pointers have same size in a given
platform with given compiler?

Thanks & Regards

Not necessarily. In the (good/bad) old days of programming 16 bit DOS
systems... There was the notion of a "near" and a "far" pointer - near
pointers being 16 bit offsets in the current segment, far pointers being
segment:eek:ffset pairs (32 bits, but resolved to 24 thanks to overlap...)
You could have various memory "models" where all pointers were near, or
data near and code far, or code near and data far, both far...

I would imagine that in some embedded systems with their constrained
memory configurations you could find oddball configurations too.

So I wouldn't assume that all pointers are created equal and/or take the
same space. That leads to dangerous thinking like casting them to int
or long or void*... ;-)
 
J

Jack Klein

Yes. Pointers essentially hold the address of an object.
And that is independent of what object it points to. Hence
sizeof (pointer to any object) is going to be a constant.


You can probably use the term *implementation* to be
more specific about the above mentioned combination.

That is your experience, but is neither required nor guaranteed by
either the C or C++ language standards. Pointer to char and pointer
to void must have the same size and representation. Pointers to all
other object and function types need not.
 
S

Siemel Naran

Cheng Mo said:
They all has same size. Do all pointers have same size in a given
platform with given compiler?

No. If they did, then you would be able forward declare enums, which is
something we unfortunately cannot do.
 
D

Dietmar Kuehl

Siemel said:
No. If they did, then you would be able forward declare enums, which is
something we unfortunately cannot do.

Actually, the pointer size may be one problem, although a solvable one
(e.g. for pointers to enums a fixed size pointer could be used). The
problem with enums is that you need to know the size of the type
underlying the enum which may be any of the integer types (char, short,
int, long). To determine the size of the underlying type, you need to
know the values of the enum. THAT is the problem with enum forward
declaration.
 
B

Bob Hairgrove

Yes. Pointers essentially hold the address of an object.
And that is independent of what object it points to. Hence
sizeof (pointer to any object) is going to be a constant.

Pointer to member can vary in size depending on the use of multiple
inheritance.

Also, the size of a pointer is platform-dependent, i.e. on a 64-bit
architecture, pointers are typically 64 bits wide, whereas on a 32-bit
platform they are most likely 32 bits wide. Some older platforms, such
as 16-bit MSDOS, use large far pointers (segment + offset) and smaller
(16-bit) near pointers; however, I believe the compiler translated
plain pointers to the appropriate types in most cases.
 
R

Ron Natalie

Cheng said:
I tried in g++
sizeof(int*)
siezof(long*)
sizeof(Foo*)//Foo is a self-defined class

They all has same size. Do all pointers have same size in a given
platform with given compiler?
Not necessarily. sizeof(char*) == sizeof(void*), but the other
pointers to objects may be of a different size (though it is unlikely...
I've used more oddball machines than most people here, and while
the data pointer FORMAT's differ, I can't think of a machine
where the size was different).

Note that function pointers quite commonly are a different size
than data pointers. Pointers to members are even more so.
 
E

Edward A Cullen

Cheng said:
I tried in g++
sizeof(int*)
siezof(long*)
sizeof(Foo*)//Foo is a self-defined class

They all has same size. Do all pointers have same size in a given
platform with given compiler?

Thanks & Regards

Yes.

A pointer is an address in memory. The size of the address is defined by
the architecture of the system, for example on IA32 processors (386 and
later), running in 32bit mode, the memory address width is 32bits (which
equals 4GB of memory). On AMD64 processors it is 64bits.

There are all sorts of clever things done in hardware (paging) that maps
a 32-bit address space onto less than 4GB of physical memory, but you
don't really need to worry about this unless you are writing operating
systems...
 
A

Andrew Koenig

A pointer is an address in memory. The size of the address is defined by
the architecture of the system, for example on IA32 processors (386 and
later), running in 32bit mode, the memory address width is 32bits (which
equals 4GB of memory). On AMD64 processors it is 64bits.

However, not all processor architectures allow individual characters to be
addressed.

On an architecture that doesn't allow individual character addressing, a C++
implementation has two choices:

1) Store only one character per addressible unit.

2) Augment character pointers by additional information identifying the
specific character within an addressible unit.

On implementations that do (2), character pointers will usually take up more
space than other pointers.
 
S

Siemel Naran

Dietmar Kuehl said:
Siemel Naran wrote:

Actually, the pointer size may be one problem, although a solvable one
(e.g. for pointers to enums a fixed size pointer could be used). The
problem with enums is that you need to know the size of the type
underlying the enum which may be any of the integer types (char, short,
int, long). To determine the size of the underlying type, you need to
know the values of the enum. THAT is the problem with enum forward
declaration.

The point of a forward declaration is to be able to form a pointer or
reference to the type, or to declare a function accepting or returning the
type by value even. No-one needs to know the underlying sizeof(enum
MyEnum), just as no-one needs to know the sizeof(class MyClass). If we did
"e.g. for pointers to enums a fixed size pointer could be used" then we
could simply forward declare enums from now on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,536
Latest member
VeldaYoung

Latest Threads

Top