0

I've noticed this error raises every time I'm accessing member of packed struct

  1. How can I disable it specifically for functions that handle the misalignment? (safe_unaligned_val_16/32/64 - constructs the output by manually taking byte after byte like memcpy)
  2. Does GCC make some optimization if I pass struct pointers to a function? I've noticed some optimizations like when you have a struct with 4 bytes of primitive types, it just passes that value to a single 32 bit register when the function doesn't change the value of the primitives in. Elaborate if you know anything like this ^

Thanks, Greg

Greg Doe
  • 1
  • 1
  • 2
  • 1
    Can you provide an example structure definition with example usage and compiler version and options needed to reproduce the issue/warning/message? `this error raises` - please post the exact error message including steps necessary to reproduce the error. `byte after byte like memcpy` - what if the member is bit-unaligned? 2) please limit to one question per question. It would be preferable if you would ask a separate question including example code and necessary steps that lead to you "noticing" some optimization including the target architecture you were compiling for. – KamilCuk Nov 12 '19 at 12:19
  • 1
    It is a great opportunity to get to know [how can you ask a good question on stackoverflow](https://stackoverflow.com/help/how-to-ask), check the [question checklist](https://meta.stackoverflow.com/questions/260648/stack-overflow-question-checklist) which helps me a lot each time and on a side note [why is saying “Thank you!” in question undesirable](https://meta.stackexchange.com/questions/115694/why-is-saying-thank-you-in-question-undesirable). – KamilCuk Nov 12 '19 at 12:27
  • @KamilCuk What do you mean by "bit-unaligned"? – Ian Abbott Nov 12 '19 at 12:28
  • @IanAbbott Uch, right, sorry, thought too fast. I was thinking what if a member doesn't start at full byte, ex. starts at bit 5. But you can't take the address of a bit-field member, so it makes no sense (or it depends how OP implements the "access to a member of a packet struct"). – KamilCuk Nov 12 '19 at 12:31
  • 1
    @KamilCuk Ah! So you meant "byte-unaligned" or "not byte-aligned". It makes sense now, even though it's wrong, as you later realized. :) – Ian Abbott Nov 12 '19 at 12:34

1 Answers1

0
  1. How can I disable it specifically for functions that handle the misalignment? (safe_unaligned_val_16/32/64 - constructs the output by manually taking byte after byte like memcpy)

The C language specifies neither the diagnostic nor any way to work around it. It does not even have a sense of structure packing, leaving most details of the layout of structures to implementations' discretion.

Since you seem to be interested specifically in GCC, you would probably find that implementation's pragmas for influencing diagnostics to be of use for the purpose. In particular, the you would probably find it convenient to do

#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wwhatever-the-relevant-option-is"

// function or functions ...

#pragma GCC diagnostic pop
  1. Does GCC make some optimization if I pass struct pointers to a function? I've noticed some optimizations like when you have a struct with 4 bytes of primitive types, it just passes that value to a single 32 bit register when the function doesn't change the value of the primitives in. Elaborate if you know anything like this ^

I am not aware of that specific optimization, but implementations have great freedom to optimize. It is up to the compiler to ensure that the semantics of the resulting program are equivalent to those C describes for the source code presented to it. Such an optimization might indeed be valid and useful for a function that satisfies a rather stringent set of requirements, but not modifying the pointed-to value is not sufficient for such an optimization to be safe.

John Bollinger
  • 160,171
  • 8
  • 81
  • 157