Have a global variable that I do not want anyone, but an ISR function, to change, the variable is g_epoch, so see the code below:
#include <stdint.h>
volatile const uint32_t g_epoch = 0;
void adc_ISR() {
static uint32_t epoch = 0;
++epoch;
*(const_cast<uint32_t*>(&g_epoch)) = epoch; // Expecting g_epoch to change
printf("g_epoch adr: %x, const_casted adr: %x, epoch: %x, g_epoch: %x\n",
(uint32_t)(&g_epoch),
(const_cast<uint32_t*>(&g_epoch))),
epoch,
g_epoch);
}
The output is: g_epoch adr: ACF8, const_casted adr: ACF8, epoch: 28, g_epoch: 0, how come the g_epoch remains zero? Above is the sanitized function, but the code does compile with no warnings.
EDIT: Based on the comments, the variable is placed into the ROM area, and therefore cannot be changed, thank you zneak. But it looks like I can make it work by using pragma and forcing the variable into RAM, but I wont, due to answer by Alessandro Pezzato, since the behavior is undefined.