Bytes of storage memory and C-aligned structure

  

Environment: ubuntu64 bit + gcc
  
  

1. Each byte size data type representing

  Define a hollow structure, through which to view the discovery sizeof occupy memory byte 0 (vc ++ 6.0 to 1), the other types of data as the byte size of the memory of FIG.

#include <stdio.h>

struct Null{

};

int main()
{
	printf("int型:%ld \n",sizeof(int));
	printf("float型:%ld \n",sizeof(float));
	printf("char型:%ld \n",sizeof(char));
	printf("short型:%ld \n",sizeof(short));
	
	printf("空的struct型:%ld \n",sizeof(struct Null));
	
	return 0;
}

  Defines a non-empty structure, and then observe the size of memory bytes, the size of a structure found in each type of data is not simply added Size

#include <stdio.h>

struct Student{
	int name;    //4
	float score; //4
	char sex;    //1
	short num;   //2
};

int main()
{
	printf("struct Student字节数:%ld \n",sizeof(struct Student));
	
	return 0;
}

  In order to reflect this fun mystery, may wish for a way to measure a test to observe 1 and 2, found that even if the structure members are the same, but changed after sorting, the number of bytes occupied on the changes; 3 and 4 was observed , float + char and float + int actually as large as

  
  

2. The structure alignment problems

  Accessed know, the default storage structure employed to maximum number of bytes for byte alignment of the elements of its way, so-called byte alignment, refers to the data type byte size according to a fixed arrangement, to facilitate computer cpu, memory It reads. Structure data type are not the same, this time need byte alignment to improve efficiency of a computer read
  the number of bytes the fixed byte size, the maximum element is set to default compiler structures in the body, such as the above-described float + char and float + int, in fact, when stored either int or char, are based on four bytes to store, and it simply is, after the step value when the index variables that are a fixed number of bytes access memory
  to get the structure below to illustrate

struct Student1{
	int name;    //4
	float score; //4
	char sex;    //1
	short num;   //2
};
Types of Address 1 Address 2 Address 3 Address 4
int 1 byte 1 byte 1 byte 1 byte
float 1 byte 1 byte 1 byte 1 byte
char+short 1 byte 1 byte 1 byte filling

  Bytes corresponding to the maximum element is a fixed byte, float above or variable int maximum, it is 4 (the number of bytes stored in each line); for the third row of char + short, because the third when the beginning of the line, after completion of storage char byte, and three bytes, short enough to hold the next, it can be stored char and short on the same line. Line 3 4, a total of 12 bytes.
  For the same elements of structure 2, the following distribution memory

struct Student2{
	short num;   //2
	int name;    //4
	char sex;    //1
	float score; //4
};
Types of Address 1 Address 2 Address 3 Address 4
short 1 byte 1 byte filling filling
int 1 byte 1 byte 1 byte 1 byte
char 1 byte filling filling filling
float 1 byte 1 byte 1 byte 1 byte

  A beginning of the first line of pre-existing short type, 2 bytes, the remaining two bytes is not enough space and the next 4 bytes of type int, so only a separate line, Similarly, when the char- after the presence of the third line, the remaining 3 bytes is not enough space next float type, then a separate line. Four rows and four columns, a total of 16 bytes. It is possible to know why the order will affect the size of the internal structure of the structure element
  look at an example to be appreciated be appreciated, the memory mode is student1 3 rows and 8 columns, 2 rows STUDENT2 is eight, but only in Student3 Student2 increase on the basis of a char, it turned into 3 rows and 8 columns, and the influence shows different byte alignment of the memory is still very large (Note here that, if the last line is not full eight, it must be filled to 8, in the present embodiment of the structure of the memory size must be an integer multiple of 8, if)

  
  

3. Why do you want to align

(1) Structure of elements aligned with the access to the hardware mainly, i.e. the hardware itself has physical limitations, and if the alignment arrangement will increase access efficiency, which would significantly reduce the efficiency.

(2) itself is a physical memory device (DDR memory chips, DDR controller of the SoC), itself has some limitations: if the memory access every 4-byte aligned accesses, then the efficiency is the highest; if you unaligned access efficiency is much lower.

(3) there are many other factors and reasons leading to align the access we need. Some properties such as cache Cache, and other hardware (such as MMU, LCD displays) in some memory-dependent properties, it will access memory alignment requirements.

(4) Comparative unaligned access and an unaligned access: the expense of memory space unaligned access, exchange speed performance; not sacrifice access unaligned access speed performance in exchange for full use of memory space.

Summary: To put it plainly, is to access structure members with high efficiency, that is, read data more efficient (but there will also sacrifice a little bit of memory).
  
  Worth happy, the compiler has helped us to complete alignment, as previously mentioned, the compiler is the default maximum number of bytes as the basis for alignment, the advantage is to increase access efficiency, the disadvantage is some wasted memory, as waste more or less, it is to write the code to sort variables related to
  the general practice is that a small number of bytes variables close together as possible, rely on the same type of variables together like this would reduce the maximum extent possible memory waste the
  
  

4. The other byte alignment can customize it

  Share a face questions

How to make the following structure to maintain a byte alignment or eight-byte alignment?
struct Test{ int a ; int c ; char d; };

  In the compiler, we can set the alignment of a structure by the alignment instruction, there #pragma pack () with #pragma pack (n) (n = 1/2/4/8)

  #pragma pack (n) to tell the compiler member variable classes internal structure or offset with respect to the first address of a variable alignment, by default, the compiler aligned on natural boundaries, if desired variable natural alignment boundary is larger than the n, n aligned in accordance with, or aligned to a natural boundary; #pragma pack () / cancel specify the alignment, restore the default alignment /

  When tested on the graph (in win10 + VS2019), found that the use of a byte-aligned, the structure memory 11, exactly 4 + 4 + 2 + 1, without the use of the same directive #pragma pack structure, the default maximum bytes (ie, 4 bytes) sort; #prgma pack in many ways C environments are supported, but gcc although it could be, but is not recommended (the figure is in gcc environment)

  gcc supported, but not recommended above alignment instruction, the following illustration shows, in gcc environment, for two different structures, the use of a first 8-byte alignment, the second 4 bytes of the default, but the results is the same, in accordance with the analysis, if a set of 8-byte aligned, the size of the structure 16 should be as fishes, so the compiler can know the structure or the 4-byte alignment according to the process

  From the test results look like this: in gcc, if specified #pragma pack (N), then the N, N default alignment can not exceed a specified length, i.e., if the default alignment is 4, then, the value of N may be 1 2,4, 4 processing as later than 4. On Windows systems does not seem to limit this

  Recommended gcc alignment instructions __attribute __ ((packed)) and __attribute __ ((aligned (n)))

  Action of the __attribute__ ((packed)) is telling the compiler during compilation canceled structure optimized alignment, are aligned according to the actual number of bytes occupied, it is GCC-specific syntax.

  Above __attribute __ ((aligned (m))) tells the compiler to a structure or a joint or a class or type of variable (target) is assigned an address alignment when the address space. I.e. that, if the __attribute __ ((aligned (m))) action, then the variable type when allocating address space, the address which is stored in a certain alignment in accordance with a type of m bytes (m must be a power of two square). And the space it occupied, i.e. the size, is an integer multiple of m, at the time of application to ensure a continuous memory space, the address of each element are aligned in m byte.

  Below demonstrate, all three structures are the same, i.e. cancel the automatic alignment in a byte aligned manner, when the size of the structure and the size of the elements is; use __attribute __ ((aligned (m))) may be provided its alignment, two rows 8

  
  

End

  If wrong, correct me hope yet!

Published 17 original articles · won praise 33 · views 10000 +

Guess you like

Origin blog.csdn.net/fengge2018/article/details/104925581