How are structural members stored on a small destination machine?

struct Dummy { int x; char y; }; int main() { struct Dummy dum; dum.x = 10; dum.y = 'a'; } 

How will the layout of the structural elements on a small finite machine be?

Will it be something like this?

  0x1000 +0 +1 +2 +3 ___________________ x: | 10 | 0 | 0 | 0 | ------------------- y: | 'a'| 0 | 0 | 0 | ------------------- 0x1000 +4 +5 +6 +7 
+4
source share
3 answers

I think you will find this question helpful. Context usually matters to the word in memory, and not to the whole structure.

+6
source

The structure structure is a detail of the compiler implementation that is affected by standard packaging. Endianness usually affects only the byte order in the value of the structure element, not the layout. Check the fine print in the compiler manual or use the sizeof parameter and the offset macro for experimentation.

The layout you documented in your question is really very common for the 32-bit LE compiler.

+4
source

The elements of the structure will be in the declared order, with the addition being added as necessary so that each field is correctly aligned for its type and with the addition, as necessary, inserted at the end, so that in the array each subsequent structure was correctly aligned and immediately started after the end of the previous structure. It is also possible (but unlikely) that an additional unwanted addition will be inserted between any two elements or at the end.

Each field will be stored according to type in the compiler and architecture, for example. int 10 will be stored as bytes 0a 00 00 00 on a regular machine with small order with 32-bit ints.

+1
source

Source: https://habr.com/ru/post/1345428/


All Articles