C-Language byte alignment problem (32-bit system example)

Source: Internet
Author: User

1. What is alignment?

The memory space in modern computers is divided by byte (byte), theoretically it seems that access to any type of variable can start from any address, but the reality is that when accessing a particular variable, it is often accessed at a specific memory address, which requires all types of data to be spatially arranged according to certain rules, Instead of sequentially discharging one by one, this is the alignment.

2. Why should the computer be aligned?

The processing of storage space varies greatly with each hardware platform. Some platforms may not be able to access certain types of data only from certain addresses, which might not be the case for other platforms. But most often, if the data storage is not aligned according to the requirements of its platform, it can result in loss of access efficiency. For example, some platforms each read from an even address, an int (assuming 32-bit) if it is stored at the beginning of the even address, then a read cycle can be read out, and if it is stored in the location of the odd address, it may require 2 read cycles, And the high and low bytes of the two read results are pieced together in order to get the int data, which obviously drops a lot in the reading efficiency. This is also the game of space and time. In a Web-based program, it's important to master this concept: if you pass 2 of streams (such as structs) between different platforms, such as a struct, then you have to define the same alignment between the two platforms, or somehow make some mistakes, but it's hard to troubleshoot.

3. An example of an alignment

Normally, when we write a program, we don't need to think about alignment, and the compiler chooses the alignment strategy for the target platform. Of course, we can also notify the compiler to pass a precompiled instruction and change the alignment method for the specified data, such as writing a precompiled instruction #pragma pack (2), which tells the compiler to align two bytes.

However, because we generally do not need to care about this problem, if the editor is aligned with data storage and we do not understand it, it is often confusing to some questions. The most common is the sizeof result of the struct data structure, such as the following program:

#include <stdio.h>void  main () {    struct  a{        char  A;          Short b;         int c;    };     " size of struct A =%d \ n " sizeof (struct  A));}

The output is: 8 bytes.

If we make a slight change to the position of the variable declaration in the struct (without changing the variable itself), look at the following procedure:

#include <stdio.h>void  main () {    struct  a{        short  b;         int  C;         Char A;    };     " size of struct A =%d \ n " sizeof (struct  A));}

The output is: 12 bytes.

The problem comes out, they are all the same structure, why do they occupy different sizes of memory? To do this, we need to understand the alignment algorithm.

4. Alignment Algorithm

Due to the different platforms and compilers, the 32-bit, vc++6.0 system is an example of how the compiler aligns the members of the struct data structure.

First, we give four concepts:

1) The alignment value of the data type itself : is the self-aligning value of the base data type, such as a char type with its own alignment value of 1 bytes, and an int type with its own alignment value of 4 bytes.

2) Specify The alignment value: The Precompiled Command #pragma pack (value) specifies the alignment value of values.

3) The self-aligning value of a struct or class : The value that is the largest of its own alignment values in its members, such as the alignment value of struct A above is 4.

4) valid alignment values for data members, structs, and classes : Their own alignment values and the smaller values in the specified alignment values.

The structure is defined as follows:

struct a{    char  A;      Short b;     int c;};

A is char data, consumes 1 bytes of memory, short data, consumes 2 bytes of memory, int data, consumes 4 bytes of memory. Therefore, the self-aligning value of struct A is 4. Thus, A and b are composed of 4 bytes to align with the 4 bytes of C. And a has only 1 bytes, and a and b are emptied by one byte. We know that the structure type data is sequentially stored in the structure one after the other, so it is stored in the following way:

There is no data in the blank squares, it is a waste of memory space, a total of 8 bytes of memory.

In fact, in order to express "alignment" more clearly, we can think of the above structure as the following line arrangement:

For another struct definition:

struct a{        short  b;         int C;         Char A;    };

Its memory is stored in the following ways:

It is also imagined to be arranged in rows:

It can be seen that more space is wasted.

In fact, in addition to the structure, the entire program in the memory allocation for each variable will follow the alignment mechanism, will also create a waste of memory space. But we need to know that this kind of waste is worthwhile, because it is in exchange for efficiency improvement.

The above analysis is based on the program default alignment values, we can add predefined command #pragma pack (value) to customize the alignment value, such as #pragma pack (1), the alignment value becomes 1, when the memory is compact, there is no memory waste, But the efficiency is reduced. Efficiency is reduced because: if there is a larger number of variables (greater than 1), such as the int type, multiple read cycles are required to piece together an int data.

References:

[1] http://blog.sina.com.cn/s/blog_715de2f50100pgs3.html

[2] Http://baike.baidu.com/view/1523557.htm?fr=aladdin

C-Language byte alignment problem (32-bit system example)

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.