473,671 Members | 2,281 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 12333

"Razmig K" <st*********@po stmaster.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*********@pos tmaster.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*********@po stmaster.co.uk> wrote in message
news:3c******** *************** *@posting.googl e.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*********@ro cketmail.com> wrote in message
news:bh******** **@news.apple.c om...
[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******@vrml3 d.com> wrote:
Peter Ammon <pe*********@ro cketmail.com> wrote in message
news:bh******** **@news.apple.c om...
[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*********@ro cketmail.com> wrote in message
news:bh******** **@news.apple.c om...
[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.ne t
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 13 '05 #9
Razmig K <st*********@po stmaster.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

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

Similar topics

29
2896
by: Damian Brown | last post by:
www.phpexpert.org
175
7076
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
7964
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. Programming with PyGUI feels like programming with a real Python tool. If you're developing a commercial application in Python, wxPython is currently the only option that offers native widgets on w32. It would be a boost for Python if PyGUI got...
242
13338
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 comments on past experience, research articles, comments on the matter would be much appreciated. I suspect something like C would be the best based on comments I received from the VB news group. Thanks for the help in advance James Cameron
176
8252
by: basecamp | last post by:
just checking the average age of programmers using this group -- thanks
55
4467
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 healthily w/o spending big bucks at special healthy food places and also take care of life's daily chores w/o feeling like a robot. Any time for social activites with people other than programmers? Is feeling like a robot a typical description of a...
7
1409
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 to be the attitude, not with everyone obviously!
8
2165
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 conceptual art project, performed in world's leading contemporary art centres. Art objects at MANAGEX are real employed managers, who volunteer to exhibit themselves in a gallery setting. Our new project is "Exhibition of Living Programmers"
6
2007
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
4027
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 technology is implemented on more sites now than ever. Compatibility is no longer an issue (IE, Mozilla and Opera all support it), and the benefits to using it are amazing. There are too many PHP programmers avoiding any
0
8476
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8917
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7437
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6229
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2051
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.