I have a function in C that takes a parameter uint8_t *that should point to 32-bit aligned memory. Is it possible in C or C ++ or using any specific platform macros to add some decoration to a parameter so that the compiler or linker gives an error during assembly if it is not aligned as necessary?
The idea here is that I want to protect the function from misuse by other users (or me after 6 months). I know how to align the material that I want to convey to him. I would like to guarantee that no one will be able to convey to them the inconsistent things.
Based on this answer , I think the answer to my question is “no”, this cannot be done during the build, but it seems useful, so I decided to check. My job is to put assert((((size_t)ptr) % 4) == 0);in a function, so at least I could capture it at runtime while debugging.
In my experience, the results are undefined if you have inconsistent uint8_t*on uint32_t*many built-in platforms, so I don’t want to rely on the “right” result coming out at the end. Plus it is used in a real-time system, so slowing down may be unacceptable.
Quotes are welcome if available.
source
share