answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How can the union member share memory?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

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.


What are advantages and disadvantages of union in c language?

A union in C++ is similar to a C-style union in that the members of the union are all mapped to the same memory address. In other words, if you were to unify a long with a char, the char would be mapped to the first byte of the long. However, unlike C unions, C++ unions can be derived from other unions, structs or classes, as well as act as base classes for derived unions, structs and classes.


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


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.


Difference between struct and union?

A union is a way of providing an alternate way of describing the same memory area. In this way, you could have a struct that contains a union, so that the "static", or similar portion of the data is described first, and the portion that changes is described by the union. The idea of a union could be handled in a different way by having 2 different structs defined, and making a pointer to each kind of struct. The pointer to struct "a" could be assigned to the value of a buffer, and the pointer to struct "b" could be assigned to the same buffer, but now a->somefield and b->someotherfield are both located in the same buffer. That is the idea behind a union. It gives different ways to break down the same buffer area.The difference between structure and union in c are: 1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. 2. In union, one block is used by all the member of the union but in case of structure, each member have their own memory spaceDifference in their Usage:While structure enables us treat a number of different variables stored at different in memory , a union enables us to treat the same space in memory as a number of different variables. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion. There is frequent rwquirement while interacting with hardware to access access a byte or group of bytes simultaneously and sometimes each byte individually. Usually union is the answer.=======Difference With example***** Lets say a structure containing an int,char and float is created and a union containing int char float are declared. struct TT{ int a; float b; char c; } Union UU{ int a; float b; char c; }sizeof TT(struct) would be >9 bytes (compiler dependent-if int,float, char are taken as 4,4,1)sizeof UU(Union) would be 4 bytes as supposed from above.If a variable in double exists in union then the size of union and struct would be 8 bytes and cumulative size of all variables in struct.Detailed Example:struct foo {char c;long l;char *p;};union bar{char c;long l;char *p;};A struct foo contains all of the elements c, l, and p. Each element isseparate and distinct.A union bar contains only one of the elements c, l, and p at any giventime. Each element is stored in the same memory location (well, they allstart at the same memory location), and you can only refer to the elementwhich was last stored. (ie: after "barptr->c = 2;" you cannot referenceany of the other elements, such as "barptr->p" without invoking undefinedbehavior.)Try the following program. (Yes, I know it invokes the above-mentioned"undefined behavior", but most likely will give some sort of output onmost computers.)======= Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. In union,one block is used by all the member of the union but in case of structure, each member have their own memory space.Union allocates the memory equal to the maximum memory requried by the member of the union but structure allocates the memory equal to sum of the memory allocated to its each individual members.In Union, one block is used by all the member of union but in case of structure, each member have their own memory space.Union allocates the memory equal to the maximum memory requried by the member of the union but structure allocates the memory equal to total memory requried by the members. In Union, one block is used by all the member of union but in case of structure, each member have their own memory space.

Related questions

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.


What are advantages and disadvantages of union in c language?

A union in C++ is similar to a C-style union in that the members of the union are all mapped to the same memory address. In other words, if you were to unify a long with a char, the char would be mapped to the first byte of the long. However, unlike C unions, C++ unions can be derived from other unions, structs or classes, as well as act as base classes for derived unions, structs and classes.


Is Bulgaria a member of the European Union?

No, Kosovo is not a member of the European Union.


What are the advantages of using Unions in a structure in C?

In case of structure each variables have their own storage location whereas for unions all the variables share a common memory location. In structure all the variables can be handled at a time but for union only a member can be handled at a time.


Is Switzerland a member of the the European Union?

No, Switzerland is not a member of the European Union.


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


What is the relationship between the European Union and the member nations?

The European Union (EU) is a political and economic union of 27 member nations in Europe. The member nations voluntarily join the EU and share sovereignty with the EU institutions. The EU has authority over certain policy areas, while others remain the responsibility of individual member nations. The EU and member nations work together to shape policies, regulations, and laws that affect the European Union as a whole.


Is azerbajan a member of the European union?

no it is not a member of the European union


How do you Declare and initialized structure list different between union and structure?

C unions are also like structures. We have to allocate more memory when we use stucture.Because, memory needs to be allocated to all structure variables. Where as in Union, less memory is enough to allocate. Because, memory is allocated for only one member which occupies more memory among all members in the union. But, both have their own advantages and disadvantages.I came across one website which gives fair idea of union and structure difference and good sample programs and good explanationsC unionC structure


Is Georgia a member of the European union?

Georgia is not a member of the European Union.


Do you need a memory card to game share on a PSP?

YES, you do need a memory card to game share on a PSP!!!


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.