5 ms·
It'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 some
by asveikau 12y ago
It'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.