3 ms·
There are a million problem with C that come before curly braces. So many of the people that venerate C either live in a reality distortion bubble or don't actu
by optforfon 10y ago
There are a million problem with C that come before curly braces. So many of the people that venerate C either live in a reality distortion bubble or don't actually writing C.
I've always found C a disaster for generating good assembly.
to start:
https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/C-Extensions.html#C-Extensions https://gcc.gnu.org/onlinedocs/gcc-4.7.0/gcc/C-Extensions.ht...
we see that bare C has:
- no vectorization/SIMD support
- no hinting at likely branches
- no way to prefetch memory
- no way to block inlining
- no actual inlining!
whoever decided that the compiler should be allowed to ignore the 'inline' keyword ....
additional issues off the top of my head:
- const != immutable so 'const' is relegated to being a keyword for generating compiler warning
- RVO is implicit.. so just pray it happens!
The language is frankly just too old. Half of those features probably just simply didn't exist in hardware when the language was designed
I'm desperate for a better language
- rkangel 10y ago> const != immutable so 'const' is relegated to being a keyword for generating compiler warning I'm not sure what you mean here. On embedded targets, static data declared 'const' will be put in with the program memory, and so will be definitely read-only. Casting the pointer and writing to it will cause a hard fault (or segfault, or whatever the equivalent is on your platform).
- Terribledactyl 10y ago#include <stdio.h> int main(int argc, char argv) { const int *b = 9; b = 10; printf("%d",b); } what does (should) this print? (It compiles with warnings on llvm 7.3 / clang703 OSX)
- rkangel 10y agoIt will print 10. There's absolutely no issue there at all. You've changed the value of the pointer which is NOT const. const int \*b Means a pointer to a thing that is const. The pointer itself (which is on the stack in this case) is NOT const. const int *b = 9; *b = 10; ^^^ This will NOT compile. const int b = 9; int main() { int *a = (int *)b; *a = 10; } ^^^ This will compile (possibly with warnings). If you run it on an embedded target, this will crash: b will have been put in flash, so trying to write to it is a hard-fault. Yes, C allows you to do 'unsafe' things with pointers. You aren't going to fix that without throwing away C and starting again.
- Terribledactyl 10y agoNo I don't want to throw it out, I kinda like C. And you can also do seemingly weird things with it. I was trying to bring some light to how const and immutable could be conflated. A lot of people see const and think, no aspect of this is changeable.
- rkangel 10y agoIf you think that though, you haven't got a clear picture of what pointers are and how they work. The pointer itself and thing it is pointing to are two completely different variables in memory. Being unclear about the difference is likely to cause lots of problems when coding C. Using a pointer to iterate through a string/array/other data structure, that is stored in const is a completely standard thing to do.
- Terribledactyl 10y agoI do get what's going on here, maybe poor example. I was trying to show off some confused ways I've seen pointers and const used together, in attempt to confuse people. Some corollary of Poe's law maybe at play.
- jamessu 10y agoHis point was that you aren't interpreting the `const' the same way the compiler does. To the compiler, this: const int *a; a = NULL; // perfectly OK *a = 0; // does not compile Is a pointer to a constant int, not a constant pointer to an int. You're probably confusing it with this: int *const a; a = NULL; // does not compile *a = 0; // perfectly OK Which is a constant pointer to a regular int. Of course you can combine both as follows: const int *const a; a = NULL; // does not compile *a = 0; // does not compile Which is a constant pointer to a constant int (which, of course, makes no sense at all in this case since 'a' is uninitialized and cannot be initialized without an unsafe cast, but it's a perfectly valid statement in C).
- Terribledactyl 10y ago
- nkurz 10y agoI like that you have an example, but I'm not getting the results you are. Perhaps there is some oddity with your odd use of pointers? I switched the code to this: #include <stdio.h> int main(int argc, char **argv) { const int b = 9; b = 10; printf("%d",b); } And I get a very clear "error" and nothing compiled with gcc, icc, and clang. Did you maybe have a left over binary from a previous compilation? I didn't try OSX, but it's possible the real moral would be to always pay attention to warnings.
- Terribledactyl 10y agoIt's all about the underhanded trick(s) with the pointers, yours will never compile. I'm forcing clang to do horrible things.
- ArkyBeagle 10y agoThere's no veneration here. It's just eminently possible to use 'C' to get work done.