Re: copy unnamed union in struct



On Jan 13, 12:20 am, Logan Shaw <lshaw-use...@xxxxxxxxxxxxx> wrote:
mic...@xxxxxxxxx wrote:
On Jan 10, 5:08 am, Logan Shaw <lshaw-use...@xxxxxxxxxxxxx> wrote:
mic...@xxxxxxxxx wrote:
You should then, probably, be able to
access the members with or without the name as you please, and you
should, probably, also be able to access the entire named union
within the outer union. Because the inner union will have all the
same members as the outer union, it should require the same amount
of storage. :-)

Perhaps an example would help:

#define UNION_MEMBERS int idx; void* ptr;

struct bar {
double val;
union {
union { UNION_MEMBERS } myunion;
UNION_MEMBERS;
};
};

struct bar x, y;

Now you should be able to refer to both idx and myunion.idx, both
referring to the same storage (in the same position) and of
the same type.
That is quite cunning.

Thanks. ;-)

This (ability to refer) is based on assuming rhyme and reason in the
universe, rather than it being garantueed by the C standard?

I do not know the C standard very well (especially compared to
the c.l.c folks) and do not know whether it is guaranteed by the
standard or not.

However, it seems quite *possible* it's guaranteed behavior. If
in fact it is, and if you were writing a proof to show that it is
based on other more basic guaranteed behaviors, I think this proof
might involve reasoning that the members of the inner union have
the same relationship to members of the outer union that members
of the outer union have to each other. To wit, there may be
guarantees about the relationship between x and y here:

union {
int x;
int y;
} u;

And those same guarantees might apply to the relationship
between x and y here:

union {
int x;
union {
int y;
} v;
} u;

Again, whether this is true or not, I don't know, but it seems
like the type of thing that, while not intentionally put into
standard explicitly, could implicitly fall out logically from
something else that was put there intentionally.

I know
this is a question that may belong to clc, but the wider context of my
question definitely does not fit there.

True, but if you are willing to brave the waters there, you
could narrow your question to something specific about the
guaranteed behavior(s) of unions, and you might be able to
frame the discussion in way that's on-topic and interesting
to the people there *and* get an answer to your question.

That assumes that nested (named) unions are legal, which I'm
fairly sure they are but not 100% sure, and that you phrase
the question in terms of named unions (not unnamed ones),
then extrapolate the named union behavior to the unnamed
unions you're using. (I would think the named vs. unnamed
question would be orthogonal to the nesting question.)

I can see the line of reasoning and the proposed orthogonality seems
reasonable. Very recently the following thread washed up from the
rough waters of clc: "how is a struct represented internally", in
which the issue of different struct types with identical members and
member ordering is discussed. The discussion seems to agree with your
line of reasoning.

My default compiler setting is gcc -Wall -pedantic -ansi. I will have
to drop -ansi for this to compile, and -pedantic will generate a
warning, all of which makes perfect sense of course.

This was an interesting excursion, one I may put to use on some
glorious day.

Stijn
.



Relevant Pages

  • Re: void * pointer convert problem.
    ... >> int year; ... unions: if a union contains several structures that share a common ... _common initial sequence_ if corresponding members have compatible ...
    (comp.lang.c)
  • Re: Pointer to "base" type - what does the Standard say about this?
    ... One such exception is to access equivalent initial members of structs ... that are union members. ... Don't make the base type a struct member, ... struct s {int i; const int ci;}; ...
    (comp.lang.c)
  • Licensing - drop in live music in smaller venues.
    ... Writing about the Licensing Act on page 9 of the latest issue of Musician, ... the in-house journal for Musicians' Union members, ...
    (uk.music.folk)
  • CAW Union accepts cuts in pay, jobs
    ... CAW Union accepts cuts in pay, ... Canadian Auto Workers members at Chrysler Group's Brampton plant near ... The approval by CAW Local 1285 comes after a vote last month by members to ... Chrysler planned to make an investment at the facility worth ...
    (rec.autos.makers.chrysler)
  • Re: wages
    ... usually even the crew members go down by rate for their gear up to nothing ... the union influence is a non issue. ... There really is no union covering sound recordists / boom swingers etc, ... unfortunately) some of whom are union members and some aren't. ...
    (rec.arts.movies.production.sound)