C ++ struct memory allocation size

Source: Internet
Author: User

C ++ struct memory allocation size <br/> 1 principle: <br/> 1. The order of all fields in the memory is consistent with the declared Order (theoretically, the interpreter can place fields as needed.) <br/> 2. for performance reasons, most compilers actually place each field of struct in an appropriate memory boundary. The details vary with the language compiler and CPU. In the 80x86 system, compilers that comply with the intel application binary interface (ABI application bianry Interface) allocate a single-byte object to any offset in struct, and the word is allocated to the word offset, objects with double or larger characters are allocated at the double-word boundary. <Br/> 3 most compilers ensure that the length of the entire record is an integer multiple of 2, 4, or 8 bytes, they do this by adding bytes at the end of the record to increase the struct length. The compiler fills up the struct size to ensure that the record length is an integer multiple of the maximum scalar object size in the record or the optimal CPU alignment size. <Br/> 4 generally, the compiler provides an option that allows you to disable this function globally. Many compilers also provide keywords such as The Pragma latter packed to enable you to turn off field alignment by struct (the advantage is to save memory and the disadvantage is to increase the time overhead) <br/> two C ++ data types <br/> char 1 byte <br/> short 2 byte <br/> int 4 byte <br/> long 8 byte <br/> string 32 byte (null string, STL constructor temporarily sets its size to 32 byte) <br/> 3. The principle of struct size in vs2008 is as follows: <br/> the order of 0 fields in the memory is the same as that in the declared Order (with 1st entries) <br/> 1 struct is an integer multiple of the maximum pure amount (with 3rd entries) <br/> 2. The size of struct conforms to the ABI rule (2nd entries) <br/> four examples <br/> string t; <br/> long l; <br/> int I; <br/> char C; <br/> the size is 48 = 32 + 8 + 4 + 4 <br/> long l; <br/> string t; <br/> int I; <br/> char C; <br/> the size is 48 = 8 + 32 + 4 + 4 <br/> int I; <br/> long l; <br/> string T; <br/> char C; <br/> the size is 56 = 8 + 8 + 32 + 8 <br/> char C; <br/> short S; <br/> int I; <br/> long l; <br/> string T; <br/> the size is 48 = 2 + 2 + 4 + 8 + 32 <br/> short a; <br/> long B; <br/> char C; <br/> short D; <br/> int e; <br/> short F; <br/> long g; <br/> the actual size is 2 + 8 + 1 + 2 + 4 + 2 + 8. The actual size is 8 + 8 + 2 + 2 + 4 + 8 + 8 = 40; <br/> 5 Reference <br/> Randall Hyde Zhang Fei, the first book of excellence in programming, a deep understanding of computer </P> <p> Lu Xiaolei <br/> <br />

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.