User: Password:
|
|
Subscribe / Log in / New account

Betrayed by a bitfield

Betrayed by a bitfield

Posted Feb 6, 2012 22:22 UTC (Mon) by giraffedata (subscriber, #1954)
In reply to: Betrayed by a bitfield by daglwn
Parent article: Betrayed by a bitfield

That's very interesting, because it seems to say the provision for "volatile" is so incomplete as to be a pointless language feature.
Not totally true. It's true that volatile doesn't do what most people expect. Basically, volatile says the data can't be cached in a register ...

I was commenting at a higher level. Never mind what the compiler can and can't do. The question is, what programs can you write in C because it has the "volatile" feature that you couldn't otherwise? Relying on nothing but that the compiler implements the C standard.

I've always understood the originally intended answer to be, "you can write a program that uses memory mapped I/O." But comments in this thread say regardless of how one uses the "volatile" keyword in a program, the compiler may always generate code that arbitrarily writes to memory mapped I/O addresses. There's nothing in the standard to stop it. If so, then you not only can't use memory mapped I/O in a C program, you can't even let a C program — any C program — run in an address space that includes memory mapped I/O regions.

Unless all memory-mapped I/O regions ignore writes.

And it's hard to believe that the definers of "volatile" actually had such a useless thing in mind.


(Log in to post comments)

Betrayed by a bitfield

Posted Feb 7, 2012 0:52 UTC (Tue) by daglwn (guest, #65432) [Link]

Again, this is implementation-defined behavior. The standard you're looking for is the ABI for your target. That's where all the memory layout, access size, calling convention and other low-level stuff is specified.

Volatile is perfectly fine for I/O as long as you know the address being accessed is suitably aligned to avoid problems, as the ABI should indicate.

This is why volatile is non-portable. Unfortunately, C99 has no standard way to force alignment of any object.

Betrayed by a bitfield

Posted Feb 7, 2012 8:46 UTC (Tue) by khim (subscriber, #9252) [Link]

This is why volatile is non-portable. Unfortunately, C99 has no standard way to force alignment of any object.

GCC, MSCV and other compilers include such an ability and C11 finally adds it to standard so it's all is not so bad...


Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds