This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

structures

so lots of threads on this forum say you must not care about how the compiler puts things into structures with alignment and padding and stuff

then we look at example code with structures used to access registers like in cmsis

who is right and what is safe?

Parents
  • They do know that their structs contains multiple registers of same size after each other. They will be stored in the same order. And since they are same size, they have same align requirement so no strange padding added.

    You can also use a struct and map to hardware. But you need to understand specifically the requirements for your used architecture and what design goals the compiler vendor had when implementing the code generator.

    And mapping a struct directly to hardware is non-portable. But the hardware itself is non-portable. So there isn't a big problem with a number of lines of non-portable code mapped to a non-portable hardware platform.

    People should be very careful about using a struct and map to a defined standard like data structures inside a file format. Or when trying to send a struct to another hardware using different compiler and/or processor.

    So the main thing is that if you know what you are doing, you can do a lot. And you would then normally also have the knowledge to properly document your assumptions and often add compile-time checking to try to catch some of these assumptions.

    The normal case is that people who do have to ask on a web forum should normally avoid doing clever things. Because they often do not have enough knowledge to identify all the different ways the code can bite. There must be a big advantage selecting a specific method of doing something before it is meaningful to invest in a solution that can bite.

    The compiler vendors will help out trying to make sure that the Cortex libraries will work well - because their users will blame them when the libraries fails.

Reply
  • They do know that their structs contains multiple registers of same size after each other. They will be stored in the same order. And since they are same size, they have same align requirement so no strange padding added.

    You can also use a struct and map to hardware. But you need to understand specifically the requirements for your used architecture and what design goals the compiler vendor had when implementing the code generator.

    And mapping a struct directly to hardware is non-portable. But the hardware itself is non-portable. So there isn't a big problem with a number of lines of non-portable code mapped to a non-portable hardware platform.

    People should be very careful about using a struct and map to a defined standard like data structures inside a file format. Or when trying to send a struct to another hardware using different compiler and/or processor.

    So the main thing is that if you know what you are doing, you can do a lot. And you would then normally also have the knowledge to properly document your assumptions and often add compile-time checking to try to catch some of these assumptions.

    The normal case is that people who do have to ask on a web forum should normally avoid doing clever things. Because they often do not have enough knowledge to identify all the different ways the code can bite. There must be a big advantage selecting a specific method of doing something before it is meaningful to invest in a solution that can bite.

    The compiler vendors will help out trying to make sure that the Cortex libraries will work well - because their users will blame them when the libraries fails.

Children
No data