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

Advantage of unions.

nirmalsingh
218 100+
What is the advantage of unions in C++?
What are the situations to use unions?
With Cheers
-Nirmal
Oct 29 '08 #1
5 5780
Meetee
931 Expert Mod 512MB
Union are basically used to save space.
Expand|Select|Wrap|Line Numbers
  1. struct s {
  2. int temp1;
  3. char temp2;
  4. } s1;
  5.  
  6. union u {
  7. int temp1;
  8. char temp2;
  9. } u1;
when you take the size of both then you will see the difference.
in case of structure sizeof(s1) = 5 whereas in caes of union sizeof(u1) = 4

Hope this helps! :)
Oct 29 '08 #2
r035198x
13,262 8TB
This question has been asked here several times. Example .
Oct 29 '08 #3
the amount of memory space conserved is so small to the multi-gigabyte computers seen today to the extend that i would like to ask : is the a redundant c++ feature ?
Sep 12 '09 #4
Banfa
9,065 Expert Mod 8TB
No its not redundent, remove the union and all legacy code that suddenly uses it would not only stop working but would require a major rewrite. You can not remove things from the language only add things.

If on the other hand you mean is there a place for the union in a modern program design then the answer is probably no much. However if you had asked the same question 20 years ago of C programmers then the answer would probably have been the same. The union has always been one of the less used language features in my experience and usage has never been about saving space but rather either an easy way to create a polymorphic pointer or a convenient method for converting binary variables to/from a byte stream.
Sep 13 '09 #5
Unions can be thought of "variants" in other programming languages.
Sep 13 '09 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

15
by: David | last post by:
Some developers in my group are using UNIONS to define their data types in a C++ program for an embedded system. Are there any pro and cons in doing this when you can define a CLASS to do the same...
8
by: SteveM | last post by:
The general consensus I am getting is that nobody really uses unions much (engineers here at work) but this is an academic exercise for me so I am looking for an answer (I know there may be better...
6
by: Neil Zanella | last post by:
Hello, I would like to know whether the following C fragment is legal in standard C and behaves as intended under conforming implementations... union foo { char c; double d; };
16
by: Tim Cambrant | last post by:
Hi. I was reading up a bit on the features of C I seldom use, and I came across unions. I understand the concept, and that all the contained variables etc. share the same memory. Thus, when a new...
23
by: rohit | last post by:
Hi, In my couple of years of experience, I have never found a single instance where I needed to use unions and bitfields(though I have used structures).I was just imagining where would these find...
4
by: uralmutlu | last post by:
Hi, I was wandering if I can have classes in unions? I basically have source code in a format very similar to: union example { ClassA variable1; ClassB variable2; };
67
by: bluejack | last post by:
A recent post asking for help with unions reminded me of this component of the C language that I have only used a couple of times, and those almost entirely out of personal whim -- Unions for the...
26
by: Old Wolf | last post by:
Ok, we've had two long and haphazard threads about unions recently, and I still don't feel any closer to certainty about what is permitted and what isn't. The other thread topics were "Real Life...
11
by: pereges | last post by:
Hello, can some one please guide me a little into using unions. I read about unions in K & R but I am finding it difficult to apply to my problem at hand. I want to save up some space by using...
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:
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...
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...
0
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,...
0
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.