answersLogoWhite

0


Best Answer

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.

User Avatar

Wiki User

8y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you use union in embedded system C programming?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

Can you use a printed circuit board on an embedded system?

I cannot imagine a modern embedded system that is not built on a printed circuit board.


Why is C plus plus not widely chosen for embedded process application?

There is no such term as embedded process application but you probably meant embedded system software. Embedded systems can make use of general purpose compilers, assemblers and debuggers, but these are often used in conjunction with more specific high-level programming tools and modellers, many of which emit C-code or raw assembly.


What are the necessity of embedded systems?

An embedded system is a special-purpose computer system designed to perform one or a few dedicated functions,[1] often with real-time computing constraints. It is usually embedded as part of a complete device including hardware and mechanical parts. In contrast, a general-purpose computer, such as a personal computer, can do many different tasks depending on programming. Embedded systems control many of the common devices in use today. Since the embedded system is dedicated to specific tasks, design engineers can optimize it, reducing the size and cost of the product, or increasing the reliability and performance. Some embedded systems are mass-produced, benefiting from economies of scale. Physically, embedded systems range from portable devices such as digital watches and MP4 players, to large stationary installations like traffic lights, factory controllers, or the systems controlling nuclear power plants. Complexity varies from low, with a single micro controller chip, to very high with multiple units, peripherals and networks mounted inside a large chassis or enclosure. In general, "embedded system" is not an exactly defined term, as many systems have some element of programmability. For example, Handheld computers share some elements with embedded systems - such as the operating systems and microprocessors which power them - but are not truly embedded systems, because they allow different applications to be loaded and peripherals to be connected.


How to use NS2 programming?

you don't need to use ns2 programming


What is the use of keyword new used in AWT programming?

what is the use of new keyword in awt programming

Related questions

Does an embedded system need operating system?

Yes. It is because the embedded system involves a hardware that can use a software. This is where we need an operating system.


Is C plus plus good for Embedded programming or not?

C and C++ programming is good for embedded programming. However, embedded implies long running, and that means the possibility of memory fragmentation. You will need to spend time working out or acquiring a method of memory management, such as the use of Smart Pointers and Garbage Collection. This is not a trivial task, but it is a necessary task.


Can you use a printed circuit board on an embedded system?

I cannot imagine a modern embedded system that is not built on a printed circuit board.


Why is C plus plus not widely chosen for embedded process application?

There is no such term as embedded process application but you probably meant embedded system software. Embedded systems can make use of general purpose compilers, assemblers and debuggers, but these are often used in conjunction with more specific high-level programming tools and modellers, many of which emit C-code or raw assembly.


When to use embedded system?

Embedded systems are used to manage a certain operation inside of a device. Embedded systems are often merely made to carry out this task repeatedly, but more advanced ones can take control of whole operating systems.


How do you use embedded system in robotics?

you have way too much spare time


What is the difference between FPGA and embedded system?

One of the differences is with respect to chip. VLSI- How to develop a chip Embedded- How to use the chips to produce an efficient system. VLSI - measurement of complexity of chip in terms of number of transistors in one circuit/chip Embedded is a computing platform that is formed by using proper logic to put together n number of VLSI


Why we use programming in computer system?

we use computer programing to operates different logic in computer system


What are programming languages use in Management information system?

my sql


What is meant by sap and what is use of it?

It stands for system application and programming.


How do you use the word embedded in a sentence?

There is a nail EMBEDDED in my sofa


Whynot you use any onther programming except shell programming?

Shell programming can get you only so far; after that you need to use another programming language (possibly even another scripting language) to get the job done. Shell programming is designed to work with the system interface and to help automate tasks; it isn't good for everything.