C + + interview-memory alignment is a different data type storage space

Source: Internet
Author: User

Below is a list of the dev-c++ of the base type and the range of values:

Basic type number of placeholder value range Input Example output character example

----Char 8-2^7 ~ 2^7-1%c %c,%d,%u

Signed--Char 8-2^7 ~ 2^7-1%c %c,%d,%u

Unsigned--Char 8 0 ~ 2^8-1%c %c,%d,%u

[Signed] short [int] 16-2^15 ~ 2^15-1%HD %hd

unsigned short [int] 0 ~ 2^16-1%hu%hu ,%ho,%HX

[Signed]--int 32-2^31 ~ 2^31-1%d

unsigned--[int] 0 ~ 2^32-1%u,%o,%x

[Signed] long [int] 32-2^31 ~ 2^31-1%ld

unsigned long [int] 0 ~ 2^32-1%lu,%lo,%LX

[Signed] long long [int] 64-2^63 ~ 2^63-1%i64d

unsigned long long [int] 0 ~ 2^64-1%i64u,%i64o,%i64x

----float +/-3.40282e+038%f,%e,%g

----double +/-1.79769e+308%lf,%le,%lg%f,%e,%g

--long double + +/-1.79769e+308%lf,%le,%LG

Note: int data is not the same as the number of digits in a system of different digits some compilers have different effects

16 2*8 Bit

32 4*8 Bit

64 8*8 Bit

1 byte=8 BIT

Memory alignment can be summed up in a sentence:

"Data items can only be stored in memory locations where the address is an integer multiple of the data item size"

For example, the int type occupies 4 bytes, and the address can only be located in 0,4,8.

The double type occupies 8 bytes, and the address can only be placed above the 0,8,16.

If there is a special provision in the code, the alignment is program-based

#pragma pack (n)

If the above statement appears in the code

Then the alignment mechanism is the address only in the 0,n,2*n,3*n position

1#include <iostream>2#include <cstdlib>3#include <pthread.h>4#include <windows.h>5 using namespacestd;6 structX17 {8     intI//4 bytes9     CharC1;//1 bytesTen     CharC2;//1 bytes One     LongA//4 bytes A     floatb; - } x1; - structX2 the { -     CharC1;//1 bytes -     intI//4 bytes -     CharC2;//1 bytes +     LongA//4 bytes -     floatb; + } x2; A structX3 at { -     CharC1;//1 bytes -     CharC2;//1 bytes -     intI//4 bytes -     LongA//4 bytes -     floatb; in } x3; - structX4 to { +     intI//4 bytes -     CharC1;//1 bytes the     CharC2;//1 bytes *     LongA//4 bytes $     floatb;Panax Notoginseng } x4; - intMain () the { +cout<<"Long"<<sizeof(Long) <<"\ n"; Acout<<"float"<<sizeof(float) <<"\ n"; thecout<<"int"<<sizeof(int) <<"\ n"; +cout<<"Char"<<sizeof(Char) <<"\ n"; -cout<<"size of the X1"<<sizeof(x1) <<"\ n"; $cout<<"size of the X2"<<sizeof(x2) <<"\ n"; $cout<<"size of the X3"<<sizeof(x3) <<"\ n"; -cout<<"size of the X4"<<sizeof(x4) <<"\ n"; -     return 0; the } - Operation Result:Wuyi Long 4 the float 4 - int 4 Wu Char 1 -Size of the X1 - AboutSize of the X2 - $Size of the X3 - -Size of the X4 -
View Code

The data in the three struct structures is the same, but the structure takes up a different space. This is how the alignment mechanism affects storage

C + + interview-memory alignment is a different data type storage space

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.