How to interface to a C struct with volatile variables?

Jacob Carlborg doob at me.com
Sat Feb 16 07:19:10 PST 2013


On 2013-02-16 15:13, Ben Davis wrote:
> On 16/02/2013 03:54, Charles Hixson wrote:
>> Does D have an equivalent to the C marking of volatile?
>>
>> Currently I'm just going to try removing all variables from the struct,
>> as it's never declared in D or accessed from within D, and I just want
>> to avoid cast(void*)s, but I may need to access something like this
>> sometime in the future.
>
> In your case, I think you can declare opaque structs in D:
>
> struct S;    //Opaque
> struct S { ... }    //Not opaque
>
> As for 'volatile', there's some info at
> http://dlang.org/deprecate.html#volatile about how it used to be
> available for marking statements as 'do not optimise field accesses'.
> The corrective action listed there is to use 'synchronized' instead. So
> probably your best bet is to use 'synchronized' blocks wherever you
> access the variables that you know are meant to be volatile.
>
> Hope that helps :)

If you're interfacing with C you should just remove the volatile 
statement/keyword.

"There is no volatile type modifier in D. To declare a C function that 
uses volatile, just drop the keyword from the declaration."

http://dlang.org/interfaceToC.html

-- 
/Jacob Carlborg


More information about the Digitalmars-d-learn mailing list