Possible duplicate:Why isn & rsquo; t sizeof for a structure equal to the sum of the sizeof of each member?
Why is sizeof();this structure 16 bytes? I am compiling in g ++.
sizeof();
struct bitmapfileheader { unsigned short bfType; unsigned int bfSize; unsigned short bfReserved1; unsigned short bfReserved2; unsigned int bfOffBits; };
This is because 4 byte ints are aligned on a 4 byte boundary, so after bfType there are 2 padding bytes.
Alignment. On your platform, probably, ints should be aligned by 4 bytes, and shorts aligned by 2 bytes.
+0 -1 : bfType +2 -3 : <padding> +4 -7: bfSize +8 -9: bfReserve1 +10 -11: bfReserve2 +12 -15: bfOffBits ------------- 16 bytes
Alignment is good because custom structures require additional work for many architectures.
. , .
, UNALIGNED.
UNALIGNED
, 4- .
- , . , , ( , ). n- , n - . . , . ( ) - , - , , . , int , , int .
int
bfType, , 2- , bfSize, , 4- . 4 2 bfType bfSize.
bfType
bfSize
, , , , ABI ( , , ). 5 , 16 , .
, . , , , , . .
U ,
ISO ++ 03, 9.2 [class.mem]/12:
() , , , . , , (11.1). , ; (10.3) (10.1).
- , ,
This is due to alignment - the compiler needs to do some debugging.
Source: https://habr.com/ru/post/1725207/More articles:What is the best way for you to offload the database insert so that the web response comes back faster? - sql-serverProblem with Raytraced Shadows - raytracingbasic training scripting - design-patternsAlternative HttpContext.Current.Cache - c #The best way to access a value from the last iteration in a loop - pythonHow to put a BY order in SQL UNION so that TOTALS are always displayed as the last line? - sqlКакую версию ASP.NET MVC 2 я устанавливаю на своем веб-сервере, если я разработал решение с использованием ASP.NET MVC 2 в Visual Studio 2010 Beta 2? - asp.net-mvcWhen does the MVC pattern fit? - model-view-controllerКакой стек технологий вы бы рекомендовали для приложения интенсивной интрасети ввода данных с использованием инструментов Microsoft (asp.net mvc OR silverlight)? - jqueryАнимировать путь? - animationAll Articles