union access

R

Richard Bos

Mabden said:
Structs and unions have a similar definition, so the Standards Whores
think they are the same, but in fact if they were that similar you
could, for instance make a union array.

Well, then here's your lesson for today, and maybe it'll teach you not
to use words like "whores" again in a hurry: you _can_ create union
arrays. I could even think of a reasonable application.

Richard
 
C

Chris Dollin

Dan said:
With the *significant* difference that all offsets in a union are zero.
Which is what makes both things *completely* different semantically: one
is grouping a number of related entities together, the other is
overlapping them.


Not true for unions:

struct foo s;
union bar u;

s.a = 1;
s.b = 2;

u.a = 1;
u.b = 2;

True for unions:

union bar u;
u.a = 1;
u.a += 1;

The difference is just that writing into one member makes the other
member's values undefined, which in turn is why overlapping them
makes sense.
Which means exactly zilch. if and while are also syntactically almost
identical, yet they are still completely different things.

You must have some awsomely picky notion of sameness, then, since
both if [1] and while are semantically very similar things - they
have an expression component, which they evaluate in the same way
and which guards the statement component, which they also evaluate
the same way; the differences are that the while repeats itself
and the if doesn't, and the while rebinds the meaning of break
and continue, and the if doesn't.

You'd have been better off picking switch vs if, since the switch-
expression isn't evaluated as a boolean, and its component compound
statement isn't evaluated in the same way as other compound statements.

[1] if-without-else, otherwise they wouldn't be "syntactically almost
identical".
 
C

CBFalconer

Mabden said:
.... snip ...

No. Duh. I have never seen a union used to save memory.

You obviously haven't done much system programming. This is the
primary use for them.
The main reason to use unions is to take in chunk of data in
binary form and break it down into meaningful data. Things like
packets that come in as X number of bits, and pulling out the
real data as flags and bytes. We do this all the time when
communicating with the mainframe.

This is probably (depending on types) non-portable coding, and
should be concentrated in system-dependant files. Sooner or later
that dog will byte.
Structs and unions have a similar definition, so the Standards
Whores think they are the same, but in fact if they were that
similar you could, for instance make a union array. That doesn't
make sense, so that is one difference unions and structures have.

Except it does make sense, and arrays of unions are implemented
all the time. Your inexperience is showing again.
 
D

Dan Pop

In said:
True for unions:

union bar u;
u.a = 1;
u.a += 1;

The difference is just that writing into one member makes the other
member's values undefined, which in turn is why overlapping them
makes sense.

Which is precisely what makes unions and structures completely different
things, despite any *superficial* similarities.
Which means exactly zilch. if and while are also syntactically almost
identical, yet they are still completely different things.

You must have some awsomely picky notion of sameness, then, since
both if [1] and while are semantically very similar things -

You must have some awsomely lax notion of sameness, if the if and while
statements look semantically very similar to you. To me, the only
statements semantically close to while are do and for.

Then again, a wheel mouse and a wheelbarrow might be semantically very
similar things to you...

Dan
 
B

Ben Pfaff

Mabden said:
No. Duh. I have never seen a union used to save memory.

Here's an example:

/* Node in a parse tree for a simple expression grammar. */
struct node {
enum { PLUS, MINUS, TIMES, DIV, VAR, NUM } type;
union {
struct node *operands[2]; /* PLUS, MINUS, TIMES, DIV: operands. */
const char *var_name; /* VAR: variable name. */
double num; /* NUM: numerical value. */
} u;
};

If you change "union" to "struct", it works just as well, but you
waste memory.
 
M

Michael Wojcik

No. Duh. I have never seen a union used to save memory.

Duh indeed. What makes you think that your personal experience is
definitive?

*I* have certainly seen unions used to save memory; I may have used
them that way myself, though it's a matter of interpretation. (If I
use a union as a container for various types of data, only one of
which will be stored in it, for reasons of elegance and efficiency
rather than to "save memory" per se, does that count?)

Note that even on systems with ample memory, a union may often win
over a structure with redundant members because of the limited size
of cache lines, among other things.
The main reason to use unions is to take in chunk of data in binary form
and break it down into meaningful data. Things like packets that come in
as X number of bits, and pulling out the real data as flags and bytes.

The "main reason", eh? You've surveyed a representative sample of
C programmers to determine this?
We do this all the time when communicating with the mainframe.

Well hooray for you. Those of us who have to write *portable* C
comms code don't like to make assumptions about how implementations
will lay things out in memory, so we don't use unions for type
punning; we marshall and unmarshall packets manually, using unsigned
char, in the manner actually supported by the language.
Structs and unions have a similar definition, so the Standards Whores
think they are the same, but in fact if they were that similar you
could, for instance make a union array. That doesn't make sense, so that
is one difference unions and structures have.

What are you talking about? You can make arrays of unions, and you
can put arrays inside unions. What's a "union array"?
You could just as well say that functions and arrays are the same thing
because they both have curly braces to begin and end them.

This is as bogus an analogy as I have seen here in some time. unions
and structs are obviously more similar than functions and arrays,
since the former pair are both data structures; and arrays don't
"begin and end" with "curly braces" (though array initializers do).
 
K

Keith Thompson

Mabden said:
No. Duh. I have never seen a union used to save memory.

The main reason to use unions is to take in chunk of data in binary form
and break it down into meaningful data. Things like packets that come in
as X number of bits, and pulling out the real data as flags and bytes.
We do this all the time when communicating with the mainframe.

Structs and unions have a similar definition, so the Standards Whores
think they are the same, but in fact if they were that similar you
could, for instance make a union array. That doesn't make sense, so that
is one difference unions and structures have.

You could just as well say that functions and arrays are the same thing
because they both have curly braces to begin and end them.

Apart from your other errors, nobody has claimed that structs and
unions are the same. Obviously they're quite different in many ways.

This whole argument is about a claim that structs and unions are
"completely different". They are neither identical nor completely
different; they're similar in some ways, different in others.
 
O

Old Wolf

The main reason to use unions is to take in chunk of data in binary form
and break it down into meaningful data. Things like packets that come in
as X number of bits, and pulling out the real data as flags and bytes.

Undefined behaviour.
We do this all the time when communicating with the mainframe.

What happens when you change to a new compiler and the code
stops working?
Structs and unions have a similar definition, so the Standards Whores
think they are the same, but in fact if they were that similar you
could, for instance make a union array. That doesn't make sense, so that
is one difference unions and structures have.

What doesn't make sense about an array of unions?
 
K

Keith Thompson

Undefined behaviour.


What happens when you change to a new compiler and the code
stops working?
[...]

If the code stops working, you change back to the old compiler.

Undefined behavior doesn't *guarantee* nasal demons, and non-portable
code can be acceptable if you understand that it's non-portable and
you're sufficiently careful about it.
 
M

Mabden

CBFalconer said:
You obviously haven't done much system programming. This is the
primary use for them.

Well, I have only used them to take raw packets and break them up into
fields. Usually IPX or whatnot. As you point out, very non-portable
stuff.
This is probably (depending on types) non-portable coding, and
should be concentrated in system-dependant files. Sooner or later
that dog will byte.

Of course.
Except it does make sense, and arrays of unions are implemented
all the time. Your inexperience is showing again.

I didn't mean an array of unions. I said a union can't be used as an
array. I don't think you can say:

union interface {
int input;
int ouput;
} u [50];

can you?
 
B

Ben Pfaff

Mabden said:
I didn't mean an array of unions. I said a union can't be used as an
array. I don't think you can say:

union interface {
int input;
int ouput;
} u [50];

can you?

That *is* an array of unions and yes you can do that.
 
M

Mabden

Michael Wojcik said:
Duh indeed. What makes you think that your personal experience is
definitive?

Because I have experienced it.
*I* have certainly seen unions used to save memory; I may have used
them that way myself, though it's a matter of interpretation. (If I
use a union as a container for various types of data, only one of
which will be stored in it, for reasons of elegance and efficiency
rather than to "save memory" per se, does that count?)

No. That is just to confuse the next guy that has to interpret your
code. Are you aiming for job security through obfuscation? That never
works.
Note that even on systems with ample memory, a union may often win
over a structure with redundant members because of the limited size
of cache lines, among other things.

Which may result in a union inside an array. We're not talking about
combinations of structs in unions or unions in structs, just about how
the two are different animals.

bytes.

The "main reason", eh? You've surveyed a representative sample of
C programmers to determine this?

Yes, me.
Well hooray for you. Those of us who have to write *portable* C
comms code don't like to make assumptions about how implementations
will lay things out in memory, so we don't use unions for type
punning; we marshall and unmarshall packets manually, using unsigned
char, in the manner actually supported by the language.

Are you sure you shouldn't be in the C++ newsgroup?

This is as bogus an analogy as I have seen here in some time. unions
and structs are obviously more similar than functions and arrays,
since the former pair are both data structures; and arrays don't
"begin and end" with "curly braces" (though array initializers do).

Actually, I meant functions and structures. Sorry for the confusion.
 
F

Flash Gordon

Because I have experienced it.

That merely defines your experience and has nothing to do with what
occurs in the rest of the world.
No. That is just to confuse the next guy that has to interpret your
code. Are you aiming for job security through obfuscation? That never
works.

I doubt that he is aiming at job security through obscurity. It is a
method I have seen used many times, including a lot of code written by
experienced programmers using the Pascal equivalent (variant records)
for projects having to meet MoD quality standards.
Which may result in a union inside an array.

Not a problem.
We're not talking about
combinations of structs in unions or unions in structs, just about how
the two are different animals.

Well, it was you who just raised arrays of unions, not Michael.

You are only representative of you and I somehow doubt that you have
done most types of programming.
Are you sure you shouldn't be in the C++ newsgroup?

Why? What he said is true for C. I can't comment about whether it is
also true for C++, although I would suspect that it is.
Actually, I meant functions and structures. Sorry for the confusion.

Unions and structures are obviously more similar that functions and
structures. For a start unions and structures are types of data storage,
functions are not.
 
K

Keith Thompson

Mabden said:
CBFalconer said:
Mabden wrote: [...]
Structs and unions have a similar definition, so the Standards
Whores think they are the same, but in fact if they were that
similar you could, for instance make a union array. That doesn't
make sense, so that is one difference unions and structures have.

Except it does make sense, and arrays of unions are implemented
all the time. Your inexperience is showing again.

I didn't mean an array of unions. I said a union can't be used as an
array.

No, a union can't be used as an array (and I don't recall anyone
claiming otherwise). I suppose a struct can be used as an array:

struct {
int elem0;
int elem1;
int elem2;
/* etc. */
}

but it seldom makes much sense to do so. If you want an array, use an
array. If you want a union, use a union. If you want a struct, use a
struct.
I don't think you can say:
union interface {
int input;
int ouput;
} u [50];

can you?

Certainly you can. It's an array of unions. It defines a type "union
interface", and defines u as an array of 50 of them.

Here's a small program using the above declaration:

#include <stdio.h>
#include <stddef.h>

int main(void)
{
union interface {
int input;
int ouput;
} u [50];

printf("sizeof(int) = %d\n", (int)sizeof(int));
printf("sizeof(union interface) = %d\n",
(int)sizeof(union interface));
printf("offsetof(union interface, input) = %d\n",
(int)offsetof(union interface, input));
printf("offsetof(union interface, ouput) = %d\n",
(int)offsetof(union interface, ouput));
printf("sizeof u = %d\n", (int)sizeof u);
return 0;
}

And here's the output I got:

sizeof(int) = 4
sizeof(union interface) = 4
offsetof(union interface, input) = 0
offsetof(union interface, ouput) = 0
sizeof u = 200

The sizes will vary, of course, on systems where sizeof(int) != 4.

It's not at all clear to me what point you're trying to make. If
you're just arguing that structs and unions aren't the same thing,
don't bother; we all know there are significant differences between
them, and nobody here has claimed otherwise.

On a more personal note, I'm tempted to suggest that if you're going
to call people "whores" you should be more careful about the facts.
There is a certain irony in insulting people when it turns out that
they're right and you're wrong. But there are really two separate
issues here. You should be more careful of your facts (whether you're
calling people "whores" or not), and you shouldn't call people
"whores" (whether you're right about the facts or not).

Think before you post.
 
M

Mabden

Keith Thompson said:
but it seldom makes much sense to do so. If you want an array, use an
array. If you want a union, use a union. If you want a struct, use a
struct.
I don't think you can say:
union interface {
int input;
int output;
} u [50];

can you?

Certainly you can. It's an array of unions. It defines a type "union
interface", and defines u as an array of 50 of them.

Here's a small program using the above declaration:

It's not at all clear to me what point you're trying to make. If
you're just arguing that structs and unions aren't the same thing,
don't bother; we all know there are significant differences between
them, and nobody here has claimed otherwise.

Well, that _was_ actually the argument. Someone had a problem with Dan
Pop saying they were "completely different" and off it went. I think
they are completely different in use and concept, if not in declaration
or whatever the Standard Readers suggest. You don't usually use one
where the other is more apropos, but then the Standards Suitors got
their backs up...
On a more personal note, I'm tempted to suggest that if you're going
to call people "whores" you should be more careful about the facts.

Well, I was asking a question, not posting "facts".

There is a certain irony in insulting people when it turns out that
they're right and you're wrong.

Can I be wrong by asking a question? Am I wrong now?

But there are really two separate
issues here. You should be more careful of your facts (whether you're
calling people "whores" or not), and you shouldn't call people
"whores" (whether you're right about the facts or not).

And since they're not being paid, should I just say Standards Sluts? ;-)
<gd&r>
Are you a little over-sensitive about my phrasing? Do you consider
yourself to be included in this group, or are you just speaking up for
the maligned?
Think before you post.

Point taken, I sometimes get annoyed with the "believers" who spout
"nonsense"^w "the truth" about C99 when I don't have any idea how to get
a compiler that will comply to it...


Last note: are unions and structs the same in YOUR mind, Keith?
 
M

Mabden

Flash Gordon said:
That merely defines your experience and has nothing to do with what
occurs in the rest of the world.

Oh.




I doubt that he is aiming at job security through obscurity. It is a
method I have seen used many times, including a lot of code written by
experienced programmers using the Pascal equivalent (variant records)
for projects having to meet MoD quality standards.


Pascal is not as versatile as C. You might have to do tricks to play
games around the compiler. Perhaps you should have used a Real language
to begin with...?


Unions and structures are obviously more similar that functions and
structures. For a start unions and structures are types of data storage,
functions are not.

"more similar that functions" doesn't make sense. What I said does. See,
we are comparing "apples to oranges", so I am bound to compare things
that are dissimilar (is that a word? unsimilar?). See, I believe unions
and struct are dissimilar - even though they look the same. Is a duck a
goose? Is a zebra a mule?
 
M

Michael Wojcik

Because I have experienced it.

And you are ... megalomaniacal? schizophrenic? simply delusional?

Why is it that so many people on Usenet think that their experience
can be generalized to the whole world? Is it a problem of education,
or are they actually inherently stupid?

Y'know, Dan's attitude becomes more understandable every day.
No. That is just to confuse the next guy that has to interpret your
code. Are you aiming for job security through obfuscation? That never
works.

Any developer who's confused by this very common use of unions
should not be maintaining C code, mine or anyone else's. I'm not
sure they should be doing any programming at all.
Which may result in a union inside an array. We're not talking about
combinations of structs in unions or unions in structs, just about how
the two are different animals.

No, *I* am talking about unions, what they are commonly used for,
and why. I don't know what the hell *you* are talking about. It
doesn't appear to be C.
The main reason [more inanity].

The "main reason", eh? You've surveyed a representative sample of
C programmers to determine this?

Yes, me.

Well, I suppose if your group is "crap C programmers who don't know
how unions are commonly used, or what you can do with them", then
you may be representative. Purely by accident, but I suppose that's
how you get a lot of things done.
Are you sure you shouldn't be in the C++ newsgroup?

Nothing I wrote above has anything to do with C++, as distinct from
C. It's equally applicable to both. What reference to C++ did you
hallucinate?

--
Michael Wojcik (e-mail address removed)

Reversible CA's are -automorphisms- on shift spaces. It is a notorious
fact in symbolic dynamics that describing such things on a shift of finite
type are -fiendishly- difficult. -- Chris Hillman
 
K

Keith Thompson

Mabden said:
Well, that _was_ actually the argument. Someone had a problem with Dan
Pop saying they were "completely different" and off it went. I think
they are completely different in use and concept, if not in declaration
or whatever the Standard Readers suggest. You don't usually use one
where the other is more apropos, but then the Standards Suitors got
their backs up...

No, that was actually not the argument. I'll try one more time to
explain the distinction.

Logically, there are three possibilities:

(1) Structures and unions are completely different.

(2) Structures and unions are similar in some ways, but significantly
different in other ways.

(3) Structures and unions are the same thing.

Dan Pop asserted (1). I disagreed, and asserted (2). You've been
arguing that the rest of are wrong for asserting (3), but in fact,
nobody has done so.
Well, I was asking a question, not posting "facts".

You've done both.
And since they're not being paid, should I just say Standards Sluts? ;-)
<gd&r>

No, you should keep your stupid insults to yourself. (Yes, I saw the
smiley.)
Are you a little over-sensitive about my phrasing? Do you consider
yourself to be included in this group, or are you just speaking up for
the maligned?

No, I am not being over-sensitive. You referred to some set of people
as "Standards Whores". It seemed clear from the context that I was
being included in that generalization. Calling people whores is
insulting. Taking offense at being called whores is not
"over-sensitive".

Based on your history, I suspect you didn't realize how offensive you
were being, and I'm not sure it's going to sink in even now (though
I'm prepared to be pleasantly surprised).
Point taken, I sometimes get annoyed with the "believers" who spout
"nonsense"^w "the truth" about C99 when I don't have any idea how to get
a compiler that will comply to it...

I don't know what "nonsense" you're referring to in this context. In
particular, I think all the issues in this thread are common to C90
and C99.
Last note: are unions and structs the same in YOUR mind, Keith?

No, they are not the same. I have never said that they are, nor has
anyone else. I have clearly and repeatedly said that unions and
structs are two different things that are similar in some ways.
Please pay attention.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top