8

Lets assume I have a structure:

struct A {
  uint16_t a;
  uint64_t b;
};

is there a way to get the size of A w/o padding ? i.e.: The sum of sizeof of all the members (even if it is not recursive).

Normally sizeof(A) == 16. I would like __GCC_sizeof__(A) == 10.

I want it in a test code w/o affecting the actual code, which means no "#pragma"s and no "__attribute__" in the structure definition. (Though it can be done with #ifdef TEST, but it is very ugly).

It doesn't have to be portable, GCC is enough.

Thanks!

Grijesh Chauhan
  • 57,103
  • 20
  • 141
  • 208
Vadim S.
  • 187
  • 1
  • 4
  • I doubt it can be done without pragmas. – Luchian Grigore Jan 17 '13 at 14:27
  • 3
    To clarify, are you trying to remove padding from your struct, or just get the size it _would be_ without padding? – Matt Kline Jan 17 '13 at 14:30
  • slavik, the second part. Just the clean size (leave the padding). – Vadim S. Jan 17 '13 at 14:47
  • Doesn't make sense TBH. E.g. what value would you expect for `__GCC_sizeof__(&A::a)` ? There might be some padding in a pointer-to-member, but you can't act on that. – MSalters Jan 17 '13 at 15:46
  • Hmm, isn't the size of a pointer fixed for a specific architecture? I would probably expect it to be 8 bytes on the system I use. – Vadim S. Jan 17 '13 at 16:25
  • @VadimS. pointer-to-member is a completely different thing [Size of pointer to member function varies like crazy](https://stackoverflow.com/q/29607359/995714), [Why the size of a pointer to a function is different from the size of a pointer to a member function?](https://stackoverflow.com/q/12006854/995714) – phuclv Feb 18 '21 at 13:13

4 Answers4

4

I think sizeof(A::a) + sizeof(A::b) should do the trick for you. There's no way to get an unpadded size of a struct because what purpose could such a size serve a program?

Mark B
  • 95,107
  • 10
  • 109
  • 188
  • 1
    More generically, does GCC or any other compiler allow you to reflect upon a struct's members individually? – Aniket Inge Jan 17 '13 at 14:37
  • 1
    The purpose is the ability to track newly added structure members from inside the test. This means if someone would add a new member to the structure my test will make sure that the member is addressed in the test, for example is the new member sent over some network correctly? If I take the regular sizeof then this member can fill a padding and not affect the result of sizeof. – Vadim S. Jan 17 '13 at 14:51
  • Why not just require an `operator==` which does whatever is correct for that struct, rather than trying to guess what semantics are correct in your test? – Useless Jan 17 '13 at 15:34
  • 1
    @MarkB: if you need to read the exact number of bytes from a file using the sizeof of structure THEN you need the exact size without padding. – user18490 Feb 04 '17 at 17:17
3

The purpose is the ability to track newly added structure members from inside the test.

It would have been better had you ask this first...

And the answer is yes, there are ways, but not by #include the file; you should use something that is able to get the AST/ABT structure and lists the fields then compare it against a pre-registered list. This is something possible with Clang, for example.


But now, let's go one step further. Why would you want to test that ? It's brittle!

It would be better to test functionality, what's hidden is hidden for a reason.

If each functionality is tested correctly, then it matters not whether you know the list of fields as long as the tests pass.

Matthieu M.
  • 287,565
  • 48
  • 449
  • 722
  • In general I would agree that it is better to test interfaces. In this case, the test is meant for a module that does a kind of an IPC and when you add a structure to it you have to state every member field explicitly (yes it is a really lame framework) so I would want to make sure that if a structure grows then the module is updated. My conclusion from this discussion is that there is no such kind of a magic sizeof. I will try a different approach. Thanks – Vadim S. Jan 18 '13 at 14:17
2

I would look at this Stackoverflow response:

Is gcc's __attribute__((packed)) / #pragma pack unsafe?

and at this web site for the GCC information:

http://gcc.gnu.org/onlinedocs/gcc/Structure_002dPacking-Pragmas.html

If you set the pragma before the structure to 1, it should align on byte boundaries and be a compact size so you can use sizeof to get the number of bytes.

If it is just with a small amount of structure, you can enable the pragma before the declaration of the structure and then disable it afterward.

Hopefully the about is helpful in your efforts.

Community
  • 1
  • 1
Glenn
  • 1,169
  • 1
  • 14
  • 23
  • 1
    He specifically said the didn't want to use pragmas or attributes. – netcoder Jan 17 '13 at 14:35
  • Again, I don't want to affect the 'production' code, just the test. As I said, maybe putting the pragma or attribute in #idefs is the way, but it is a very ugly way. – Vadim S. Jan 17 '13 at 14:53
0

In C language, you can use a preprocessor directive #pragma to check sizeof structure without padding..

#pragma pack(1)  

will give you the sizeof structure without padding..

Raghu Srikanth Reddy
  • 2,703
  • 33
  • 29
  • 42