15 ms·
Code review: * The struct members and variables pointing at string literals should be const. * People do break this all the time but typedefs ending in _t are
by asveikau 12y ago
Code review:
* The struct members and variables pointing at string literals should be const.
* People do break this all the time but typedefs ending in _t are reserved by POSIX.
* Your C99 style struct initializers are valid but it's notable that one popular compiler, MSVC, will not like them.
* If we are being idiomatic I would suggest pointer arithmetic to iterate instead of using a counter i.
- adamnemecek 12y ago> * If we are being idiomatic I would suggest pointer arithmetic to iterate instead of using a counter i. How exactly is one more idiomatic than the other?
- asveikau 12y agoIt's subjective. It feels more c like and natural to me to write while (foo->bar) and not while (foo[i].bar). The latter feels like the way you would do it somewhere else, grafted onto C. But it's not wrong, I don't have deep qualms with it, and that's why I put it last on my list.
- DSMan195276 12y agoI would have to agree. IMO it lends itself to cleaner code if you're working with NULL terminated lists, especially if you use a `for` when doing the iteration instead of a `while`: const char **a; for (a = job->accomplishments; *a; a++) printf(" - %s\n", *a); And job_t *j; for (j = jobs; *j; j++) print_job(j); The person who wrote this code seems to dislike `for`s for some reason it seems, these are pretty obvious places to use one IMO, using an index `i` or not. Doing it with a `while` separates out the initialization, condition, and increment in his code, and 'continue' won't work like you normally want it too.
- deleted 12y ago[deleted]
- danieldk 12y agoIt's not subjective. Intel's guidelines for writing vectorizable code: Prefer array notation to the use of pointers. C programs in particular impose very few restrictions on the use of pointers; aliased pointers may lead to unexpected dependencies. Without help, the compiler often cannot tell whether it is safe to vectorize code containing pointers. Source: https://software.intel.com/sites/default/files/8c/a9/CompilerAutovectorizationGuide.pdf https://software.intel.com/sites/default/files/8c/a9/Compile... (Section 5.1)
- chii 12y agoyep - signal your intentions as specifically to the compiler as possible, so that it can generate as optimized output as possible. Array access is more specific and intentional than pointer arithmetic.
- asveikau 12y agoWhat are the odds this turns out to be practical advice for this example? Are you going to write all your loops like that because someone at Intel told you it was a good idea, or are you going to measure it when it's seen to be a problem?
- danieldk 12y agoWhat are the odds this turns out to be practical advice for this example? High. E.g. gcc 4.8.2 vectorizes this on my machine (-march=native -mtune=native -O3 -ffast-math -ftree-vectorizer-verbose=1): for (size_t i = 0; i < n; ++i) { sum += a[i]; sum += b[i]; } (a and b have the same length) But doesn't vectorize: for (double *p = a; p < a + n; ++p) { sum += *p; sum += *(b + (p-a)); } (Yes, you can make an extra pointer for b, but just for the sake of the example.) tl;dr: your compiler writer probably knows better than you.
- asveikau 12y agoThe poster did not sum values. They used the loop to call into printf. I'm guessing they didn't also use those particular compiler options... What you have here sounds like a lot of tuning that does not apply equally for all scenarios.
- anta40 12y ago> * Your C99 style struct initializers are valid but it's notable that one popular compiler, MSVC, will not like them. The code compiled fine with MSVC 2013 (simply 'cl resume.c'). No warnings at all.
- asveikau 12y agoI think they they implemented it very recently. 2012 will fail.
- Aldo_MX 12y ago> People do break this all the time but typedefs ending in _t are reserved by POSIX. hahaha, shame on me, I always use _t to abbreviate types or make custom ones, ex. uint_t, fraction_t, bitfield32_t, etc... bitfield32_t: https://gist.github.com/AldoMX/526e84da13b00ac0c75e https://gist.github.com/AldoMX/526e84da13b00ac0c75e
- pjmlp 12y ago> If we are being idiomatic I would suggest pointer arithmetic to iterate instead of using a counter i. Well, the CVE guys need to keep their job.
- asveikau 12y agoHe's already iterating by using null termination instead of based on array bounds. I am suggesting he do it in the more common way. On another topic, I have noticed you post this sort of trollish c bashing on almost every hn thread about c. It's fine that you prefer something else, but maybe time to give it a rest?
- pjmlp 12y agoSomeone needs to spread the gospel of safe systems programming to young minds not exposed to the existing alternatives, before C grew out of UNIX. If C didn't need this kind of attention to safety, Clang, GCC and others wouldn't have all those nice analyzers built-in, not MISRA would be required. Besides, looking for my post history and fellow comments, I am not alone in this.
- agentultra 12y agoI think Peter Van Der Linden would agree. He states in Deep C Programming that the separation of the compiler from the linter, and I assume by extension semantic analysis tools as well, was a mistake in retrospect. It's good to see that compilers like Clang are bringing them back together in a way. One does have to be more careful when writing C but I don't "blame" C for unsafe code. It's the kind of language that assumes the programmer know what they're doing. In this regard it is very unforgiving.
- pjmlp 12y agoYes lint does exist since 1979 and Ritchie remarks in his notes that it was already a need back then. The problem got worse when C dialects started to appear outside UNIX without Lint. Or the compiler vendors selling it separately of the compiler. The problem with languages that trust the developer is that they are based on a set of false premises, namely: - the team is composed from top skill developers - the team never changes - everyone knows the whole code base Which are all false in the majority of corporations.
- amelius 12y agoStyle review: * Avoid using NULL in your resume. It has a negative connotation, and makes your resume look bad. * Make use of as many existing libraries as possible, to show that you're not the kind of programmer that wants to invent the wheel on every occasion. * Try to obfuscate your resume a little (but not too much!) As it is written here, your reader can easily guess what the program will do, and he/she will not even want to run it anymore. * #include a portfolio of your work inside your resume.
- evincarofautumn 12y agoYou had me going for a moment there. I was ready to respond to your comment about NULL before I realised you were joking.
- emergentcypher 12y agoI think we have bigger problems to address before we get to these minor contrivances. This resume is not web scale enough. We need to wrap the resume in node.js so it can be non-blocking for big data. It should be written in an esoteric JS variant and transpiled to ES5 because everyone knows ES5 is unreadable. Oh and the storage of his education and job history should really be stored in a NoSQL database like MongoDB that way he can shard asynchronously. When he presents the resume in his interviews it should be wrapped in a webkit container.