3 ms·
"Not mentionned, about being an expert C programmer, is knowing the pitfalls of C, (cf. undefined behaviors)" I am an expert in C, I had been decades writing o
by Htsthbjig 12y ago
"Not mentionned, about being an expert C programmer, is knowing the pitfalls of C, (cf. undefined behaviors)"
I am an expert in C, I had been decades writing on it and other languages, and managing teams of coders. We created a company that used it a lot.
I can't understand what undefined behaviors C has, because it is the most simple and defined language I do know of. I have lots of experience writing assembler,fortran, Lisp, c++, python, objective C and also use C#, java, javascript and other web languages and functional languages from time to time.
c++, java could be extremely undefined because the behavior depends on conventions, committees and implementations. E.g we had to change some code because of different compilers interpretation of the standars.
But c? c is basically portable assembler. If you understand how computers work it is extremely reliable bar none. We have ported years of work of dozen programmers in one day. With c++ and "write once, run everywhere marketing" java we spent months.
It was also terribly frustrating for the team. Fontforge author had a similar experience writing c++ compilers and that experience was so traumatic the interface of Fontforge is so ugly as he does not want to use c++ with a ten foot pole.
We try to avoid pitfalls more in high level languages because the programmer has the ability to write code without understanding what is really happening in the processor, or even in the program. Recipe programmers population is growing a lot this days.
- mahkoh 12y ago>If you understand how computers work it is extremely reliable bar none. On my processor, INT64_MAX + 1 will be INT64_MIN, a negative value. But the compiler is free to turn for (int64_t i = 1; i > 0; i++) { f(i); } into an infinite loop. Understanding how the computer works without reading about undefined behavior will make you fall into these kinds of traps.
- musername 12y agomissing overflow guards is just one of the big disappointments in c, if you have experience with asm for any popular processor atm.
- MrGando 12y agoWasn't part of the scope of the language. It's a doble edged sword, but I could see that being a good thing too. If you want raw speed, and know what you're doing, it could be OK not to have overflow guards.
- deleted 12y ago[deleted]
- GregBuchholz 12y agoHere is a fun site to play with some of the more nuanced corners in C: http://www.gowrikumar.com/c/index.php http://www.gowrikumar.com/c/index.php
- jeffreyrogers 12y ago> I can't understand what undefined behaviors C has, because it is the most simple and defined language I do know of. C has lots of undefined behaviors because the language was designed to be easy for compiler writers to implement. As such, a lot of decisions were left to the compiler writers, which is what "undefined" means. Here is a (probably partial) list of undefined behaviors in C. (For a full list you'd have to search through the standard): http://blog.regehr.org/archives/213 http://blog.regehr.org/archives/213 Edit: Linked to a C++ article by mistake, so I changed it.
- goodcanadian 12y agoSure, there is a lot of code you could write that results in undefined behaviour. However, none of it is code that you should write, nor is it code that an expert would write.
- pascal_cuoq 12y agoWhile your sentence can be true until we get to defining “expert”, the problem is that there are not going to be many experts if to be an expert at C, one has to be able to write useful code that does not accidentally invoke undefined behavior. - these four bug reports in NTP are for undefined behaviors. You can say that the NTP maintainers are not experts, the problem is that they are the maintainers of NTP: http://bugs.ntp.org/buglist.cgi?emailreporter2=1&emailtype2=substring&emailcc2=1&query_format=advanced&email2=trust-in-soft&product=ntp http://bugs.ntp.org/buglist.cgi?emailreporter2=1&emailtype2=... - a famous undefined behavior was found in OpenSSL recently, but there were and remain plenty more. Sure, the authors of OpenSSL aren't experts, the problem is, they are the people writing and maintaining OpenSSL. - The Linux kernel has had its share of undefined behaviors. Usually, the developers blame it on the compiler, of which the kernel admits only one (GCC—at least as of recently). There was the time when GCC was blamed for taking advantage of strict aliasing rules, and there was GCC removing of a NULL test on an execution path that dereferenced NULL. If the developers wrote for more compilers, they would realize that the optimizations practiced by GCC are practiced by other compilers too, because they are justified by undefined behavior in the source code. - I could go on. I have quoted three useful, widely deployed C pieces of software that have contained undefined behavior, and likely still contain more. Can you name one nontrivial C program, written by an expert according to your definition, that you are confident does not invoke undefined behavior?
- Dewie 12y ago> I can't understand what undefined behaviors C has, because it is the most simple and defined language I do know of. "Undefined behaviour" is a very simple and well-defined term when it comes to languages like C. Undefined behaviour is code who's behaviour is specified to be arbitrary. And languages like C is famous for having them. (I'm assuming that you were being honest about not knowing about undefined behaviour means in C, and not trying to re-frame the word to mean something else.) An example is overflow on signed integers. Strictly speaking it does not help that you know "how computers work" since you have no guarantees about what will happen if an overflow happens in your program. You can't assume that a wrap-around will happen and base your program on that, since that might be a faulty assumption on some compilers. Undefined behaviour allows the compiler to assume that it never happens and introduce optimizations based on that assumption. For example, use of initialized variables is undefined. This allows the compiler to avoid having to initialize variables (like arrays) to some default value when they are declared.
- noproblemo 12y agoe.g. Take the following a = f() + g() * h(); Now in this, it is undefined whether g() is going to be executed first or h(). The precedence in this case, I mean. Precedence is well defined for operators, but there are certain things, which I still haven't figured out yet.
- readerrrr 12y agoIt is unspecified not undefined. Important difference.
- NAFV_P 12y ago> But c? c is basically portable assembler. I wouldn't call myself an expert, but I avoid calling C portable assembler. I tend to think of C as having low level data manipulation and high level flow.
- claren 12y agoIt might not be applicable anymore, as the compiler optimizations become more advanced to the point that it is hard to predict the translated machine-code. This is one of the most important points with regards to tool-chain specific knowledge. The other is proper usage of the debugger.