473,382 Members | 1,717 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,382 software developers and data experts.

Adress of a struct vs address of its first field

Hello everybody,

Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?

What if the type of bar is unsigned char[16]?

--
Regards, Grumble
Jul 23 '05 #1
19 6109
Grumble wrote:
Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?

What if the type of bar is unsigned char[16]?


You shouldn't assume that. However, for POD you can use 'offsetof'
macro to find out what the actual offset is. You're very likely to
get 0, but it's not guaranteed by the language, IIRC. Of course it
is possible that I don't RC.

V
Jul 23 '05 #2
Grumble wrote:
Hello everybody,

Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?

What if the type of bar is unsigned char[16]?


Yes to both. That's the rule in C, and for C-like structs (which the C++
standard calls PODs) the same rule applies.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #3
Pete Becker wrote:
Grumble wrote:
struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?
What if the type of bar is unsigned char[16]?

Yes to both. That's the rule in C, and for C-like structs (which the

C++ standard calls PODs) the same rule applies.


Actually, I don't think that the above code is supposed to compile
without a diagnostic (which will cause the compilation to fail on
many systems): You cannot compare 'foo*' to 'int*'. However, the
following should compile and yield true for POD types:

static_cast<void*>(&s) == static_cast<void*>(&s.bar)

Of course, this is just nitpicking...
--
<mailto:di***********@yahoo.com> <http://www.dietmar-kuehl.de/>
<http://www.contendix.com> - Software Development & Consulting

Jul 23 '05 #4
Pete Becker wrote:

Grumble wrote:
Hello everybody,

Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?

What if the type of bar is unsigned char[16]?


Yes to both. That's the rule in C, and for C-like structs (which the C++
standard calls PODs) the same rule applies.

Are you sure? As far as I know this only applies to unions. A struct may
contain padding bits. That is, the above is not portable.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #5
Won't say too much theory but why not just try it on a compiler or ide.
You can even see the assembler results.personally the premis is true
for all truely ansi complience C++
Edernity
---------
if it help
please help me provide better school supplies for childrenof indonesia.
it may be a donation but I merely ask you to letme send email to you
from one of the product of I'm affiliated and please register there on
mechant account free. If you even want you refund back on your dollar
of i-kard, its ok, I'll send you just use regular delivery on shipping
please. Please note this is very safe since this is a bonafid company.
remember your credit card company will confirm you on your purchase.
Still have doubts, sign on a personal account and let me contact you
again on it. just 1 email
also
I'm also affiliated to this site:
www.getaportal.com/portals/edd y_ruslim

Jul 23 '05 #6
Won't say too much theory but why not just try it on a compiler or ide.
You can even see the assembler results.personally the premis is true
for all truely ansi complience C++
well bits all the best of days that god may grant! (and not grand :-D).
standards can't live with it can't live without it
Edernity
---------
if it help
please help me provide better school supplies for childrenof indonesia.
it may be a donation but I merely ask you to letme send email to you
from one of the product of I'm affiliated and please register there on
mechant account free. If you even want you refund back on your dollar
of i-kard, its ok, I'll send you just use regular delivery on shipping
please. Please note this is very safe since this is a bonafid company.
remember your credit card company will confirm you on your purchase.
Still have doubts, sign on a personal account and let me contact you
again on it. just 1 email
also
I'm also affiliated to this site:
www.getaportal.com/portals/edd y_ruslim

Jul 23 '05 #7
Ioannis Vranos wrote:

Are you sure? As far as I know this only applies to unions. A struct may
contain padding bits. That is, the above is not portable.


Yes, I'm sure. A POD struct cannot contain padding bits before the first
field.

--

Pete Becker
Dinkumware, Ltd. (http://www.dinkumware.com)
Jul 23 '05 #8
ed*********@telkom.net wrote:
Won't say too much theory but why not just try it on a compiler or ide.
You can even see the assembler results.personally the premis is true
for all truely ansi complience C++

I would prefer to see a reference to the standard, than check some
particular compiler's output.

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #9
like I said "standards can't live with it can't live without it" sorry
for the broken posts. My ADHD/dyslexia get ahead of me if u know what i
mean.
Guys what about the donation pls 1 email could help a child for 1 month

Jul 23 '05 #10
Grumble wrote:
...
Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?
Yes, for POD structs. You just have to convert the pointer types
accordingly (using 'reinterpret_cast').
What if the type of bar is unsigned char[16]?


Doesn't matter.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #11
Ioannis Vranos wrote:
...
Won't say too much theory but why not just try it on a compiler or ide.
You can even see the assembler results.personally the premis is true
for all truely ansi complience C++

I would prefer to see a reference to the standard, than check some
particular compiler's output.
...


9.2/17

A pointer to a POD-struct object, suitably converted using a
reinterpret_cast, points to its initial member (or if that member is a
bit-field, then to the unit in which it resides) and vice versa.

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #12
In article <d1**********@news-rocq.inria.fr>,
Grumble <de*****@kma.eu.org> wrote:
Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?
In this case, yes.
What if the type of bar is unsigned char[16]?


Still yes.

When and if possible, you should favor more portable techniques though.
For instance, since you're referring to PODs only, consider maybe
offsetof, or even just use the &'s as you've shown above.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '05 #13
In article <11**********************@o13g2000cwo.googlegroups .com>,
Dietmar Kuehl <di***********@yahoo.com> wrote:
Pete Becker wrote:
Grumble wrote:
> struct foo s;
>
> Will &s == &(s.bar) always be true regardless of the type of bar?
> What if the type of bar is unsigned char[16]?
>


Yes to both. That's the rule in C, and for C-like structs (which the

C++
standard calls PODs) the same rule applies.


Actually, I don't think that the above code is supposed to compile
without a diagnostic (which will cause the compilation to fail on
many systems): You cannot compare 'foo*' to 'int*'. However, the
following should compile and yield true for POD types:

static_cast<void*>(&s) == static_cast<void*>(&s.bar)

Of course, this is just nitpicking...


Indeed, IOWs, the addrs are the same but the types are different,
so metablizing them would be necessary.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '05 #14
In article <1110824562.415403@athnrd02>,
Ioannis Vranos <iv*@remove.this.grad.com> wrote:
Pete Becker wrote:

Grumble wrote:
Hello everybody,

Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?

What if the type of bar is unsigned char[16]?


Yes to both. That's the rule in C, and for C-like structs (which the C++
standard calls PODs) the same rule applies.

Are you sure? As far as I know this only applies to unions. A struct may
contain padding bits. That is, the above is not portable.


But for POD structs, not in the very beginning.
Let's see...9.2p17
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '05 #15
In article <11**********************@f14g2000cwb.googlegroups .com>,
<ed*********@telkom.net> wrote:
Won't say too much theory but why not just try it on a compiler or ide.


Because if it were not true, if may work for some compilers,
but not others, and Murphy's Law says he wouldn't have one of the latter.
So he would not know for certain.

That said, generally speaking, there is something to be said for folks
trying things out.
--
Greg Comeau / Comeau for the Mac? Stay tuned.
Comeau C/C++ ONLINE ==> http://www.comeaucomputing.com/tryitout
World Class Compilers: Breathtaking C++, Amazing C99, Fabulous C90.
Comeau C/C++ with Dinkumware's Libraries... Have you tried it?
Jul 23 '05 #16
Greg Comeau wrote:
But for POD structs, not in the very beginning.
Let's see...9.2p17

OK but the wording is a bit "foggy". It says in square brackets that
there may be no padding bits in the beginning, however it mentions
reinterpret_cast, so to "pointers not suitably converted with
reinterpret_cast" but to void * directly, isn't that true? I guess it
is, it is a "rhetorical" question. :-)

--
Ioannis Vranos

http://www23.brinkster.com/noicys
Jul 23 '05 #17

ed*********@telkom.net wrote:
like I said "standards can't live with it can't live without it" sorry for the broken posts. My ADHD/dyslexia get ahead of me if u know what i mean.
Guys what about the donation pls 1 email could help a child for 1

month

Please quote a relevant portion of the previous message when you reply.
To do so from Google, click "show options" and use the Reply in the
expanded message header.

Brian

Jul 23 '05 #18
Andrey Tarasevich wrote:
Grumble wrote:
Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field
is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?


Yes, for POD structs. You just have to convert the pointer types
accordingly (using 'reinterpret_cast').


I was under the impression that "reinterpret_cast" should be used to
examine, for example, the actual bits that make up a pointer. Wouldn't
"static_cast" be more appropriate here?

How are they different in this situation?

--
Regards, Grumble
Jul 23 '05 #19
Grumble wrote:
Andrey Tarasevich wrote:
Grumble wrote:
Assume I have a plain, old, boring struct foo (i.e. no virtual
functions, no methods, just data fields). Assume the first field
is bar.

struct foo s;

Will &s == &(s.bar) always be true regardless of the type of bar?


Yes, for POD structs. You just have to convert the pointer types
accordingly (using 'reinterpret_cast').


I was under the impression that "reinterpret_cast" should be used to
examine, for example, the actual bits that make up a pointer. Wouldn't
"static_cast" be more appropriate here?

How are they different in this situation?


'static_cast' cannot be used here at all. 'static_cast' cannot convert
between completely unrelated types (and completely unrelated types is
what we have here). This conversion is performed specifically by
'reinterpret_cast', as described in 9.2/17

--
Best regards,
Andrey Tarasevich
Jul 23 '05 #20

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

Similar topics

4
by: Angus Comber | last post by:
Hello I have received a lot of help on my little project here. Many thanks. I have a struct with a string and a long member. I have worked out how to qsort the struct on both members. I can...
60
by: Mohd Hanafiah Abdullah | last post by:
Is the following code conformat to ANSI C? typedef struct { int a; int b; } doomdata; int main(void) { int x;
5
by: FKothe | last post by:
Hello together, the program below shows a behavior i do not understand. When compiled with the HX-UX11 c-comiler ( version B.11.11.04 ) v2.p in function test_it0 points to an invalid adress and...
2
by: mark.e.nelson | last post by:
Hi, I am trying to pass a pointer to a struct to a function that uses the data in the struct, and also happens to use ncurses. I always get a segmentation violation when the program exits. I...
11
by: mwebel | last post by:
Hi, i had this problem before (posted here and solved it then) now i have the same problem but more complicated and general... basically i want to store the adress of a istream in a char* among...
1
by: Simon | last post by:
Dear reader, By printing an e-mail adress field (hyperlink field) it will be shown as: "xxxx@yyyy.nn#Mailto:xxxx@yyyy.nn#"
3
Sagittarius
by: Sagittarius | last post by:
Hi there. I have a problem concerning an UDP socket in C++ (Winsock). The next paragraphs is merely to explain the system I am working on. If U want to skip it, I have marked the question in...
0
by: MichK | last post by:
Hi, I have a problem with passing a pointer in visual basic. The thing is I receive an array address of a certain API I call in visual basic. There is no value passed, just an address in the...
1
by: mitola | last post by:
#include <stdlib.h> #include <stdio.h> #include <string.h> #include <conio.h> struct oseba //struktura osebe , ki jo kasneje uporabljamo v datoteki { char ime; char priimek;
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.