Structure and byte alignment in C Language

Source: Internet
Author: User

Struct

Struct Concept

★Computer programming languages all have the concept of data types.

★Data types include basic and user-defined types.

★In C, the basic types include int, double, and char.

★The struct type belongs to the User-Defined type and is a complex type composed of basic types.

★A struct type can be defined as a basic type. Define struct types and struct type variables

★Let's take a look at the general form of defining struct types:

Struct identifier

{

Struct element;

};

As follows:

Struct StructDemo

{

Char c;

Int I;

};

★Method 1 of defining struct type variables:

Define both types of variables as follows:

Struct StructDemo

{

Char c; int I;

}

Sd1, sd2;

In this way, two struct variables sd1 and sd2 are set. The two variables are of the struct StructDemo type.

★Method 2:

Define the variable after the type is defined, as follows:

Struct StructDemo

{

Char c; int I;

};

This definition indicates that a new type has been defined, and the type specifier is struct StructDemo.

So we can use this to define variables. As follows:

Struct StructDemo sd1, sd2;

★Method 3 for defining struct type variables:

Rename the type specifier.

If you are a friend of C language, you are not supposed to be unfamiliar with typedef. You can use it to give the type description an alias. This name has the same meaning as the original name. For example:

Typedef int zhengxing;

So int I and zhengxing I mean the same. The same principle can also be applied to custom data types. T

Ypedef struct StructDemo SD;

SD sd1, sd2;

Struct type Initialization★Initialize the definition at the same time, for example:

Struct StructDemo sd1 = {'A', 10}, sd2 = {'B', 12}; SD sd1 = {'A', 10}, sd2 = {'B ', 12 };

Struct StructDemo {char c; int I;} sd1 = {'A', 10}, sd2 = {'B', 12 };

★Initialize after definition, for example:

Sd1 = {'A', 10 };

Sd2 = {'B', 12 };

Sd1.c = 'D ';

Sd1. I = 20;

Struct use Example 1 we will present the definition of struct, define struct type variables, and initialize it with an actual program. The linux system installed on this machine is OpenSuSe, so we open OpenSuSe, open the terminal, and use the vim editor StructDemo01.c as follows:

Compile and run the program as follows: The structure size problem the Data Type in computer programming language occupies a certain amount of memory space in the computer during use. The structure type is user-defined, it also belongs to the data type. What is the size of the space it occupies in the computer memory? Let's take a look. We define a struct type as follows:

Struct StuctDemo {char c; int I ;};

First, we try to estimate the memory space occupied by this struct type. We know that a char type occupies 1 byte, while an int type occupies 4 bytes, can we assume that this struct type occupies 5 bytes of memory? Next we will use a program to verify this problem, which is also a common method for us to learn about computers, fast, convenient, and accurate. Before giving an example, we will talk about the next keyword sizeof. sizeof can tell us how much memory space a data type occupies. The specific form is: sizeof (type specifier) to get the number of bytes. Next we will create a C language source program file named Demo02.c. We will use vim to open and edit it as follows: We compile and run it: The result is as expected, and the result is 8. Why not 5? In modern computers, memory space is divided by byte. Theoretically, it seems that access to any type of variables can start from any address, however, the actual situation is that access to specific types of variables is often performed at a specific memory address, which requires various types of data to be arranged in the space according to certain rules, instead of sequential emissions, this is alignment. The processing of buckets varies greatly by hardware platform. Some platforms can only access certain types of data from some specific addresses. For example, some architectures may encounter errors when the CPU accesses a variable that is not aligned, so in this architecture, programming must ensure byte alignment. this may not be the case for other platforms, but the most common problem is that alignment of data storage according to the requirements of their platforms may cause a loss of access efficiency. For example, some platforms start from the even address each time they read data. If an int type (assuming a 32-bit System) is stored at the beginning of the even address, then a read cycle can read the 32bit data. If the data is stored at the beginning of the odd address, two read cycles are required, the 32-bit data can be obtained only when the high and low bytes of the two read results are pieced together. In fact, the details of byte alignment are related to the specific compiler implementation, but generally the three criteria are met: 1) the first address of the struct variable can be divisible by the size of its widest basic type member; 2) The offset of each member of the struct to the first address of the struct is an integer multiple of the size of the member. If necessary, the compiler adds the padding byte between the members. For example, the address space of the second struct variable above. 3) the total size of the struct is an integer multiple of the size of the widest basic type of the struct. If necessary, the compiler will add the padding byte after the last member. After reading this text, we believe it is easy to understand why 8 is not 5, because of the needs of byte alignment.

Related Article

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.