Pointers

What is the meaning of (volatile unsigned int*)(0x04) in following code?
As here should be some address(&address).
 
volatile unsigned int* address = (volatile unsigned int*)(0x04)


Thanks
cam
Thanks for reply

But this page is related to volatile, But I am asking about (volatile unsigned int*)(0x04). Here I am not able to get the meaning of (volatile unsigned int*) in (volatile unsigned int*)(0x04).

We are assigning some pointer into pointer or something else?


Its a C-Style cast. Basically, it takes one value (0x04, in this case of type int) and forces the compiler to assume it is a different type, in this case volatile unsigned*, or a volatile pointer to an unsigned integer.

In C++, typically you would use a reinterpret_cast here instead, to specify what you are attempting (in comparison to something like static_cast or dynamic_cast).
But this page is related to volatile, But I am asking about (volatile unsigned int*)(0x04). Here I am not able to get the meaning of (volatile unsigned int*) in (volatile unsigned int*)(0x04).


probably the address was for an unsigned int variable. now that is being using for volatile int*.

volatile int n; or, volatile int* pn;
pointer or variable, you need why "volatile" is used for.


http://stackoverflow.com/questions/72552/why-does-volatile-exist

http://en.cppreference.com/w/cpp/language/cv
The hardware has put something other than RAM at address 0x4. It is probably related to some piece of hardware. The read/write it, as a first cut you might create a pointer variable to it:
int *p = (int *)(0x4);
and then access it through the pointer. But that might not work right. The compiler normally assumes that the contents of memory is only changed by the program, so if you do something like:
1
2
3
int val = *p;
// do some other things;
int val2 = *p;

Then it might optimize this code into something like:
1
2
3
int val = *p;
// do some other things;
int val2 = val;

To prevent this, you need to tell the compiler that the value at address 0x4 is "volatile" - it can change spontaneously without any action by the program. Doing this ensures that whenever your C++ reads/writes that address, the actual machine code will read/write the address.


Topic archived. No new replies allowed.