VC++ memory alignment example tutorial

  • 2020-04-02 02:40:13
  • OfStack

Memory is a very important skill in VC++ programming, this article is to illustrate the VC++ memory method. Specific analysis is as follows:

An overview,

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

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

#pragma pack() is a preprocessor that represents memory alignment. Layout control #pragma, which provides unconventional control flow information for compilers.

Two, the size of the structure of the rules

The structure size is an integer multiple of the number of processor bits and the number of bytes occupied by the longest data element in the structure.

For example, suppose the processor number is n and the number of bytes occupied by the largest data element in the structure is m.

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

Note: some operating systems are 64-bit, but the compiler is 32-bit, with 32 bits.


class A{
   int a;
   char b;
   short c;
};
sizeof(A) for 8 for 4 Integer multiples of omega. 

struct B{
   short a;
   short b;
   short c;
};

Sizeof (B) is 6 and sizeof(short) is 2.

Note: the only difference between structs and classes in C++ is that structs default to public members and classes default to private.


class X{
public:
  double a;
  float b;
  int c;
  char d;
};

Sizeof (X) is 20, and is an integer multiple of 4 (processor bits).

Third, # pragma pack (n)

The n in #pragma pack(n) defaults to 4, which is the processor number 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 an integer multiple of 1(#pragma pack(1)).


#pragma pack(1)
  class X{
  public:
    double a;
    int b;
    short c;
    char d;
  };

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


#pragma pack(4)
  class X{
  public:
    double a;
    int b;
    short c;
    char d;
  };

Sizeof of(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, which is an integer multiple of 8 (#pragma pack(8) or sizeof(double)).

Memory alignment

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

One is n in #pragma pack(n), and the other is the number of bytes taken by the element type, sizeof(type). The offset from the element's memory address to the starting address of the structure or class is an integer multiple of the decimal number.

For example, #pragma pack(n) defaults to 4 and has the following structure


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

The offset of the starting address of a from the starting address of the structure is 0, which is an integer multiple of sizeof(int).

The starting address of b is offset by 4 from the starting address of the structure, which is an integer multiple of sizeof(char).

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

The address of output a, b, c is

0043 fd68

0043 fd6c

0043 fd6e

You can see that the starting address of c is 2 bytes larger than the starting address of b, and b takes up 2 bytes, because the type of c is short, the size is 2, and the default of n is 4, sizeof(short). < N, so the offset is going to be an integer multiple of 2, which is 6.

Hope that this article described for everyone's VC++ programming help.


Related articles: