16 ms·
>> The C standard requires that pointers generally be created from referencing a valid, existing object. It is common to reference memory mapped peripherals by
by phkahler 7y ago
>> The C standard requires that pointers generally be created from referencing a valid, existing object.
It is common to reference memory mapped peripherals by casting an integer to a struct pointer. In that case only a human can certify the validity of the object.
- chrisseaton 7y agoThe C language does not support what you're trying to do. You should use assembly to do this.
- pjmlp 7y agoI bet plenty of embedded C code is full of such casts.
- icedchai 7y agoC 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?
- nrclark 7y agoThis is actually more subtle than one would think. Say you were trying to talk to three UART registers at 0xA000, 0xA004, and 0xA008. It's legal to access: *(volatile uint32_t *)(0xA000); *(volatile uint32_t *)(0xA004); *(volatile uint32_t *)(0xA008); The struct equivalent of this would be something like: struct UART_Peripheral { volatile uint32_t control; volatile uint32_t txdata; volatile uint32_t rxdata; }; struct UART_Peripheral *uart0 = 0xA000; uart0->control; uart0->txdata; uart0->txdata; As long as your structure is packed/aligned correctly, this is perfectly valid C. You can enforce the structure packing with an __attribute__ ((__packed__)) if using GCC or Clang. I think MSVC has a pragma for it. I know that the C language spec allows for structure padding, but I'm not sure if the concept is applicable if every member of the structure is the native word-size of the target CPU. But regardless of whether the spec specifies this expicitly or not, I've never seen a compiler insert padding in-between struct members that are already word-aligned. So the code might be very slightly questionable if written in pure C with no packing pragmas/attributes - but even then it would probably work on almost every target. And if used with the right pragma/attribute for your compiler, it's a perfectly valid construct. And it's also a common one - especially in embedded work or hardware drivers.
- chrisseaton 7y agoThe C standard doesn't define what happens when you cast an integer to a pointer like that. For example I work with a compliant C implementation where this would not work, because it uses fat pointers.
- nrclark 7y agoInteresting! I didn't know that. Does uintptr_t exist on your platform? If so, how does it work?