2

What effect does volatile have on pointers? I got some unexpected behavior while using it

int main() {

volatile uint32_t *ptr=((uint32_t *)(0x00000000+0x00100100));
cout<<ptr<<endl;

uint32_t *ptr1=((uint32_t *)(0x00000000+0x00100100));
cout<<ptr1<<endl;

return 0;
}

"ptr" returns value 1, while "ptr1" return value 0x100100.

What causes this difference?

Yash Gupta
  • 187
  • 2
  • 11
  • 1
    What are they pointing to? –  Feb 25 '19 at 12:20
  • It does have an effect on the display of the variable. Doesn't matter what they point to, as we just display the address here, not the pointee. – Matthieu Brucher Feb 25 '19 at 12:23
  • 3
    My guess: Different overloads of `operator<<` are selected. The first one is probably the one for booleans. – melpomene Feb 25 '19 at 12:24
  • @NeilButterworth I was just checking the working of the keyword Volatile,gave random addresses,even If I change the address the "volatile uint32_t *ptr=((uint32_t *)(0x00000000+0x00100100)" still gives output as 1 – Yash Gupta Feb 25 '19 at 12:25

0 Answers0