Why the size of a structure is bigger than it should be? | Sololearn: Learn to code for FREE!
Neuer Kurs! Jeder Programmierer sollte generative KI lernen!
Kostenlose Lektion ausprobieren
+ 3

Why the size of a structure is bigger than it should be?

https://code.sololearn.com/cc4Hoh73DPz2/?ref=app Why size of a structure test is 16, not 12 as it should be? It seems like compilator is filling space to an iteration of 8, but why? It is not doing that for a class/structure with a single element.

2nd Feb 2018, 10:16 PM
Arkadiusz Babiarz
Arkadiusz Babiarz - avatar
1 Antwort
+ 2
To solve this, just add the following line : #pragma pack(1) This will force the compiler to use a byte padding of one, which will ensure that extra memory is not used. By default, in memory, some blocks of a fixed number of size are used to store data. The size of these blocks is determined usually by the last member or the largest sized member of the class. As a result, the blocks were 8 bytes each and 4 bytes were wasted. So if you force a block size of one, the problem of extra bytes no longer occurs.
3rd Feb 2018, 2:39 AM
Kinshuk Vasisht
Kinshuk Vasisht - avatar