I'm writing a C library for STM32 and ran into a problem.
I have typedef struct:
typedef struct foo {
// some struct elements
} foo;
Volatile variable with foo type:
volatile foo bar;
And function with foo-typed argument:
int foobar(foo* baz) {
// some operations with baz
return 0;
}
When I'm trying to call foobar(&bar);, I get an error: error: invalid conversion from ‘volatile foo*’ to ‘foo*’ [-fpermissive]
Will it work if I cast volatile foo* to foo* ( foobar((foo*)&bar);)?
I tried to cast volatile foo* to foo* but I don't know if it will work without bugs.
It is not only cast. If you need to cast this way it means that your code has a issue.
If you want to use a function which does not take a
volatileparameter with avolatileobject it means that this object needs to be treated as not *side effects prone. Your function does not have to assume that something not visible to the compiler can change it and apply optimizations which are not possible forvolatileobjects. The most important is that all accesses to thevolatileobject have to be applied to its permanent storage place.If you have this problem then:
You overuse the
volatilekeyword and yourvolatileobject does not have to bevolatile(as you want to use it as nonvolatile). You should rethink your program data types.You can declare the function as taking
volatile- but it will prevent many possible optimizations.You can have different functions handling
volatileand nonvolatiledata.No. It will only silence the warnings but it can be very dangerous.
and resulting code:
in non volatile version skips the second check. If something (signal, interrupt) changes the
dangerousmember it will call the function - killing someone for example