Introduction The essence of C language is that the program needs to be able to manipulate any memory of its own program space at will. In this regard, C language programming is machine-oriented. Therefore, it is very important to be familiar with C's data storage. This article describes how to store c-language data structures in memory. Conclusion only the following conditions are verified: number of operating system digits: 32 Compiler: GCC version 4.6.3 (UBUNTU) mingw32-gcc data storage method: small-end storage large-end storage and small-end storage refer to the high or low data storage of memory addresses. For example, int I = 0x12345678 large-end system storage: Small-end system storage: data needs to be converted (in byte order) when it flows from different systems to different systems ), the most typical is that the large-end bytes of the network are converted to x86 small-end bytes. The data structure of alignment C language is generally a polymer of different data types, which contains different data types, because the data structure does not provide so many functions as the C ++ class, its storage is very simple. Only store data in order. For example, the following data structure:
struct str4_t{unsigned char c1[1];unsigned char c2[2];unsigned char c3[3];unsigned char c4[5];}str4;
It is stored in the memory in order.
The size of sizeof (str4) is 11, 1 + 2 + 3 + 5 = 11. How is it? Easy! Don't worry, look at the following data structure:
struct str3_t{unsigned char ac[3];unsigned short s;}str3;
It should be 3 + 2 = 5. But in fact, sizeof (str3) is 6. Why? Let's see how the memory is stored:
The yellow area is AC, and the light blue area is S. White does not store data. It was thrown out, which is the legend
Byte alignment. At the beginning, my operating system is 32-bit, the data read by the operating system at a time in our program space is 32 bits (our program memory is virtualized by the operating system), and 32 bits are 4 bytes, if we remove the white ones above, we need to access s twice to get the value of S, which is a waste of time. So it must be aligned in byte order. Let's look at the following data structure:
struct str2_t{unsigned short s;unsigned int i;unsigned char c;}str2;
What should this be? The principle of byte order alignment should be 4 + 4 + 1 = 9. Unfortunately, the result is 12. Is it because the data structure of str2 requires byte order alignment? This is not the case. The str3 above is 6 or a multiple of 4. Why? Baidu and Google found another rule:The overall size of the struct must be an integer multiple of the members occupying the maximum byte of the struct.. Str2 is up to the int type, which is 4 bytes. Therefore, it can only be 12 bytes. Appendix: Test code
#include <stdio.h>#include <string.h>#define BUFFERSIZE 120unsigned char *p;struct str1_t{unsigned char c;unsigned short s;unsigned int i;unsigned long l;}str1;struct str2_t{unsigned short s;unsigned int i;unsigned char c;}str2;struct str3_t{unsigned char ac[3];unsigned short s;}str3;struct str4_t{unsigned char c1[1];unsigned char c2[2];unsigned char c3[3];unsigned char c4[5];}str4;int main(){int in,i,len;in = 0x12345678;p = (unsigned char *)∈len = sizeof(in);printf("int size:%d\r\n", len);for(i=0; i<len; i++)printf("%2X ", *(p+i));printf("\r\n\r\n");str1.c = 0x12;str1.s = 0x34;str1.i = 0x56;str1.l = 0x78;p = (unsigned char *)&str1;len = sizeof(str1);printf("str1 size:%d\r\n", len);for(i=0; i<len; i++)printf("%2X ", *(p+i));printf("\r\n\r\n");str2.s = 0x12;str2.i = 0x34;str2.c = 0x56;p = (unsigned char *)&str2;len = sizeof(str2);printf("str2 size:%d\r\n", len);for(i=0; i<len; i++)printf("%2X ", *(p+i));printf("\r\n\r\n");str3.ac[0] = 0x12;str3.ac[1] = 0x34;str3.ac[2] = 0x56;str3.s = 0x78;p = (unsigned char *)&str3;len = sizeof(str3);printf("str3 size:%d\r\n", len);for(i=0; i<len; i++)printf("%2X ", *(p+i));printf("\r\n\r\n");str4.c1[0] = 0x1;str4.c2[0] = 0x2;str4.c2[1] = 0x3;str4.c3[0] = 0x4;str4.c3[1] = 0x5;str4.c3[2] = 0x6;str4.c4[0] = 0x7;str4.c4[1] = 0x8;str4.c4[2] = 0x9;str4.c4[3] = 0xa;str4.c4[4] = 0xb;p = (unsigned char *)&str4;len = sizeof(str4);printf("str4 size:%d\r\n", len);for(i=0; i<len; i++)printf("%2X ", *(p+i));printf("\r\n\r\n");unsigned short s = 0xF1;unsigned int i2 = 0xF2;unsigned char c = 0xF3;unsigned char buffer2[20];//avoid the memery overflowmemset(buffer2, 0 , sizeof(buffer2));struct str2_t *pstr2;pstr2 = (struct str2_t *)&s;printf("%X %X %X\r\n", pstr2->s, pstr2->i, pstr2->c);return 0;}