类型转换还是函数指针? [英] Typecasting or function Pointer?

查看:78
本文介绍了类型转换还是函数指针?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#define writel(value, address) \
    (*(volatile unsigned int *)(address)) = (value)


我想知道上面的定义宏是类型转换还是用作函数指针.

由于使用了volatile,感到困惑


I wish to know whether a above define macro is typecasted or used as function pointer .

Got confused since volatile is used

推荐答案

这是一个赋值.
volatile关键字(可能)用于强制编译器实际执行这种分配(不要通过优化跳过它).
It is an assignment.
The volatile keyword is (probably) used in order to force the compiler to actually perform such assignment (don''t skip it by optimization).


CPallini当然是正确的,您感到困惑的原因可能是表达式(*(volatile unsigned int *)(address))
中涉及到指针的某种程度的修改
在此表达式中:
-unsigned int是用于(C风格)类型转换的基本类型
-volatile是类型修饰符(例如const),告诉编译器在此处访问的unsigned int值可能随时更改
-(volatile unsigned int*)是一种类型转换,用于指向类型为unsigned intvolatile值的指针"
-(address)是类型转换的参数;如果将表达式用作地址而不是变量或文字,则括号可能是必需的

由于周围的''()'',必填项''*''和函数参数列表,函数指针type definition看起来有点相似.我试图弄清楚如果要分配一个函数指针,那应该是什么样子,也就是说,如果类型转换是为了将address的类型更改为一个函数指针,而该函数指针又被分配了一个新的值:

While CPallini is of course correct, the reason for your confusion may have been the somewhat involved pointer modification in the expression (*(volatile unsigned int *)(address))

In this expression:
- unsigned int is the base type used for a (C-style) type-cast
- volatile is a type modifier (such as const), telling the compiler that the unsigned int value accessed here may change at any time
- (volatile unsigned int*) is a type-cast to ''pointer to a volatile value of type unsigned int''
- (address) is the argument to the type-cast; the brackets may be necessary, if an expression is used as an address rather than a variable or literal

A function pointer type definition looks a bit similar due to the surrounding ''()'', the obligatory ''*'', and the function argument list. I''ve tried to figure out how this should look like if it were to be the assignment of a function pointer, i.e. if the type cast were meant to change the type of address to a function pointer, which gets assigned a new value:

(unsigned int (cdecl)())(address) = value


[edit]经过编译器检查,并修复了类型转换语法;但是编译器仍然不接受它,因为它不允许我将值分配给函数指针[/edit]

请注意在(address)之后的另一个空集().它散发出一个事实,我们正在处理的是函数指针,而不是普通的指针.另外,我省略了volatile,因为这对于返回类型是无效的限定符(换句话说,表达式中的volatile表示这不是函数指针)


[edit]Checked with the compiler and fixed the type cast syntax; but the compiler doesn''t accept it anyway since it won''t let me assign a value to a function pointer [/edit]

Note the additional empty set of () after (address). It gives away the fact we''re dealing with a function pointer rather than an ordinary one. Also, I left out volatile, because that''s an invalid qualifier for return types (or, in other words, the volatile in your expression is an indication this is not a function pointer)


你好

volatile 表示将直接使用变量执行操作,而无需使用累加器寄存器,该操作通常在访问多线程变量的情况下使用.
为此,可以更好地使用互锁功能.
实际上在您的情况下:(辅助)可以使用InterlockedExchangeInterlockedExchangePointer 功能,它们比简单的volatile分配更加安全.如果您不在该宏中执行跨线程变量使用-那么简单的Assignmet就可以了.

问候,
Maxim.
Hello

volatile means that operation will be performed with variable directly without using accumulator register, that mostly used in cases of multithreaded variables accessing.
For that porpose better usage of Interlocked functions.
Actually in your case: (assigment) can be used InterlockedExchange or InterlockedExchangePointer functions they are more safety then simple volatile assignment. If you not performs cross threading variables usage in that macro - then simple assignmet will be fine.

Regards,
Maxim.


这篇关于类型转换还是函数指针?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆