473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Sizeof for pointers

Hi,

I just wanted to confirm that sizeof for a pointer always returns the
same value on a machine.
I would appreciate if somebody would respond to this.

Thanks.

Dec 3 '06 #1
15 2249
Avinash said:
Hi,

I just wanted to confirm that sizeof for a pointer always returns the
same value on a machine.
It doesn't. I have a machine right here with different compilers on it,
which use different sizes for the same kind of pointer.

But on a given machine, using a given version of a given compiler with given
options, I think it's probably safe to say that the size of a pointer won't
change. What is certainly safe to say is that, within a program run,
pointer size is fixed.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 3 '06 #2
Richard Heathfield wrote:
It doesn't. I have a machine right here with different compilers on it,
which use different sizes for the same kind of pointer.

But on a given machine, using a given version of a given compiler with given
options, I think it's probably safe to say that the size of a pointer won't
change. What is certainly safe to say is that, within a program run,
pointer size is fixed.
Just to emphasize a point Richard makes here, the size of a particular
type of pointer won't change under those conditions; it's possible
that, say, a void pointer and a function pointer can have different
sizes.

Dec 3 '06 #3
"Avinash" <av******@gmail.comwrites:
I just wanted to confirm that sizeof for a pointer always returns the
same value on a machine.
I would appreciate if somebody would respond to this.
The same value as what?

On many implementations, all pointers are the same size, but it's not
guaranteed by the language. You can probably assume that sizeof will
yield the same value for the same type in the same implementation
(some compiler options may in effect cause it to be a different
implementation). Beyond that, there's seldom a reason to write code
that assumes that all pointers have the same size.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 3 '06 #4
Avinash wrote:
Hi,

I just wanted to confirm that sizeof for a pointer always returns the
same value on a machine.
I would appreciate if somebody would respond to this.

Thanks.
"On one machine" is very unclear. Many people can for example run 32
bit PowerPC, 32 bit x86 and 64 bit x86 code on the same machine, and I
surely expect them to have different pointer sizes on the same machine.
You probably mean "on one implementation".

On one implementation, it wouldn't be at all surprising if function
pointers and data pointers have different sizes. All pointers to struct
must have the same size; all pointers to unions must have the same
size, things like int*, const int*, volatile int* must have the same
size. char* and void* must have the same size. Pointers to structs and
for example pointers to chars can have different sizes, and there are
implementations where the sizes are actually different.

Dec 3 '06 #5
Yes, I meant "on one implementation" and the same compiler.
Why do we have different pointer sizes for struct and data pointers?
I mean for eg on a 32-bit machine, can't a struct and some data (char,
int etc) be accessed using a 32-bit address. So is'nt it sufficient if
all pointers have the ability to hold 32-bit information and thus
ensure uniformity in pointer size, or am I missing some special case.
Also, by data pointers I guess you also include float *, double * and
other access specifiers (volatile float *, const float *, const double
*, volatile double* etc).
christian.bau wrote:
Avinash wrote:
Hi,

I just wanted to confirm that sizeof for a pointer always returns the
same value on a machine.
I would appreciate if somebody would respond to this.

Thanks.

"On one machine" is very unclear. Many people can for example run 32
bit PowerPC, 32 bit x86 and 64 bit x86 code on the same machine, and I
surely expect them to have different pointer sizes on the same machine.
You probably mean "on one implementation".

On one implementation, it wouldn't be at all surprising if function
pointers and data pointers have different sizes. All pointers to struct
must have the same size; all pointers to unions must have the same
size, things like int*, const int*, volatile int* must have the same
size. char* and void* must have the same size. Pointers to structs and
for example pointers to chars can have different sizes, and there are
implementations where the sizes are actually different.
Dec 3 '06 #6
"Avinash" <av******@gmail.comwrites:
Why do we have different pointer sizes for struct and data pointers?
From http://www.parashift.com/c++-faq-lit....html#faq-26.6

The PDP-10 has 36-bit words with no hardware facility to address
anything within one of those words. That means a pointer can point
only at things on a 36-bit boundary: it is not possible for a
pointer to point 8 bits to the right of where some other pointer
points.

[...] Another valid approach would be to define a "byte" as 9
bits, and simulate a char* by two words of memory: the first could
point to the 36-bit word, the second could be a bit-offset within
that word. In that case, the C++ compiler would need to add extra
instructions when compiling code using char* pointers. For
example, the code generated for *p = 'x' might read the word into
a register, then use bit-masks and bit-shifts to change the
appropriate 9-bit byte within that word. An int* could still be
implemented as a single hardware pointer, since C++ allows
sizeof(char*) != sizeof(int*).

It's C++ FAQ but same applies to C.
I mean for eg on a 32-bit machine, can't a struct and some data (char,
int etc) be accessed using a 32-bit address.
It probably can but standard does not guarantee that your program will
run on 32-bit machine.
So is'nt it sufficient if all pointers have the ability to hold
32-bit information and thus ensure uniformity in pointer size, or am
I missing some special case.
You do - a case where your program is running on some "strange"
architecture. ;)

--
Best regards, _ _
.o. | Liege of Serenly Enlightened Majesty of o' \,=./ `o
..o | Computer Science, Michal "mina86" Nazarewicz (o o)
ooo +--<mina86*tlen.pl>---<jid:mina86*chrome.pl>--ooO--(_)--Ooo--
Dec 3 '06 #7
sj*******@yahoo.com said:
Richard Heathfield wrote:
>It doesn't. I have a machine right here with different compilers on it,
which use different sizes for the same kind of pointer.

But on a given machine, using a given version of a given compiler with
given options, I think it's probably safe to say that the size of a
pointer won't change. What is certainly safe to say is that, within a
program run, pointer size is fixed.

Just to emphasize a point Richard makes here, the size of a particular
type of pointer won't change under those conditions; it's possible
that, say, a void pointer and a function pointer can have different
sizes.
Oops, yes, I should have said that, within a program run, the size of
pointers of a particular *type* is fixed. Thanks for clarifying.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Dec 3 '06 #8
"sj*******@yahoo.com" wrote:
Richard Heathfield wrote:
>It doesn't. I have a machine right here with different compilers
on it, which use different sizes for the same kind of pointer.

But on a given machine, using a given version of a given compiler
with given options, I think it's probably safe to say that the size
of a pointer won't change. What is certainly safe to say is that,
within a program run, pointer size is fixed.

Just to emphasize a point Richard makes here, the size of a particular
type of pointer won't change under those conditions; it's possible
that, say, a void pointer and a function pointer can have different
sizes.
Also that a void* and a double* have different sizes. The only
requirement is that an arbitrary pointer can be converted to a
void* and back again (to the same type) without losing any
information.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Dec 3 '06 #9
Avinash wrote:
>
Yes, I meant "on one implementation" and the same compiler.
Why do we have different pointer sizes for struct and data pointers?
I mean for eg on a 32-bit machine, can't a struct and some data (char,
int etc) be accessed using a 32-bit address. So is'nt it sufficient if
all pointers have the ability to hold 32-bit information and thus
ensure uniformity in pointer size, or am I missing some special case.
Also, by data pointers I guess you also include float *, double * and
other access specifiers (volatile float *, const float *, const double
*, volatile double* etc).
Don't top-post. See the following links.

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Dec 3 '06 #10
Michal Nazarewicz wrote:
"Avinash" <av******@gmail.comwrites:
>Why do we have different pointer sizes for struct and data pointers?

From http://www.parashift.com/c++-faq-lit....html#faq-26.6

The PDP-10 has 36-bit words with no hardware facility to address
anything within one of those words. That means a pointer can point
only at things on a 36-bit boundary: it is not possible for a
pointer to point 8 bits to the right of where some other pointer
points.
This is very misleading. The PDP-10 had machine-level byte pointers
which specified a 36-bit word boundary and an offset. One could load or
store bytes, with or without increment or decrement of the byte pointer,
at the assembly level. Pointers *do* point other that at 36-bit
boundaries. Further, there is a complete set of half-word (left and
right 18-bit) instructions. The PDP-10 had one of the richest
addressing schemes available. Whoever wrote that FAQ chose his example
poorly. Maybe C++ people need several layers of obfuscation above the
machine level.

Dec 3 '06 #11
On 3 Dec 2006 02:49:43 -0800, "Avinash" <av******@gmail.comwrote:
>Yes, I meant "on one implementation" and the same compiler.
Why do we have different pointer sizes for struct and data pointers?
Whether the sizes are different or not is a design decision made by
the compiler writer, probably based on factors such as:
what does the hardware require
what does the operating system require
what simplifies the code generating portion of the compiler
what are the conventions used by non-C runtime libraries popular
on this system
what are my personal preferences
etc

The real question is "Why do you care?" Is there part of your code
that needs to know? If so, please describe it. Someone may be able
to offer an alternative that is not dependent on the information and
thereby make your code that much more portable.
Remove del for email
Dec 3 '06 #12
CBFalconer wrote:
"sj*******@yahoo.com" wrote:
Just to emphasize a point Richard makes here, the size of a particular
type of pointer won't change under those conditions; it's possible
that, say, a void pointer and a function pointer can have different
sizes.

Also that a void* and a double* have different sizes.
Yes.
The only
requirement is that an arbitrary pointer can be converted to a
void* and back again (to the same type) without losing any
information.
I thought that function pointers in particular could not be portably
converted to void pointers (and FAQ 4.13 seems to agree).

Dec 3 '06 #13
In article <45***************@yahoo.com>,
CBFalconer <cb********@maineline.netwrote:
>Avinash wrote:
>>
Yes, I meant "on one implementation" and the same compiler.
Why do we have different pointer sizes for struct and data pointers?
I mean for eg on a 32-bit machine, can't a struct and some data (char,
int etc) be accessed using a 32-bit address. So is'nt it sufficient if
all pointers have the ability to hold 32-bit information and thus
ensure uniformity in pointer size, or am I missing some special case.
Also, by data pointers I guess you also include float *, double * and
other access specifiers (volatile float *, const float *, const double
*, volatile double* etc).

Don't top-post. See the following links.

--
Some informative links:
<news:news.announce.newusers
<http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
Yes, and don't forget these, far more helpful, informative, and topical links:

http://en.wikipedia.org/wiki/Aspergers
http://en.wikipedia.org/wiki/Clique
http://en.wikipedia.org/wiki/C_programming_language

Dec 3 '06 #14
"sj*******@yahoo.com" <sj*******@yahoo.comwrites:
[...]
I thought that function pointers in particular could not be portably
converted to void pointers (and FAQ 4.13 seems to agree).
That's correct. This leads to the periodic discussion of how you can
portably print the value of a function pointer. (You can't, but you
can print its representation by treating the pointer as an array of
unsigned char.)

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Dec 3 '06 #15
<OT>
On Sun, 03 Dec 2006 13:36:30 -0500, Martin Ambuhl
<ma*****@earthlink.netwrote:
Michal Nazarewicz wrote:
"Avinash" <av******@gmail.comwrites:
Why do we have different pointer sizes for struct and data pointers?
From http://www.parashift.com/c++-faq-lit....html#faq-26.6

The PDP-10 has 36-bit words with no hardware facility to address
anything within one of those words. That means a pointer can point
only at things on a 36-bit boundary: it is not possible for a
pointer to point 8 bits to the right of where some other pointer
points.

This is very misleading. The PDP-10 had machine-level byte pointers
which specified a 36-bit word boundary and an offset. One could load or
and length (width).
store bytes, with or without increment or decrement of the byte pointer,
at the assembly level. Pointers *do* point other that at 36-bit
or increment alone, or (as of KL10 IIRC) increment N times;

and the mnemonics LDB (load byte, "lud-bee") and DPB (store byte,
"dip-bee"), and less often the increment forms were for a long time
accepted if somewhat silly hackish for "get something from / put
something to the middle of something else".
boundaries. Further, there is a complete set of half-word (left and
right 18-bit) instructions. The PDP-10 had one of the richest
addressing schemes available. Whoever wrote that FAQ chose his example
One of the richer instruction (operation) sets of its time, including
all 2^4 two-operand bitwise operations, and multiple indirection which
had some interesting <Guses, but not otherwise very rich addressing.
Compare to -11 (register & indirect, autoinc & autodec ditto, constant
& absolute, offset & indirect, relative & indirect) (and soon after
VAX with even more, and 68k), and even S/360 (offset + base + index).
poorly. Maybe C++ people need several layers of obfuscation above the
machine level.
I believe some of the _other_ 36-bit or wider machines did have
difficulty with variable subword addressing -- GE/HIS? Cyber?

- David.Thompson1 at worldnet.att.net
Dec 26 '06 #16

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: signuts | last post by:
I'm aware of what sizeof(...) does, what I would like to know is if sizeof(...) is compiled in or a function that's executed at run-time. Like for example { int a; printf("a is %d...
79
by: syntax | last post by:
what is the size of a pointer? suppose i am writing, datatype *ptr; sizeof(ptr); now what does this sizeof(ptr) will give? will it give the size of the
33
by: Chris Fogelklou | last post by:
What is wrong with the above? Don't worry, I already know (learned my lesson last week.) It is for the benefit of our resident compiler guru who seems to think you need the cast. I thought it...
9
by: vijay | last post by:
Hello, I am new to C Programming and just started reading K&R. I was about to finish the pointers chapter but got very confused with: 1. int arr; >From what I have read, arr is a pointer to...
6
by: Amigo | last post by:
Hello all! I started working on an embedded project a few ago on Freescale 16-bit micro with an IAR toolset. Running PolySpace for the project code highlighted amongst other things a peculiar...
2
by: GRenard | last post by:
Hello, We just switch in our company to VisualStudio 2005 and the new ATL library. We use a wrapper to use CFileDialog. Its name is CFileDialogDeluxe Here the call CFileDialogDeluxe...
38
by: James Brown | last post by:
All, I have a quick question regarding the size of pointer-types: I believe that the sizeof(char *) may not necessarily be the same as sizeof(int *) ? But how about multiple levels of pointers...
17
by: kevin | last post by:
this is my programm .... char *p="abcde"; char a="abcde"; ..... printf("the size of p is:%d\n",sizeof(p)); printf("the size of a is:%d\n",sizeof(a)); ........
2
by: anon.asdf | last post by:
Hi! Q. 1) How does one write: sizeof(array of 5 "pointers to double") ??? I know that sizeof(pointer to an array of 5 doubles) can be written as: sizeof(double (*));
16
by: Alex Vinokur | last post by:
Does it have to be? : sizeof (size_t) >= sizeof (pointer) Alex Vinokur email: alex DOT vinokur AT gmail DOT com http://mathforum.org/library/view/10978.html http://sourceforge.net/users/alexvn
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.