Why does this function cast an int argument to a volatile pointer and immediately dereferences it?

Chinna

I just want to know what below function is doing

static int myfunc(int val)
{
    return *(volatile int *)val;
}
Gui13

If val is a pointer when you pass it to this function, it makes sure that the value pointed by this pointer is read and returned to the caller.

I suspect this might be a trick for embedded devices, where sometimes the operation of reading a value at an address has some effect on the hardware.
For instance, reading from an hardware FIFO will pop the read value from the FIFO.

Marking here the pointer as volatile make the compiler not optimize the read if it detects that the value is not used.

Example:

#define FIFO_ADDRESS 0x800050

static int myfunc(int val)
{
    return *(volatile int *)val; // the address *will* be read
}

static int bad( int val )
{
    return *(int*)val; // might be optimized to nop()  
                       // by the compiler if the value 
                       // is not used by the caller
}

int main(){
   bad( FIFO_ADDRESS );    // could be NOP since return value is not used

   myfunc( FIFO_ADDRESS ); // *WILL* perform a READ operation on the FIFO, 
                           // even though the result is not used, because 
                           // of the volatile keyword

}

请注意,我可能会用一个智能命名的宏来做不同的事情:

#define FORCE_INT_PTR_READ( address ) *(volatile int *)address 

您能给我们一个例子吗?

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Cast function to function pointer

来自分类Dev

Argument conversion: (normal) pointer to void pointer, cast needed?

来自分类Dev

为什么对volatile int的const引用需要static_cast?

来自分类Dev

Why does -Wcast-align not warn about cast from char* to int* on x86?

来自分类Dev

How to use function pointer in multiprocessing argument

来自分类Dev

How to make compiler not show int to void pointer cast warnings

来自分类Dev

Why does strtol require a pointer to a pointer rather than a single pointer?

来自分类Dev

Why is it a pointer to void declarator instead of a pointer to member function?

来自分类Dev

Python argparse , add_argument, type = <some function> 而不是 int?

来自分类Dev

Why does this function break in Powershell?

来自分类Dev

Cast float to int, or int to float?

来自分类Dev

Swift: why 'subscript(var digitIndex: Int) -> Int' is a valid function signature?

来自分类Dev

How to write a class that may accept a function pointer and/or functor just like a smart pointer does for custom deleter?

来自分类Dev

带有volatile int的多线程的案例

来自分类Dev

#定义REG(x)(*(((volatile unsigned int *)(x)))

来自分类Dev

为什么volatile int可转换为int但volatile T无法转换为T?

来自分类Dev

How to cast JSONArray to int array?

来自分类Dev

int to char cast,奇怪的行为

来自分类Dev

static_cast int 引用int?

来自分类Dev

Why does Scala's indexOf (in List etc) return Int instead of Option[Int]?

来自分类Dev

在赋值期间,如何使volatile结构的行为与volatile int完全相同?

来自分类Dev

Why EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when callback pointer to function?

来自分类Dev

Passing Function Pointer

来自分类Dev

Calling function via pointer

来自分类Dev

I have a Clojure seq containing 3 functions. Why does (rest my-seq) give a "cannot be cast as" exception?

来自分类Dev

Why does calling the perspective function leave me with a black screen?

来自分类Dev

Why does this tail recursive function yield <<loop>> error?

来自分类Dev

Why does sympy think that a function of only real variables is complex?

来自分类Dev

Why does printing a variable and function call together in this code give an error?

Related 相关文章

  1. 1

    Cast function to function pointer

  2. 2

    Argument conversion: (normal) pointer to void pointer, cast needed?

  3. 3

    为什么对volatile int的const引用需要static_cast?

  4. 4

    Why does -Wcast-align not warn about cast from char* to int* on x86?

  5. 5

    How to use function pointer in multiprocessing argument

  6. 6

    How to make compiler not show int to void pointer cast warnings

  7. 7

    Why does strtol require a pointer to a pointer rather than a single pointer?

  8. 8

    Why is it a pointer to void declarator instead of a pointer to member function?

  9. 9

    Python argparse , add_argument, type = <some function> 而不是 int?

  10. 10

    Why does this function break in Powershell?

  11. 11

    Cast float to int, or int to float?

  12. 12

    Swift: why 'subscript(var digitIndex: Int) -> Int' is a valid function signature?

  13. 13

    How to write a class that may accept a function pointer and/or functor just like a smart pointer does for custom deleter?

  14. 14

    带有volatile int的多线程的案例

  15. 15

    #定义REG(x)(*(((volatile unsigned int *)(x)))

  16. 16

    为什么volatile int可转换为int但volatile T无法转换为T?

  17. 17

    How to cast JSONArray to int array?

  18. 18

    int to char cast,奇怪的行为

  19. 19

    static_cast int 引用int?

  20. 20

    Why does Scala's indexOf (in List etc) return Int instead of Option[Int]?

  21. 21

    在赋值期间,如何使volatile结构的行为与volatile int完全相同?

  22. 22

    Why EXC_BAD_ACCESS (code=EXC_I386_GPFLT) when callback pointer to function?

  23. 23

    Passing Function Pointer

  24. 24

    Calling function via pointer

  25. 25

    I have a Clojure seq containing 3 functions. Why does (rest my-seq) give a "cannot be cast as" exception?

  26. 26

    Why does calling the perspective function leave me with a black screen?

  27. 27

    Why does this tail recursive function yield <<loop>> error?

  28. 28

    Why does sympy think that a function of only real variables is complex?

  29. 29

    Why does printing a variable and function call together in this code give an error?

热门标签

归档