Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Re: Compatible structs

Reply
Thread Tools

Re: Compatible structs

 
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-02-2010
"Vrtt" <> writes:

> I couldn't find this question in the FAQ, although it looks like a
> frequently asked one. Apologies if it's been answered here before.
>
> I've seen this idiom a lot in libraries:-
>
> struct base {
> int x;
> char y;
> };
>
> struct extended {
> int x;
> char y;
> double z;
> };
>
> void base_func(struct base *b);
>
> struct extended e;
> base_func((struct base*)&e);
>
> Is this actually guaranteed to work, or can struct alignment (or something
> else) mess it up?


It's not guaranteed to work, though it is one of those things that
almost certainly will. The reason is that it *is* guaranteed to work if
the two structs are members of a union, and the access is through that
union. For the compiler to arrange the layout of the common initial
members of the two structs differently it would have to be sure that no
such access is ever done and it is obviously simpler just to make the
layout of the common parts be the same "just in case".

--
Ben.
 
Reply With Quote
 
 
 
 
Seebs
Guest
Posts: n/a
 
      11-02-2010
On 2010-11-02, Ben Bacarisse <> wrote:
> It's not guaranteed to work, though it is one of those things that
> almost certainly will. The reason is that it *is* guaranteed to work if
> the two structs are members of a union, and the access is through that
> union. For the compiler to arrange the layout of the common initial
> members of the two structs differently it would have to be sure that no
> such access is ever done and it is obviously simpler just to make the
> layout of the common parts be the same "just in case".


The problem isn't layout, it's aliasing.

gcc will, already, generate code which assumes that modifications
through a (struct base *) cannot possibly affect a (struct extended *).

You can easily arrange:
struct base *b = external_base();
struct extended *e = external_extended();
assert(b == e);
e->x = 1;
b->x = 2;
printf("%d\n", e->x);

to print "1". Because the compiler KNOWS that e and b are different objects,
as they're of incompatible types.

-s
--
Copyright 2010, all wrongs reversed. Peter Seebach / usenet-
http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
I am not speaking for my employer, although they do rent some of my opinions.
 
Reply With Quote
 
 
 
 
Tim Rentsch
Guest
Posts: n/a
 
      11-02-2010
Ben Bacarisse <> writes:

> "Vrtt" <> writes:
>
>> I couldn't find this question in the FAQ, although it looks like a
>> frequently asked one. Apologies if it's been answered here before.
>>
>> I've seen this idiom a lot in libraries:-
>>
>> struct base {
>> int x;
>> char y;
>> };
>>
>> struct extended {
>> int x;
>> char y;
>> double z;
>> };
>>
>> void base_func(struct base *b);
>>
>> struct extended e;
>> base_func((struct base*)&e);
>>
>> Is this actually guaranteed to work, or can struct alignment (or something
>> else) mess it up?

>
> It's not guaranteed to work, though it is one of those things that
> almost certainly will. The reason is that it *is* guaranteed to work if
> the two structs are members of a union, and the access is through that
> union. [snip]


Access doesn't need to be done through the union type;
all that's required is that the "body" of the union
type be visible at the point where the accesses (ie,
to members in the common initial sequence) occur.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Packed structs vs. unpacked structs: what's the difference? Daniel Rudy C Programming 15 04-10-2006 08:10 AM
Array of structs instead of an array with pointers to structs? Paminu C Programming 5 10-11-2005 07:18 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
const structs in other structs Chris Hauxwell C Programming 6 04-27-2004 07:03 PM
structs with fields that are structs Patricia Van Hise C Programming 5 04-05-2004 01:37 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57