-2

How can I see the size of a struct element at compile time?

I tried:

#define XSTR(x) STR(x)
#define STR(x) #x
#pragma message "Size of struct: " XSTR(sizeof(my_struct.element))

But it just shows:

note: #pragma message: Size of struct: sizeof(my_struct.element)

I don't want to ASSERT that it's the right size. I want to know what the size is.

EDIT: I did not ask "how do I use sizeof?". (which is what the other post asked), so I don't see it as a duplicate. I asked "How can See the size of a struct element at compile time? which is a different question.

I am trying to troubleshoot why the compiler is assigning 32 bits to a 16 bit struct.

Davide Andrea
  • 1,357
  • 2
  • 15
  • 39
  • 3
    ... So you just recreate a question because you have too many downvote? What you want is not possible and has no sense. – Stargateur Jan 14 '17 at 20:19
  • 1
    You can't because the preprocessor doesn't know about keywords such as `sizeof`, much less the size of your structure. – Jonathan Leffler Jan 14 '17 at 20:21
  • Re-posting a bad question will not get you much further. It is just a good way to get a question-ban. I gave a good recommendation at the original, which I will not post again. – too honest for this site Jan 14 '17 at 20:23
  • @HamidRezaMehrabian: You should flag the question as dup then. – too honest for this site Jan 14 '17 at 20:24
  • 4
    The actual question is: why do you assume a `struct` to have 16 bits? WHat does your ABI require? Why do you rely on it? Why do you think you **can** rely on it? – too honest for this site Jan 14 '17 at 20:30
  • Yes, that is my ultimate question, but a question that I will refrain from asking here until I have tried to solve it myself. I do not wish to bother the fine folks here about that. Right now I am looking for the tools to help me troubleshoot it on my own. – Davide Andrea Jan 14 '17 at 20:35
  • 2
    Why don't you just `printf("my super important size is %zu", sizeof my_struct.element);` in the begin of the main? In your question your are not respectful. – Stargateur Jan 14 '17 at 20:57
  • 1
    Maybe it has to do with memory alignment. Can you post your struct? – Stephan Lechner Jan 14 '17 at 21:10
  • @Stargateur printf is not available: this is an embedded processor, there is no I/O, no console – Davide Andrea Jan 14 '17 at 21:11
  • 2
    @DavideAndrea Well this is clearly a XY problem... You don't want to give the structure definition we loose our time. See this http://stackoverflow.com/questions/20979565/how-can-i-print-the-result-of-sizeof-at-compile-time-in-c or look the asm generate by your compiler or give us what is the **real** problem. – Stargateur Jan 14 '17 at 21:24
  • @Stargateur The real problem (that I needed help with: "X") is that I lacked the tools to solve some other problem ("Y"). No one helped me find those tools. So I came up with my own solution. I don't need help with the other problem ("Y"): I solved it by using the tools ("X") that I myself came up with. If you consider that an XY problem, so be it. But Y is not a problem: I solved it. Thanks. – Davide Andrea Jan 14 '17 at 21:31
  • 2
    it is not a duplicate: this question asks about compile-time (#pragma message is executaed not by the preprocessor, but during the compilation), which is much more reasonable thing to ask for and expect it to be working than the other question (sizeof during the preprocessor stage). It shouldn't be closes as a duplicate of https://stackoverflow.com/questions/4079243/how-can-i-use-sizeof-in-a-preprocessor-macro – imz -- Ivan Zakharyaschev Sep 01 '17 at 16:18
  • Thank you for seeing that clearly and for expressing your agreement. It makes me feel better about the downvotes others gave me. – Davide Andrea Sep 02 '17 at 20:12
  • 2
    Among other things, I've got a response saying that pragmas are executed by preprocessor. I can't agree. pragmas clearly directly affect the compiler behavior (after the preprocessor has done its job); e.g., think about any of https://gcc.gnu.org/onlinedocs/gcc-4.9.0/gcc/Pragmas.html – imz -- Ivan Zakharyaschev Sep 03 '17 at 21:41
  • 1
    Siome answers [for clang](https://stackoverflow.com/a/35261673/94687) or [for gcc](https://stackoverflow.com/a/21005143/94687); their idea is (ab)using warnings about array sizes. – imz -- Ivan Zakharyaschev Sep 03 '17 at 21:45

1 Answers1

2

I regret to say that the following ugly, brute-force methods is all I could come up with. But they work.

Method 1

Compilation fails at the line that corresponds to the size. By checking which line fails, I know the size of the struct element

#define STATIC_ASSERT_SIZE(an_item, a_siz) do { enum { dummy_var = 1/((an_item == a_siz)? 0 : 1 )}; } while (0)

STATIC_ASSERT_SIZE(sizeof(my_struct.element), 0);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 1);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 2);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 3);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 4);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 5);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 6);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 7);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 8);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 9);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 10);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 11);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 12);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 13);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 14);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 15);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 16);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 17);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 18);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 19);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 20);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 21);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 22);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 23);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 24);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 25);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 26);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 27);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 28);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 29);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 31);
STATIC_ASSERT_SIZE(sizeof(my_struct.element), 32);

Method 2

Assuming it compiles, look at the variable in the MAP file generated by the compiler, which tells you how many bytes it uses.

Davide Andrea
  • 1,357
  • 2
  • 15
  • 39