C or C + + struct memory alignment calculation refinement scheme

Source: Internet
Author: User

The struct occupies memory calculation method:

assuming that the start address of the struct is 0x00000000, the space occupied by the starting address to the current element must be an integral multiple of the next member space ( The part that does not reach the integer multiple is left blank ), when the last member is reached, that is, when calculating the total struct space, the space must be an integer multiple of the largest basic element in all members.

If an element of a struct is an array, simply align to the basic element and not to the neat number group

Therefore, when defining the structure, try to declare the same type of variables together, and according to the size of the space, use from small to large or from large to small arrangement, such as small-footprint members placed in front of the position, this can effectively save memory.

The rules for computing structs to occupy memory space on the web are too complex, and this is a lot simpler, just focus on the current members.

Test Code :

1 //CUITestingCPP.cpp: Defines the entry point of the console application. 2 //3 4#include"stdafx.h"5#include <iostream>6 using namespacestd;7 8 structMyStruct9 {Ten     BOOLmem_1; One     Doublemem_2; A     BOOLMem_3; -     intMem_4; -      Shortmem_5; the     intmem_6; -      Shortmem_7; -     DoubleMem_8; - }; +  - structtest1 + { A     intmem_1; at     BOOLmem_2; -     intMem_3; -     BOOLMem_4; -     floatmem_5; - }; -  in structst2{ -     Charmem_1; to     Charmem_2; +     intMem_3; -      ShortMem_4; the }; *  $ structstx{Panax Notoginseng     Charmem_1; -      Shortmem_2; the     CharMem_3; +     intMem_4; A }; the  + structst1{ -     intmem_1; $     Charmem_2; $      ShortMem_3; - }; -  the structSTU1 - {Wuyi     intmem_1; the     Charmem_2; -     intMem_3; Wu }; -  About structSTU3 $ { -     Charmem_1; -     intmem_2; -     CharMem_3; A }; +  the structStu4 - { $     Charmem_1; the     Charmem_2; the     intMem_3; the }; the  - structsty{ in      Shortmem_1; the     Charmem_2; the     intMem_3; About }; the  the structste{ the     Charmem_1; + }; -  the structstefin{Bayi      Shortmem_1; the     Charmem_2; the      ShortMem_3; - }; -  the int_tmain (intARGC, _tchar*argv[]) the { the mystruct test_struct; thesize_t size =sizeof(test_struct); -  the     intsize_mem[8]; thesize_mem[0] = (int) (&test_struct.mem_1); thesize_mem[1] = (int) (&test_struct.mem_2);94size_mem[2] = (int) (&test_struct.mem_3); thesize_mem[3] = (int) (&test_struct.mem_4); thesize_mem[4] = (int) (&test_struct.mem_5); thesize_mem[5] = (int) (&test_struct.mem_6);98size_mem[6] = (int) (&test_struct.mem_7); Aboutsize_mem[7] = (int) (&test_struct.mem_8);  - 101     intAddup =0;102 103cout <<"Each member occupies a space size"<<Endl;104     intArr_size =sizeof(SIZE_MEM)/sizeof(size_mem[0]); the      for(inti =1; I <arr_size; ++i)106     {107Addup + = Size_mem[i]-size_mem[i-1];108cout << Size_mem[i]-size_mem[i-1];109         if(i = = arr_size-1) the         {111cout <<"the current number of bytes occupied is"; thecout <<Addup;113         } the  thecout <<Endl; the     }117 118cout << size-addup <<Endl;119cout << Endl <<"Total size"<< Endl << Size <<Endl; - 121System"Pause");122 123     return 0;124}

Test Results :

C or C + + struct memory alignment calculation refinement scheme

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.