We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
Hi,
I would like to prevent structure members optimization. I used volatile keyword:
typedef volatile struct { volatile ... volatile uint32_t discard; volatile ... } regs_t;
I declared a member in another non-volatile struct:
typedef struct { ... volatile regs_t *regs; ... } peripheral_t;
I used an "empty" expression to just clear flags in register (the memory read access must be performed):
peripheral_t p; p.regs = 0x12345678; /* init to register block starting address */ p.regs->discard; /* gets optimized out when opt. is on */
The "empty" expression gets optimized out although I thought (hoped) I prevented it via the volatile keywords. - What is the right place to put the volatile? - Does the whole struct have to be volatile? - Does it make sense to define volatile type? (typedef volatile struct...)
Thanks for tips/explanations.
Regards Pavel
Andy,
maybe it's not strict regarding ANSIC, but in most cases in embedded world and C language, volatile and register accesses through pointers are the prevailing methods for accessing peripherals, aren't they?
I'm not expecting this behaviour in GNU/microsoft's C compilers on my PC when compiling SW, but expecting it for embedded compilers when making FW.
----------------------------------- A volatile object is one that may be modified outside of program control. Memory-mapped I/O ports are a typical example. Declaring an object as volatile indicates that the compiler should always generate code to fetch the object's value from its actual memory location — it may have changed since the last access by the program. (This disallows optimizations which could load the value into a register and possibly return erroneous results.) ----------------------------------- www.ericgiguere.com/.../ansi-c-summary.html (google first hit)