5 ms·
A lot of C programmers make valid assumptions based on their system architecture and compiler. Is there any point trying to unify their obviously different prac
by ydcvjk 11y ago
A lot of C programmers make valid assumptions based on their system architecture and compiler. Is there any point trying to unify their obviously different practices, while at the same time ignore the Standard? No.
- MichaelCrawford 11y agoTo me, the whole point of C is to enable one to take advantage of one's system architecture. Thats not really possible in higher level languages.
- pcwalton 11y agoWell, one of the interesting takeaways from the article is that C as specified often does not let you take advantage of your system architecture. The specification has things like GPUs and segmented memory architectures in mind when it forbids you from doing seemingly reasonable things like taking the difference between the addresses of two separately-allocated objects, even though chances are very good what you're trying to do works just fine on all architectures you care about.
- AnimalMuppet 11y agoYes and no. The standard specification defines a C that is very portable. It is quite reasonable that you cannot portably take advantage of your system architecture, which means you cannot do it using C as defined by the standard. But you still can write stuff that looks just like C, that a C compiler will accept, and that lets you take advantage of your system architecture. You just need to understand that a new version of your compiler can break all of your nifty tricks.
- ydcvjk 11y agoFinally someone that understands.
- Gibbon1 11y agoNot really because there are two kinds of c programer out there. 1. Those that communicate with the outside world exclusively via library calls to an operating system. 2. Those that do not. People that live in world #1, usually don't get people living in world #2. Example. // wait for write ready while((spi.S & SPI_S_SPTEF_MASK) == 0) ; spi.D; spi.D = data; Guess what, No spi.D; is actually important. No just because the program never writes to spi.S does not mean you can delete the while loop. No you cannot reorder any of this and have it work.
- spdionis 11y agoWhy? That's the issue that people should solve. Disclaimer: I have no C experience.
- deleted 11y ago[deleted]
- deleted 11y ago[deleted]
- _dps 11y agoAre you saying something other than "you, the programmer, must declare spi to be volatile, or else the compiler might lay a bear trap for you?". Based on your comment it seems clear that you understand that... but given that you understand that I don't get the point you're trying to make (except perhaps that writing low-level hardware code in C often means actively stopping the compiler from screwing you over). (for non-C programmers: "volatile" can be approximated by "hey compiler, this variable can change unexpectedly even if you didn't do anything to change it, so don't use any optimizations that assume you're the only one changing it).
- ydcvjk 11y agoSorry, I have no idea what you are trying to say. Maybe stick to the relevant issue and be less abstract?
- 11y ago
- MichaelCrawford 11y agoIve worked on embedded systems where stuff like that would be a problem, howver I also knew the memory map for my target. I wanted to make some crypto run faster so i used memcpy on a pointer to a function to copy executable code from slow flash to fast ram.
- noselasd 11y agoI suspect the parent is more referring to people writing code that does e.g. foo(i++, i++); or e.g. relying on signed integer overflow behvior based on observing a toy program on their own machine - assuming the code will behave the same in all contexts, optimization levels or minor versions of the compiler
- jerf 11y ago"take advantage of one's system architecture." Yes, if your system is essentially a PDP-11. I don't mean that sarcastically; pcwalton's sibling message only begins to mention the ways in which C is not a match to modern systems. Vector processing, NUMA, umpteen caching layers, CPU features galore... it's not really a match to the "architecture" any more. (To the extent that you may think C supports those things, I don't really think "lets you drop arbitrary assembler in the middle of a function" constitutes "support". YMMV. To be fair to C there's a lot of features that seem to be unsupported by any high-level language today. Hardware moves way faster than programming languages. If you want to figure out what's coming after the current generation of languages, "a language that actually lets you use all the capabilities of modern hardware without dropping to assembler and giving up all the safety of the higher-level language" is at least one possible Next Big Thing.)
- arielby 11y agoC matches NUMA and caching just as well as assembly does. Basically the only thing C doesn't really expose is SIMD, but compiling "high-level" (i.e. C-level or above) code into SIMD is an open research problem (AFAIK) - I mean, you can easily compile array programs into SIMD, but the more general case is still open.
- pjmlp 11y agoReally?! How do you specify alignment and packing in ANSI C?
- stephencanon 11y ago_Alignof( ) and _Alignas( )?
- pjmlp 11y agoYeah, since C11 only. However very few compilers, specially in the embedded and real time OS space do offer C11 compliance. Gcc and clang aren't the only game in town. If one aims to write compliant C code much more compilers come into the picture.
- pjmlp 11y agoIt is also not possible in C. People just think C is any better. The standard doesn't say anything about SIMD, GPU, instruction reording, IO registers, interrupts... All of that are language extensions or library functions written in Assembly. Any programming language can offer similar extensions.
- Retra 11y agoNo, the whole point of C is to abstract away one's system architecture. C is a higher level language in this sense.
- Animats 11y agoMany C programmers assume a single flat memory space. Most machines today have that. There's a long history of machines that didn't: Intel 286 in segmented mode, some Pentium variants in segmented mode beyond 4MB (Linux supports this in the kernel), IBM AS/400, Unisys Series B 36-bit word machines, Burroughs 5500/6700/Unisys Series A segmented machines where an address is a file-like path, Motorola CPUs where I/O space and memory space are distinct, and old DEC PDP-11 machines where code and data address spaces are separate. More recently, there are non shared memory multiprocessors where addresses are duplicated across processors - the PS3's Cell and many GPUs, for example. Most programmers today will never encounter any of those.
- kevin_thibedeau 11y agoThere's also the assumption that null is address 0. Notably breaking the purity of C++s type system with magic 0s for 20 odd years before nullptr came along.
- michaeljsmith 11y agoActually the C++ spec says that 0 as a pointer doesn't actually have to have the value 0 - it just refers to a null value the same way as nullptr.
- kevin_thibedeau 11y agoThat is the point. They went through the process of tightening down the type system by invalidating automatic casts and making void* less promiscuous and then they break that whole philosophy with a magic literal "0" that can work as a normal integer or as an address placeholder for any pointer even though it won't necessarily evaluate to address zero. At that point why can't I assign "0" to a float too and enjoy some more magic there?