[Basic] Difference between Struct and Union

Reprinted from: https://blog.csdn.net/firefly_2002/article/details/7954458

1. Struct and Union have the following differences:

1. When storing multiple member information, the compiler will automatically allocate storage space to the first member of the struct. The struct can store multiple member information, while each member of the Union will use the same storage space and can only store the information of the last member. information.

2. They are all composed of multiple members of different data types, but at any one time, Union only stores one member that was selected first, and all members of the structure exist.

3. For the assignment of different members of Union, other members will be rewritten, the value of the original member will not exist, and the assignment of different members of the struct will not affect each other.

Note: The member variables of the structure need to be modified in many places. Only part of the member variables, then the union Union cannot be used, because all the member variables of the Union occupy one memory. eg: To assign values ​​to individual value fields in a linked list, you must use struct.

2. Example description

A struct is simply a collection of interrelated elements. It is said to be a collection. In fact, they are stored in memory in a sequential order, and each element has its own memory space. So in what order are they stored? In fact, it is stored in the order of the variables you declare. Let's look at an example:

struct sTest

{

int a; //sizeof(int) = 4

char b; //sizeof(char) = 1

shot c; //sizeof(shot) = 2

}x;

So it occupies at least 4+1+2 = 7 bytes in memory. However, the actual memory occupied is not 7 bytes, which involves the byte alignment.

The difference of a union is that all its elements share the same memory unit, and the size of the memory allocated to the union is determined by the size of the largest element of the type. The following memory is a double type size:

union uTest

{

int a; //sizeof(int) = 4

double b; //sizeof(double) = 8

char c; //sizeof(char) = 1

}x;

So the allocated memory size is 8 bytes.

Since it is memory sharing, of course, it cannot store the values ​​of multiple members at the same time, but can only store one of the values, which is the last value assigned to it, such as:

x.a = 3; x.b = 4.5; x.c = ‘A’;

This way you only see xc = 'A', and the others have been overwritten and lost their meaning.

eg: The Sample union contains only one of the members, either index or price.

union
Sample {

int index;

double price; };

If
Sample ss; ss.index =10;//
Only ss.index can be used from now on

If
Sample ss; ss.price=14.25;//
From now on, only ss.price can be used

In the use of union, if you assign a value to one of the members, and then use another member, it is undefined behavior, and the consequences are at your own risk.

struct members are independent of each other, a struct contains all members.

C/C++ code
struct Example
{
int index;
double price;
};

The Example structure contains two members. Modifying the index will not affect the price, and vice versa.
The members of the union share the memory space, and a union only contains one of the members.

Having said that, everyone should have understood the most critical difference between the two, which is nothing more than the allocation and use of memory units. However, there are still many tricks to use struct and union flexibly. For example, when the correlation of elements is not strong, union can be used completely to save memory size; struct and union can also be nested with each other.

3. Memory Alignment

union u
{
 double a;
 int b;
};

union u2
{
 char a[13];
 int b;
};

union u3
{
 char a[13];
 char b;
};

cost<

pragma pack (2)

union u2
{
 char a[13];
 int b;
};

union u3
{
 char a[13];
 char b;
};

pragma pack (8)

cost<

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324644236&siteId=291194637