By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,647 Members | 1,832 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,647 IT Pros & Developers. It's quick & easy.

Do all pointers have same size?

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Cheng Mo wrote:
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.

--
Karthik.
' Remove _nospamplz from my email to mail me. '
Jul 22 '05 #2

P: n/a
"Cheng Mo" <mo******@nospam.nospam> wrote in message
news:co**********@avnika.corp.mot.com...
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?
Jul 22 '05 #3

P: n/a
Cheng Mo wrote:
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:offset 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*... ;-)
Jul 22 '05 #4

P: n/a
On Tue, 23 Nov 2004 20:42:45 -0800, Karthik Kumar
<ka*******************@yahoo.com> wrote in comp.lang.c++:
Cheng Mo wrote:
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.


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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #5

P: n/a
"Edward A Cullen" <ea****@ecs.soton.ac.uk> wrote in message
news:41******@news.ecs.soton.ac.uk...
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.
Jul 22 '05 #6

P: n/a
"Dietmar Kuehl" <di***********@yahoo.com> wrote in message
Siemel Naran wrote:

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.


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.
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.