473,549 Members | 5,196 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2263
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_Keit h) 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*chr ome.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*******@yaho o.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.anno unce.newusers
<http://www.geocities.c om/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html >
<http://www.netmeister. org/news/learn2quote.htm l>
<http://cfaj.freeshell. org/google/>
Dec 3 '06 #10

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

Similar topics

15
4921
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 bytes\n", sizeof(a)); }
79
125162
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
2675
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 too, up until I started posting here! Thanks, Chris
9
2222
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 the first int and &arr is a pointer to the whole array of 10 ints. Then why does sizeof(arr) gives 40 while sizeof(&arr) gives 4. Shouldn't is be...
6
15170
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 construct in one of the compiler's header files. Here's the code snippet: #ifndef __SIZE_T_TYPE__ #if sizeof((char*)0 - (char*)0) <=...
2
2582
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 oFileDialog( ... ); There are some problems in the debug, look the sizeof from the debug :
38
2565
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 to the same type? Would sizeof(char **) be the same as sizeof(char *)? And if it is, would the internal representation be the same in both cases? ...
17
1901
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
2165
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
15627
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
7526
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7962
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7480
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7814
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6050
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5373
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5092
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3486
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1063
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.