VC + + Memory Alignment Instance Tutorial _c language

Source: Internet
Author: User
Tags pack

Memory on it is a VC + + programming in a very important skill, this article is an example of VC + + implementation of the method of memory. The specific analysis is as follows:

I. Overview

We often see the problem of finding the value of sizeof (a), where a is a struct, a class, or a consortium.

To optimize CPU access and optimize memory and reduce memory fragmentation, the compiler has some rules for memory alignment. However, different compilers may have different implementations, this article is only for VC + + compilers, the IDE used here is VS2012.

The #pragma pack () is a preprocessing that represents memory alignment. Layout control #pragma, which provides unconventional control flow information for the compiler.

Second, the size of the structure of the rules

The size of a struct is an integer multiple of the number of processor digits and the smaller of the number of bytes in the structure body that is the longest data element.

For example, assuming that the number of processor bits is n, the maximum data element in the structure occupies the number of bytes m.

The processor is 32 bits, n = 4, and the maximum data type in the structure body is short,m = 2; n > m; structure body size is an integral multiple of M, and vice versa.

Note: Some are 64-bit operating systems, but the compiler is 32-bit, with a number of digits at 32.

Class a{
 int A;
 Char b;
 Short C;
};
sizeof (A) is 8, and is an integral multiple of 4.

struct b{short
   A;
   Short B;
   Short C;
};

sizeof (B) is 6, and is an integer multiple of 2 (short).

Note:There is only one difference between a struct and a class in C + +, that is, the struct member is public by default, and the class defaults to private.

Class x{public
:
  double A;
  float B;
  int C;
  char D;
};

sizeof (X) is 20, and is an integer multiple of 4 (the number of processor digits).

Third, #pragma pack (n)

The N in #pragma pack (n) defaults to 4, that is, the processor bit 32, but we can define its size ourselves.

#pragma pack (1)
class a{public
:
  int A;
  Char b;
  Short C;
};

At this point sizeof (A) is 7 and is an integer multiple of 1 (#pragma pack (1)).

#pragma pack (1)
  class x{public
  :
    double A;
    int b;
    Short C;
    char D;
  };

sizeof (X) is 15 and is an integer multiple of 1 (#pragma pack (1)).

#pragma pack (4)
  class x{public
  :
    double A;
    int b;
    Short C;
    char D;
  };

sizeof (X) is 16 and is an integer multiple of 4 (#pragma pack (4)).

#pragma pack (8)
  class x{public
  :
    double A;
    int b;
    Short C;
    char D;
  };

sizeof (X) is 16 and is an integer multiple of 8 (#pragma pack (8) or sizeof (double).

Four, the memory alignment

The memory address of the data element in the structure is determined by two factors.

One is n in the #pragma pack (n), the second is the byte number of the element type, the sizeof (type), the smaller one, the offset of the element memory address to the structure or the starting address of the class is an integer multiple of the smaller number.

For example, the #pragma pack (n) defaults to 4, with the following structural bodies

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

The starting address of a begins with an offset of 0 from the starting address of the struct, and is an integral multiple of sizeof (int).

The start address of B has an offset of 4 from the start address of the struct, and is an integer multiple of sizeof (char).

The start address of C is offset by 5 from the start address of the struct, and is not an integer multiple of sizeof (short), so its starting address offset will be 6 instead of 5.

Output A, B, and C addresses as

0043fd68

0043fd6c

0043fd6e

You can see that the starting address of C is 2 bytes larger than the starting address of B. B takes up 2 bytes, because the type of C is short, the size is 2, and N defaults to 4,sizeof (short) < n, so the offset should be an integer multiple of 2, and this is 6.

I hope this article describes the VC + + program to help.

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.