C / C ++ memory alignment

 

1. Align the memory pragma pack grammar

Syntax: #pragma pack ([show] | [push | pop] [, identifier], n)
action: the assigned structure, and the class's package joint alignment (pack alignment), you may inform the compiler change instruction transmitted precompiled a method for the alignment of the specified data

For example as follows:

 

#include <the iostream> 
the using namespace STD; 

int main () { 

#pragma Pack (. 8) // specified alignment size. 8 
    struct test_A 
    { 
    	// Align actual size min (sizeof (int), 8 ) = min (4, 8) = 4, the compiler will ensure TEST_A i.e. the first address of a first address is 4 byte alignment, a case aligned 
        int a; 
        // size of the actual alignment min (sizeof (char), 8 ) = min ( 1,8) = 1, since b 1 first address byte alignment requirements, which are clearly suitable for any addresses, a, b are aligned 
        char b; 
        // this is an array, the array size consistent alignment therewith unit thus align = left (c) align = left = (Double) = min (the sizeof (Double),. 8) = min (8, 8) =. 8, 
        // c. 8 byte alignment requirements since the first address, thus foregoing a + b = 5, but also to fill in behind the 3-byte c to align 
        // (Note: here not only to ensure that the next TEST_A a, b of the variable alignment, make sure c also aligned, so this is not filled with 3 bytes, 7 but padding bytes) 
        Double C [10]; 
        char D; // size of the actual alignment min (sizeof (char), 8 ) = min (1,8) = 1, any addresses are aligned 
    }; 
#pragma Pack ()

	cout << sizeof (TEST_A) << endl ; // size of the overall structure of (. 4) + (. 1 +. 3) + (10 *. 8) + (. 1 +. 7) = 96 
	return 0; 
}

As another example, as follows:

Pack #pragma (. 4) 
	struct Test_B 
	{ 
		char A; 
		Short B; 
		int C; 
		a float D; 
		int * E; 
		char * F; 
		Double G; 
	}; 
#pragma Pack () 
	COUT << the sizeof (Test_B) << endl; // size of the entire structure is (1 + 1) + (2) + (4) + (4) + (8) + (8) + (8) = 36

When 36 is 4-byte aligned, when the 40 need 8-byte aligned 

 

2, the basic data memory size occupied by the type of

Examples of this chapter are run in the 64bit ubuntu system. 

3, static variables static

Static variable storage location and configuration of the storage body instance independent address is stored in a separate static data area, there is no space occupied by the static members come calculated by the size calculating siezof.

 

4, class

Empty class will take up memory space, and the size is 1 because C ++ is required for each instance in memory has a unique address.

(A) internal class member variables:

  • Ordinary variables take up memory, but be careful alignment principle (and this is very similar to the type struct).
  • static modification of static variables do not take content, because the compiler will put the global variable area.

(B) member function inside the class:

  • Normal function does not take up memory.
  • Virtual function to 8 bytes, is used to specify the address of the virtual function table entry virtual function. Therefore, the number of a class virtual function is occupied by the same address, and the virtual function is not related to

 For example, the following:

#pragma pack(4)
	class ClassA { };
#pragma pack()
	cout << sizeof(ClassA) << endl; // 1

As another example, as follows:  

Pack #pragma (. 4) 
	class the ClassB { 
	Private: 
		char C; 
		Short SH; 
		int A; 
	public: 
		void FOUT () { 
			COUT << "Hello" << endl; 
		} 
	}; 

	COUT << the sizeof (the ClassB) << endl ; // output 8, ordinary function does not take up memory space 

 When FOUT () becomes a function key plus virutal virtual functions, the output is 16, then add a new virtual function, the allocated memory space is not increased.

 

5, sub-categories

Subclass share memory size is the parent class member variable + value itself. Of particular note is the sub-class share the same parent class virtual function pointer, so when a new subclass declare a virtual function, do not have to save the virtual function table pointer entry.

 

 

 

 

 

 

 

 

 

 

 

 

Guess you like

Origin www.cnblogs.com/mazhimazhi/p/11333820.html