answersLogoWhite

0


Best Answer

it is not necessary to have same size for all members in an union ..because unions holds different data types..

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: IS it necessary to have same size for all members in an union Explain?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the difference between a class and a union in c plus plus?

The data members of a class are each allocated separate memory addresses. A union's members are all mapped to the same address. If you change the value of one union member, you change them all.


How memory space is occupied by union variables in C programming language?

A union is an aggregate of members that share the same memory address. The size of a union is determined by the largest member.


How can the union member share memory?

All union members are allocated the same memory address. The amount of memory allocated is determined by the largest member of the union. Only one value can be stored in a union. If we change the value of any one member, we change the value for all members. The member type determines how the current value is interpreted.


Which one is better between structure and union?

Sub:- C programming class:- f.y.b.sc(cs) name:-sintu mehta Union:- all member of the union share is storage in the computer memory 2. Only one member can be active at a time 3. Only first union variable can be initialized 4. Conservation the memoy


How do you use union in embedded system C programming?

All members of a union are assigned the same memory address. As such, assigning to any member of a union changes the value of all members in that union. The total number of bytes allocated to a union is equal to the length of the largest member of that union. Where the members differ in length, assigning to a member will completely overwrite all the smaller members but will only partially overwrite any larger members. After assigning a value to a union member, that member is said to be "active". The active member remains active until a value is assigned to another member. However, a union does not keep track of which member is currently active; the onus is entirely upon the programmer to ensure the correct member is accessed. In some cases, keeping track of the active member is not necessary. For instance, consider the following union: union u { int i; char c[sizeof(int)]; }; This union has two members, both of which are the same length, sizeof(int). We can read and write integer values through the u::i member just as we can any ordinary integer, however the u::c member allows us to read and write the individual bytes within the integer. Since both members are integral types, there is no need to keep track of which member is currently active; it simply provides two methods of accessing the same memory. Thus if we need to access a multi-byte value at the byte level, a union provides the most intuitive method of achieving it without resorting to type casting a pointer. Where members differ in size, we often need to keep track of which member is active. One method of achieving this is by embedding the union in a struct along with an enum member to keep track of the active member of the union: typedef enum a_t {num, arr} a; union u { int number; int* array; }; struct s { u data; a active; }; In the above example, we can choose to store a single value in s::u::number or we can choose to store multiple values in the memory pointed to by s::u::array. However, if we store a value in s::u::number and then attempted to dereference the s::u::array pointer, we incur undefined behaviour because a) an int and a pointer (to any type) are not guaranteed to be the same length and b) the number may not contain a valid address. Therefore it is important that whenever we write to s::u we update s::a to reflect which member was written to and we must read s::a before accessing s::u. In addition, we must be sure to release any resources currently allocated to the s::u::array before assigning a new value to s::u. In this example we don't actually gain any benefit by using a union because we end up using just a much memory as we would if the two members were allocated to separate addresses, because of the need to keep track of the active member. However, if the union has three or more members, we begin to save memory because all the "inactive" members cost nothing.

Related questions

Is it necessary to have same size for all members in an union?

it is not necessary to have same size for all members in an union ..because unions holds different data types..


What are the advantages of union over structures?

union occupies a lesser memory space compared to structure as the same memory block is used for all the members in union


What is the difference between a class and a union in c plus plus?

The data members of a class are each allocated separate memory addresses. A union's members are all mapped to the same address. If you change the value of one union member, you change them all.


How memory space is occupied by union variables in C programming language?

A union is an aggregate of members that share the same memory address. The size of a union is determined by the largest member.


Is the Cihnese Government the same as th European Union?

No. The Chinese government is the government of China. The European Union is an organisation with 27 of Europe's countries as members.


What is the same currency used by many European countries in the EU?

17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.17 of the 27 members of the European Union use the Euro as their currency.


Are french and Britain the same?

France and Britain are two separate countries but they are both members of the European Union.


How did eugene v. debs organize the workers of the American railroad union?

The members worked in the same industry


How did eugene V. debs organized the workers of the American railway union?

The members worked in the same industry


Describe the conditions necessary for a measure of variation to have the value zero?

The measurements for all members of the sample are the same.


What is the procedure for dissolving a civil union in Delaware?

In Delaware, the procedure for dissolving a civil union is the same as for dissolving a legal marriage. It is necessary to obtain a divorce decree signed by a judge.


Can a union steward be voted out by the members?

how stewards are chosen and removed would depend on which union you are talking about. It is not always done in the same way for each union and I don't know if any unions allow a steward to be voted out.