473,385 Members | 2,180 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,385 software developers and data experts.

C programmers! How do you use your 'union's ?

Dear mates,

This is an another survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by the same average C programmer who's made a similar
survey about the uses of 'enum's.

I know that space optimization is the key idea behind 'union's, and in
most cases a type-field is necessary to verify the type of the entity
in the 'union' variable.

I'm just curious about how my elder colleagues utilize this feature of
C in their implementations based on diverse programming styles in
diverse application domains.

Thank you for your interest.

Best regards,
//rk
Nov 13 '05 #1
13 12314

"Razmig K" <st*********@postmaster.co.uk> wrote in message

This is an another survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
My programs are pretty much a union-free zone.
I'm just curious about how my elder colleagues utilize this feature of
C in their implementations based on diverse programming styles in
diverse application domains.

One place unions are used is in the X Window system. The idea is that the
client and the X server can reside on different machines, and packets can go
over a network. An obvious optimisation is to make each packet of fixed
size, with a type field telling you how to interpret the data - a mouse
click, a request to redraw, etc. The messages are therefore implemented as
unions.
Nov 13 '05 #2
Malcolm wrote:
My programs are pretty much a union-free zone.


Another vote for: Likewise
--
|_ CJSonnack <Ch***@Sonnack.com> _____________| How's my programming? |
|_ http://www.Sonnack.com/ ___________________| Call: 1-800-DEV-NULL |
|_____________________________________________|___ ____________________|
Nov 13 '05 #3
On 15 Aug 2003 09:29:12 -0700, st*********@postmaster.co.uk (Razmig K)
wrote:
Dear mates,

This is an another survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by the same average C programmer who's made a similar
survey about the uses of 'enum's.

I know that space optimization is the key idea behind 'union's, and in
most cases a type-field is necessary to verify the type of the entity
in the 'union' variable.

I'm just curious about how my elder colleagues utilize this feature of
C in their implementations based on diverse programming styles in
diverse application domains.


In my largest project, 10 years old and going strong, I found exactly
*two* uses of 'union' in ~500K lines of C code.

And were I refactoring either area of the code base today, I would get
rid of them.

- Sev

Nov 13 '05 #4
Razmig K wrote:
Dear mates,

This is an another survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by the same average C programmer who's made a similar
survey about the uses of 'enum's.

I know that space optimization is the key idea behind 'union's, and in
most cases a type-field is necessary to verify the type of the entity
in the 'union' variable.

I'm just curious about how my elder colleagues utilize this feature of
C in their implementations based on diverse programming styles in
diverse application domains.

Thank you for your interest.

Best regards,
//rk


I use unions in my "ooze" code. An ooze is an abstract input stream
that can represent either a string or a file. Mr. Heathfield's
thesaurus deserves credit for the name.

For your viewing pleasure:
http://homepage.mac.com/gershwin/temp/ooze.h
http://homepage.mac.com/gershwin/temp/ooze.c

Oh, and the license for this stuff (just in case)
http://www.people.cornell.edu/pages/...ss/license.txt

-Peter

Nov 13 '05 #5

Razmig K <st*********@postmaster.co.uk> wrote in message
news:3c************************@posting.google.com ...
Dear mates,

This is an another survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by the same average C programmer who's made a similar
survey about the uses of 'enum's.


I use unions in a stack-based VM. Several different types can be pushed
onto the same stack. I'm hard pressed to think of something more elegant
than unions for that.

I missed the poll on enums. I never use them. I always #define stuff.

Now next you'll probably ask about gotos. I was taught to be a goto snob by
more than one professor. Recently though, I've loosed up and used some
gotos for functions that require extensive "clean up" before returning.

--$teve

Nov 13 '05 #6

Peter Ammon <pe*********@rocketmail.com> wrote in message
news:bh**********@news.apple.com...
[snip]

Oh, and the license for this stuff (just in case)
http://www.people.cornell.edu/pages/...ss/license.txt
Holy horizontals, Batman!

There are no line terminators. Quick, the Bat-cariage!
return that is... (groan).

--$teve

-Peter

Nov 13 '05 #7
istartedi <co******@vrml3d.com> wrote:
Peter Ammon <pe*********@rocketmail.com> wrote in message
news:bh**********@news.apple.com...
[snip]

Oh, and the license for this stuff (just in case)
http://www.people.cornell.edu/pages/...ss/license.txt


Holy horizontals, Batman!

There are no line terminators. Quick, the Bat-cariage!
return that is... (groan).


it is copyright "April 1st"....
Nov 13 '05 #8
istartedi wrote:

Peter Ammon <pe*********@rocketmail.com> wrote in message
news:bh**********@news.apple.com...
[snip]

Oh, and the license for this stuff (just in case)
http://www.people.cornell.edu/pages/...ss/license.txt


Holy horizontals, Batman!

There are no line terminators. Quick, the Bat-cariage!
return that is... (groan).

First, its carriage. Or maybe carrier. Think typewriters.
The IBM Model B had a carriage. That was the mechanism which held the
platen and therefore the paper and moved it (through escapes) past a
fixed print mechanism. The Teletype printers and the IBM Selectric have
fixed platen (paper) and moving print mechanisms called carriers. So
what does CR mean after all? Sorry. :-)
--
Joe Wright mailto:jo********@earthlink.net
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #9
Razmig K <st*********@postmaster.co.uk> wrote:
Dear mates,

This is an another survey for the common (and uncommon) nontrivial
uses of the aforementioned C construct.
It's posted by the same average C programmer who's made a similar
survey about the uses of 'enum's.

I know that space optimization is the key idea behind 'union's, and in
most cases a type-field is necessary to verify the type of the entity
in the 'union' variable.

I'm just curious about how my elder colleagues utilize this feature of
C in their implementations based on diverse programming styles in
diverse application domains.


One use for unions is if you have a data structure - say, a binary tree
- where the data carried by a node depends on its type:

struct node {
struct node *child_left, *child_right;
enum { NODE_TYPE_A, NODE_TYPE_B, NODE_TYPE_C } type;
union {
struct {
int x;
char *y;
char *z;
} type_a;
struct {
int x;
void *y;
} type_b;
struct {
struct tm x;
long int y;
} type_c;
} data;
};

One place this example in particular appears is building expression
parse trees.

- Kevin.

Nov 13 '05 #10
Jens Schicke wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> wrote:
"Razmig K" <st*********@postmaster.co.uk> wrote in message

This is an another survey for the common (and uncommon)
nontrivial uses of the aforementioned C construct.

My programs are pretty much a union-free zone.


My too...

I used them once in a parser written with bison, and in an
implementation of a network protocol, but usually I avoid them
since I usually dislike to access the same data as two different
types. And even if I am forced to do so (hexdumps for example) I
usually use pointers of different type.


This is a very short sighted view. unions can be very effective
in adapting data records to their function. They can be used to
build the logical (but verbose) equivalent of a Pascal variant
record.

For example, if you have a symbol table, and want to record the
characteristics of that symbol, e.g. a constant, a define, a
function, a variable, all of which have widely different things to
record, a union is the appropriate mechanism. One field of the
structure will hold the type of the identifier, another may point
to the spelling, and a union can hold such things as values,
constantness, etc.

Even with todays monstrous memories, conservation is useful. If
nothing else it reduces the missing of cache values and thrashing
of virtual memory.

If you examine Linux kernel source or filesystems, I believe you
will find many examples of unions.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #11
On Sat, 16 Aug 2003 02:35:38 GMT, CBFalconer <cb********@yahoo.com>
wrote:
Jens Schicke wrote:
Malcolm <ma*****@55bank.freeserve.co.uk> wrote:
> "Razmig K" <st*********@postmaster.co.uk> wrote in message
>>
>> This is an another survey for the common (and uncommon)
>> nontrivial uses of the aforementioned C construct.
>>
> My programs are pretty much a union-free zone.


My too...

I used them once in a parser written with bison, and in an
implementation of a network protocol, but usually I avoid them
since I usually dislike to access the same data as two different
types. And even if I am forced to do so (hexdumps for example) I
usually use pointers of different type.


This is a very short sighted view. unions can be very effective
in adapting data records to their function. They can be used to
build the logical (but verbose) equivalent of a Pascal variant
record.

For example, if you have a symbol table, and want to record the
characteristics of that symbol, e.g. a constant, a define, a
function, a variable, all of which have widely different things to
record, a union is the appropriate mechanism. One field of the
structure will hold the type of the identifier, another may point
to the spelling, and a union can hold such things as values,
constantness, etc.

Even with todays monstrous memories, conservation is useful. If
nothing else it reduces the missing of cache values and thrashing
of virtual memory.

If you examine Linux kernel source or filesystems, I believe you
will find many examples of unions.


This is similar to one of my uses of union that I would not now do the
same way. I maintain an array of "steps" to perform, and each step
contains a step type and the data for the step. The data is different
for each type, so I used a union in my original design (about 8 or 9
years ago).

However, since the data in my case is variable-length, it would
actually be more space-efficient to separately allocate each step's
data and point to it, and I would recode it that way.

Were the data all the same length (within a small %), or all very
small, a union would be appropriate.

- Sev
Nov 13 '05 #12
Severian wrote:
.... snip ...
This is similar to one of my uses of union that I would not now
do the same way. I maintain an array of "steps" to perform, and
each step contains a step type and the data for the step. The
data is different for each type, so I used a union in my original
design (about 8 or 9 years ago).

However, since the data in my case is variable-length, it would
actually be more space-efficient to separately allocate each
step's data and point to it, and I would recode it that way.

Were the data all the same length (within a small %), or all
very small, a union would be appropriate.


This is where Pascal variant records are superior. new(thing,
kind) can be called with a parameter specifying the variant, and
only enough memory for that variant will be allocated. With C you
will have to nest malloc calls and set up a chain of void*
pointers. This expands the record (struct) with the space to hold
those pointers.

C99 has provisions for implementing the equivalent of Pascal
variant records.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 13 '05 #13
On Sun, 17 Aug 2003 03:54:52 GMT, CBFalconer <cb********@yahoo.com>
wrote:
<snip>
This is where Pascal variant records are superior. new(thing,
kind) can be called with a parameter specifying the variant, and
only enough memory for that variant will be allocated. With C you
will have to nest malloc calls and set up a chain of void*
pointers. This expands the record (struct) with the space to hold
those pointers.
First, I don't see what void* pointers, or any kind of pointers, have
to do with it. Second, you can fairly easily compute the size of any
"variant" (in C, case of trailing union) with offsetof and sizeof.

I think it is technically illegal to access the resulting memory as
the whole type, but in practice it should work as long as you only
access (parts of) the variant that is allocated, as is required for
Pascal also. The only thing that's likely a problem is assigning, or
passing as an argument or returning by value, the whole type, and
(only) the first can be fixed by instead memcpy'ing the right size.

You do need to name the union and the choice on every access, which is
clutter unless you form and use a local pointer or in C++ reference,
or yuckily-global macros, but doesn't change/limit the semantics.
C99 has provisions for implementing the equivalent of Pascal
variant records.


Huh? The only thing new in C99 in this area is Flexible Array Member
in struct, which is nothing like Pascal variant record. OTOH, C++
derived or "subclass" types are functionally similar, though
notationally and conceptually reversed (specials "contain" common
instead of common "contains" specials).

- David.Thompson1 at worldnet.att.net
Nov 13 '05 #14

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

Similar topics

29
by: Damian Brown | last post by:
www.phpexpert.org
175
by: Lucas Raab | last post by:
One thing I've always kind of wondered is what is the average age of a Python programmer?? What age groups use Python?? Something to think about....
108
by: Zooko O'Whielacronx | last post by:
I'm a fan of Greg Ewing's PyGUI . I used it to code a simple game for my son , and enjoyed it. Programming with wxPython feels like programming with a C++ tool that has been wrapped in Python....
242
by: James Cameron | last post by:
Hi I'm developing a program and the client is worried about future reuse of the code. Say 5, 10, 15 years down the road. This will be a major factor in selecting the development language. Any...
176
by: basecamp | last post by:
just checking the average age of programmers using this group -- thanks
55
by: amanda992004 | last post by:
Excluding the factors of the brain capability, i.e I am not asking about this factor, if you are a single, aside from enjoying coding or debugging, how do you make time to eat properly, i.e...
7
by: guy | last post by:
Stirring up trouble here;) why is it that C# programmers try and denigrate VB.NET while VB.NET developers seem to have no problem with C# but just prefer VB.NET? I use both and this generally seems...
8
by: M_Mann | last post by:
Hello, Pls excuse if you consider this off-topic. Conceptual artists seek programmers here. We are authors of "Exhibition of Living Managers" (MANAGEX, www.managex.info) which is is global...
6
by: Biel | last post by:
Folks, One curiosity; - When can I affirm that one programmer C++ have the advanced level? - Is there some classical definition of parameters applied to C++ for define it?
1
by: geevaa | last post by:
http://www.phpbuilder.com/columns/kassemi20050606.php3 XMLHttpRequest and AJAX for PHP programmers James Kassemi Introduction: Although the concept isn't entirely new, XMLHttpRequest...
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
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: 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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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.