Union + structure + size end + byte alignment

Source: Internet
Author: User
The difference between a union and a struct is that each member of a struct variable is assigned a separate memory area, and each member of the union variable has the same storage start address, so the union variable can store only one member at any one time. When the system allocates space for union variables, the memory space is allocated on the size of the member that requires maximum storage.

A union is called a special class (because the compiler cannot know the type of the member without constructors and destructors, so the federated object is not generated by the constructor). So called a special Class).

Example 1:

#include "stdafx.h"
#include <iostream>
using namespace std;
int _tmain (int argc, _tchar* argv[])
{   
	//In Windows, the stack is a data structure that extends to a low address and is a contiguous area of memory. CH is assigned to memory as a whole, in ch ch[0] stored in low address;
	Char ch[2];
	int i;
	COUT.UNSETF (ios_base::d EC);
	COUT.SETF (Ios_base::hex);
	cout<< (int) & (ch[0]) <<endl;
	cout<< (int) & (ch[1]) <<endl;
	cout<< (int) &i<<endl;


	Union Node
	{short
		sVar;
		Char cvar[2];
	node node;
	node.cvar[0]=2;
	Node.cvar[1]=1;

	COUT.SETF (ios_base::d EC);
	cout<<node.svar<<endl;


	System ("pause");
	return 0;
}
Analytical:
The members of a struct variable are also assigned separate memory areas, and each member of the union variable has the same storage start address, so the union variable can store only one member at any one time. A consortium is a space-saving class, a memory-saving mechanism
Short a takes up two bytes
Char ch[2] occupies two bytes
So here's the thing: assign two bytes to only one struct object, and when the array is assigned, you can access the same memory through another member. 0000 0001 0000 0010 = 2+256=258

Detailed explanation of structure body
<1> arrays are a representation of the emergence of a clearer record of a finite number of data of the same type; a structure is a representation that is born to more clearly record an object with different types of attribute attributes.
Unions and structs are composed of several different data type members, but at any one time, only a selected member is stored in the Union, and all members of the struct exist. In other words, the structure is a "horizontal" expansion of the array, and the joint is the convergence of the structure in the "longitudinal". There is a certain reason for the appearance of everything, and it can be established by logical reasoning.
<2> in C + + The structure is not much different from the class. The only obvious difference is that struct default is public and class defaults to private.
The definition of <3> structure is as follows:

1//The first definition of struct type variable name mode:
2 struct student//struct is the key word and cannot be omitted. Student is the variable name of the struct for the defined type, which is generally required to "see the name, know the meaning"//
3 {
4 int num; The number is defined by the integer variable name num/
5 char name[30]; The name is defined with the variable name of the character array, length 30//
6 char sex; Sex is defined by the variable name sex with character type//
7 int age; Age is defined by the variable name of integer type//
8 float score; The result is a score definition of the variable name of the real type//
9 Char addr[30]; The address is defined by the variable name addr of the character array type//
10};
one struct student stu1,stu2; To define a struct body type variable
12
13
14
15//second definition of struct type variable name mode:
16
struct student//struct is the key word and cannot be omitted. Student is the variable name of the struct for the defined type, which is generally required to "see the name, know the meaning"//
18 {
int num; The number is defined by the integer variable name num
Char name[30]; The name is defined with the variable name name of the character array, with a length of 30
char sex; Gender-defined variable name sex with character type
int age; Age defined with variable name of integral type
Score float; Score definition of the variable name of the real type of the score
Char addr[30]; The address is defined with the variable name addr of the character array type
Student1,student2;
26//Direct definition of struct type variable, variable named Student1,student2
27
28
29
30//The members of a third structure can also be some kind of struct/
struct Date//date structure//
32 {
month int; Integral type Month month//
int day; Shaping Type day//
int year; Shaping type year//
36};
37
struct student//struct is the key word and cannot be omitted. Student is the variable name of the struct for the defined type, which is generally required to "see the name, know the meaning"//
39 {
int num; The number is defined by the integer variable name num/
30 Char name[]; The name is defined with the variable name of the character array, length 30//
Sex char; Sex is defined by the variable name sex with character type//
int age; Age is defined by the variable name of integer type//
Score float; The result is a score definition of the variable name of the real type//
addr[Char 30]; The address is defined by the variable name addr of the character array type//
struct date birthday; Birthday with struct type variable name birthday definition//
47};

Assignment of <4> structure body
1 struct student//struct is the key word and cannot be omitted. Student is the variable name of the struct for the defined type, which is generally required to "see the name, know the meaning"//
2 {
3 int num; The number is defined by the integer variable name num/
4 Char name[30]; The name is defined with the variable name of the character array, length 30//
5 char sex; Sex is defined by the variable name sex with character type//
6 int age; Age is defined by the variable name of integer type//
7 float score; The result is a score definition of the variable name of the real type//
8 Char addr[30]; The address is defined by the variable name addr of the character array type//
9};
struct Student stu1 = {1001, "Liu Yong", ' M ', DE, 95.5, "Shan Xi Sheng"}


Size-End judgment

First look at an interview question: please write a C function, if the processor is Big_endian, then return 0, if Little_endian, then return 1.
Analysis:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.