The behavior of tiny C ++ structures

If I define a structure like:

struct tiny { long t; }; 

will be processed as long in terms of function arguments and similarly, in the example there will be a parameter:

 void myfunc(tiny x) { ... } 

handled as a long parameter, actually pushing it onto the stack?

So, essentially, is a tiny structure just like its only member?

thanks

+6
c ++ struct
source share
3 answers

The memory consumption in the structure is at least the sum of the memory sizes of the constituent variables.

However, the compiler can add padding between variables or at the end of the structure to ensure proper data alignment for a given computer architecture.

+7
source share

There is no guarantee, but at least with the compilers I looked at, this is normal, yes.

+7
source share

If you start adding virtual functions, it will increase by 4 bytes on most systems / compilers (and an additional 4 bytes for each interface you use). Generally speaking, the structure should be the same size as the contents, unless the compiler has added an extra complement, as Aviv said.

Look at the #pragma pack (n) for packaging problems, at least in Visual Studio ( Microsoft page in pragma )

-one
source share

All Articles