The alignment strategy of the Microsoft C compiler under the Win32 platform

Source: Internet
Author: User
Tags microsoft c

Introduction

first Look at a C language under the structure of the small program.

#include <stdio.h>struct  studentinfo {    char  i;     int J;}; void Main () {        printf("%d\n",sizeof(struct  Studentinfo));    }

Output results: 8

Puzzled, thought to be 5. In fact, this involves the computer memory alignment problem, in the computer composition principle is introduced.

Overview

Many real computer systems limit the location of basic types of data in memory, and they require that the value of the first address of the data be a multiple of a number k (usually 4 or 8), which is called memory alignment, and this k is called the Zimo number of the data type (alignment modulus). When the ratio of one type S pair of Zimo number to the Zimo number of another type T is an integer greater than 1, we call the alignment requirement of type S stronger (strict) than T, while the T is weaker than s (loose). This mandatory requirement simplifies the design of the transfer system between the processor and the memory and improves the speed of reading the data. For example, such a processor, every time read and write memory from a certain 8 times times the address of the beginning, read out or write 8 bytes of data, if the software can guarantee that the double type of data from 8 times times the address of the beginning, then read or write a double type of data only need a memory operation. Otherwise, we might need two memory operations to complete this action, because the data might just be across two 8-byte blocks of memory that meet the alignment requirements. Some processors can make an error if the data does not meet the alignment requirements, but Intel's IA32 architecture processor works correctly regardless of whether the data is aligned. But Intel advises that if you want to improve performance, all the program data should be aligned as much as possible.

Memory alignment under VC editor

The ANSI C standard does not stipulate that adjacent declared variables must be contiguous in memory. For program efficiency, memory alignment issues are handled flexibly by the compiler, which can result in some padding bytes between adjacent variables. For the base data type (int char), the memory space they occupy has a definite value under a certain hardware system, so we'll just consider the memory allocation of the struct member.

The alignment strategy for the Microsoft C compiler (Cl.exe for 80x86) under the Win32 platform:
1) The first address of a struct variable can be divisible by the size of its widest base type member;
Note: When the compiler opens up space for the struct, it first finds the widest basic data type in the struct, and then looks for the location where the memory address can be divisible by the basic data type as the first address of the struct. The size of the widest base data type as described above for the Zimo number.
2) the offsets (offset) of each member of the struct relative to the first address of the struct are an integer multiple of the member size, and if necessary, the compiler adds padding bytes between the members (internal adding);
Note: Before creating space for a member of a struct, the compiler first checks that the first address of the pre-open space is an integer multiple of this member relative to the first address of the struct, if so, the member is stored and, conversely, a certain byte is filled between this member and the previous member to achieve the requirement of an integer multiple. That is, the first address of the pre-opening space is shifted a few bytes.
3) The total size of the struct is an integer multiple of the size of the structure's widest base type member, and if necessary, the compiler adds padding bytes (trailing padding) after the last member.
Note: The size of the structure consists of padding bytes, and the last member satisfies the above two and must also satisfy the third, otherwise it must be padded at the last few bytes to achieve this requirement.

Memory alignment rules for structs
1) struct members are stored in memory in the order of low addresses to high addresses, which are stored in the order declared
2) The address of each member must satisfy: is the integer multiple of sizeof (the member)
3) The total number of bytes is an integer multiple of the maximum number of bytes in the built-in (that is, the base type) member

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.