The structure exists in vivo docking


Memory usage of basic data types

type Byte size (64-bit system) Byte size (32 bits)
bool 1 1
char 1 1
short 2 2
int 4 4
long 8 4
long long 8 8
float 4 4
double 8 8

To calculate

// Comment for memory address
struct T1 {
    double a;  / / 0 to 7
    char b;    / / 8-8
    int c;     See rule 1, the starting position of each member must be an integer multiple of the size of the current member, because 9 is not divisible by 4...
    short d;   / / 16-17
} t1; / / size: 24 (see rule 3, the (18 + (8-1)) > > 3 < < 3 = 24)

struct T2 {
    double a;   / / 0 to 7
    int b;      / / 8-11
    char c;     / / 12-12
    short d;    / / 14 to 15
} t2; // size: 16

struct T3 {
    double a; 		 / / 0 to 7
    int b;     		 / / 8-11
    char c;             / / 12-12
    short d;            / / 14 to 15
    struct T1 e;        // (24) 16-39 (see rule 2, the largest member of T1 double 8)
} t3; // size: 40
Copy the code

Note: on 64-bit systems

Memory alignment calculation rules:

1. Data member alignment rules: The data member of a struct (or union), the first data member is placed at offset 0, and the starting position of each data member is from the size of the member or the size of the member’s children (as long as the member has children, such as arrays). Structure, etc.) (for example, if int is 4 bytes, it is stored from the address that is a multiple of 4.

Struct as members: If a structure has some struct members, the struct members are stored from an integer multiple of the size of the largest element in the structure. (struct A contains struct B, char, int, double, etc.)

The total sizeof the structure, the result of sizeof, must be an integer multiple of the largest member within the structure.

Analytic: 1, variable, a: double * * * * 8 bytes, starting from zero position, the storage, variables, 2 b: 0 to 7 char * * * * 1 byte, from 8 position, at this time eight is 1 integer times, storage 3 b, 8 variables c: Int is 4 bytes, starting at 9, but 9 is not a multiple of 4, so we need to find the nearest 12 that divisible 4, then 12-15 stores c 4, d: **short ** is 2 bytes, starting at position 16, where 16 is a multiple of 2, then 16-17 stores d. The memory size required by T1 is 18 bytes, and the maximum number of bytes of the member variable in T1 is 8 bytes. The memory size of 18 bytes is not an integer multiple of the maximum internal member, so it must be padded up, and the final size after the completion is 24 bytes

[end] Simulate the size of the structure — nesting is not considered

struct T1 {
    double a;  
    char b;    
    int c;     
    short d;  
} t1; // size: 24  

int _memberSizeOffset(int value) {
    int offset = 0; // Calculate the displacement value binary power 2^3 >> 3
    while(value ! =0) {
        value = value >> 1;
        offset ++;
    }
    if (offset > 0) {
        offset --;
    }
    return offset;
}
int calculateStructMemorySize(int structMemory[], int length) {
	int structMemorySize = 0; // The size of the structure in the body
        int maxMemberSize = 0; // Maximum length of member variable
        for (int i = 0; i < length; i++) {
            int memberSize = structMemory[i];
            maxMemberSize = MAX(maxMemberSize, memberSize);
            
            if(structMemorySize % memberSize ! =0) {
                // Memory alignment
                int offset = _memberSizeOffset(memberSize); // Calculate the displacement value binary power 2^3 >> 3
                structMemorySize = (structMemorySize + memberSize - 1) >> offset << offset;
            }
            
            structMemorySize += memberSize;
        }
        if(structMemorySize % maxMemberSize ! =0) {
            int offset = _memberSizeOffset(maxMemberSize); // Calculate the displacement value
            structMemorySize = (structMemorySize + maxMemberSize - 1) >> offset << offset;
        }
        return structMemorySize;
}
int main(a) {
	// Do not consider the structure nesting case now
        int structMemory[] = {8.1.4.2}; // Struct member size array
        int length = sizeof(structMemory) / sizeof(int); // Array length
        
        int structMemorySize = calculateStructMemorySize(structMemory, length);
        
        printf("Memory size: %d\n", structMemorySize);

	return 0;
}
Copy the code