3 ms·
C does support this and has for decades. Many device drivers are written in C and do this exact thing.
by icedchai 7y ago
C does support this and has for decades. Many device drivers are written in C and do this exact thing.
- chrisseaton 7y agoThe claim was: > It is common to reference memory mapped peripherals by casting an integer to a struct pointer. If you check the C standard you'll see that C guarantees nothing about what happens when you do this. I work with a compliant C implementation where this would not work.
- icedchai 7y agoThe fact is that many (most?) implementations do support this. It is implementation specific. If it never worked, you'd never be able to write a device driver using memory mapped IO in C, which is pure lunacy.
- chrisseaton 7y ago> The fact is that many (most?) implementations do support this. Which implementations? GCC? The compiler used for the Linux kernel? Why do you think that? https://gcc.gnu.org/onlinedocs/gcc/Arrays-and-pointers-implementation.html https://gcc.gnu.org/onlinedocs/gcc/Arrays-and-pointers-imple... does not clarify what they do in this case. So it's not defined by the language nor the compiler documentation. How do we know what it does? Do you check the disassembly? What if it compiles one way one day and another the next because something else changed in your code? If you write C code to do this and compile it with the most common C compiler, and it works, then you're just lucky! Nobody is guaranteeing it will do anything at all! It's much better to do it in assembly, where you know it's doing what you want.
- icedchai 7y agoCasting integers to pointers is very common in embedded development. You can call these tutorials "wrong" but that is the opposite of what I have experienced in over 30 years of C programming. You can find 100's of these tutorials online. One example: https://developer.arm.com/tools-and-software/embedded/legacy-tools/ds-5-development-studio/resources/tutorials/accessing-memory-mapped-peripherals https://developer.arm.com/tools-and-software/embedded/legacy... Section 6.3.2.3 of the C standard indicates it is implementation defined: "An integer may be converted to any pointer type. Except as previously specified, the result is implementation-defined, might not be correctly aligned, might not point to an entity of the referenced type, and might be a trap representation." (See http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf )
- chrisseaton 7y ago> You can call these tutorials "wrong" I don't know who you're quoting there, but it isn't me! I said C "does not support" it. And it doesn't! > Section 6.3.2.3 of the C standard indicates it is implementation defined Yes... and how does the implementation tells it defines it? If you check the GCC documentation you'll find they don't commit to doing anything when you cast an integer to a pointer and then use it. They deliberately leave it undefined themselves. It could do absolutely anything. And not necessarily what you think it should based on existing code and online tutorials. If I was reviewing your code that did this, and I asked you "how do you know that this will work as you expect", you'd have nothing to stand on to back it up! You'd just be able to say "well everyone assumes it works". That's not good engineering! I know it's a common pattern to use. But who is supporting that it will work? Not the C language spec. Not the compiler documentation. It's not supported by anyone. Hence, "does not support it". Make sense now?
- icedchai 7y agoNope. You can look at the ARM example I included to see how one implementation defines it. It that correct or incorrect? It's implementation defined and supported by compiler vendors. Maybe not yours. If you can't find out if it's supported for your environment, you shouldn't do it. I agree. But to claim you can't do this with C in general is silly. You clearly can.
- chrisseaton 7y agoBut this whole thread is about GCC.