Hot answers tagged

5

All the code looks like you are very experienced since you didn't make any obvious mistakes. Some small things to consider: I'd compile the release binary with assertions enabled since I prefer an obvious crash over undefined behavior. Since you don't include <assert.h> at all, you don't need the -DNDEBUG flags at all since they won't make any ...


4

String as formats Code uses printf(howManySides); yet howManySides is not described as a format. Such practice can lead to trouble when the string contains a %. Do not use a string as a format unless it is clearly stated in its definition that it is a format. // printf(howManySides); fputs(howManySides, stdout); Ensure output before input stdout may be ...


4

Bug strlen("\x80\x80\x80\x80\x80\x80\x80") does not return the expected 7. The do loop does not distinguish between '\0' and '\x80'. strlen("\x80\x80\x80") returns 0, 1, 2 or 3 depending on alignment. These render the function broken. Why int32_t? Certainly int32_t* marches down the sting at a 4x clip rather than a plodding char *. Yet for a 16-bit ...


3

We can eliminate the magic number 12 here: /* If we have a name for this polygon, then get it from the table. */ if(numSides <= 12) { name = names[numSides - 3]; } The common idiom for determining the number of elements in an array works by using the size of the array and the size of each element: static const int max_sides = sizeof names / sizeof *...


3

Potential bugs I just spent a few minutes reviewing this, so I'm not sure these are bugs, but you should have a look at them: The comma operator is not the same as &&. You have this code: for (int i = 0; i < ipSrcLen, addrIdx < 4; i++) { but I think you meant to use && instead of ,. I'm not sure it matters because I'm not sure what ...


2

As noted, the zero detection trick would also detect any 0x80 bytes (€ in CP1252, various different characters in UTF-8 contain 0x80 as continuation byte, for example Hiragana mu: む = "\xE3\x82\x80") as if they were zero-terminators. There are slightly more expensive "contains zero byte" checks that avoid this, for example (sprinkle with parenteses as ...


2

Use X-Macro to construct parallel enum and array As the number of items increases, so does the effort required to keep the parallel constructions in sync. enum Combinator { POP, DUP, SWAP , FLIP, ID, QUOTE , UNQUOTE, UNKNOWN }; static const char Str_Combs[][8] = { "pop", "dup", "swap" , "flip", "id", "quote" , "unquote" }; Instead,...


2

Small review Bug case 'u': { ... *ptr = strtol_s(token, base, &strtolErr); can form the wrong value as strtol() can limit values to 231-1. Same with 'x'. Better to use strtoul(). Type conflagration long strtol_s() looks like a function to convert a string to long yet it errors when out of int range and is curiously used in the code only for ...


1

C's string implementation is null-terminated. Thus, as long as, one doesn't care about the original string, (this may not be possible if you have a const char *,) and one has at least one char that one is willing to discard per block, (usually the block delimiter,) to turn into '\0', one can do this in place without copying. This tokenizing is very common, ...


1

Your compiler should be smart enough to replace the division by multiplication. Here's an experiment I did with CLang on x86_64: #include <inttypes.h> uint8_t div8_100(uint8_t a) { return a / 100; } uint8_t div8_10(uint8_t a) { return a / 10; } clang -O3 -Wall -Weverything -S div8.c div8_100: movzbl %cl, %eax leal (%rax,%rax,4), %ecx ...


Only top voted, non community-wiki answers of a minimum length are eligible