How to interface to a C struct with volatile variables?

Lee Braiden leebraid at gmail.com
Sat Feb 23 16:11:03 PST 2013


On Sun, 17 Feb 2013 10:58:57 -0800, Charles Hixson wrote:
> Thank you.  Since "volatile", IIUC, is suppose to mean "This think can
> change without warning, and for no reason that you'll be able to see.",

It ALSO means, "This can be read by other processes / devices at any 
time, so don't assume you can ignore whatever I put in there, just 
because I don't use it later myself."

I think it was Walter who said that D just doesn't do this kind of 
volatility awareness, and the only way to achieve it in D is to write 
your code with inline assembly. That seems like a shame, to me.

> I think perhaps I should just use a normal variable of the correct size,
> and comment it as "/**< DON'T USE THIS VARIABLE*/".  My only qualm about
> this was that the compiler might write the current value of the
> structure back over the structure parameter.  (Well, actually since I
> don't intend to use ANY of the variables in the struct, but just pass it
> to another C routine in the same library, the opaque pointer solution
> works well in THIS case.  But I was worried about what to do if I needed
> to change one of the variables.)
> 
> Perhaps this is worrying unreasonably...it's definitely worrying about
> things I don't yet need to deal with.

These sound like valid concerns.  Maybe D's contracts will help you to 
guarantee that nothing's being changed, though?


-- 
Lee


More information about the Digitalmars-d-learn mailing list