Live data from Hacker News

Programming Idioms

programming-idioms.org

21–30 of 99 posts

Re: Programming Idioms

#21

Earlier quoted context omitted.

It's not unreasonable to assume the compiler will optimize it to a single call. Though I guess people who are capable of making that judgement won't need to look this idiom up on the internet.

Is there something in the C spec that allows optimizing to a single strlen call?

Don't know about the C spec, but this is a very popular optimisation. It's called Loop-invariant code motion.

Re: Programming Idioms

#22
post #12
post #7

The random idiom I got was: > Idiom #120 Read integer from stdin > Read an integer value from the standard input into variable n int n[15]; fgets(n, 15, stdin); Really?

The longer I look at this example, the more weirdness I spot: - There are no standard integer types that take 15 (decimal) digits to represent. - The array contains ints instead of chars - Why would you use fgets() instead of just gets()? (Though I don't touch C very often so perhaps that is considered proper style) - Obviously no conversion of the digits into else, let alone specifying a base or handling a `0x` pref…

> Why would you use fgets() instead of just gets()?

I assume it's because gets() ranks as "-10: It's impossible to get right" on Rusty's API Design Manifesto? (http://sweng.the-davies.net/Home/rustys-api-design-manifesto)

Re: Programming Idioms

#23

Earlier quoted context omitted.

Is there something in the C spec that allows optimizing to a single strlen call?

Absolutely. The gist of it: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)."

As I understand it, the strlen implementation ("calling a function") is typically going to come from another object file (at link time), so it’s not clear that when compiling this file that “calling strlen has no side effects” is information available to the compiler.

Re: Programming Idioms

#24

Earlier quoted context omitted.

Absolutely. The gist of it: "In the abstract machine, all expressions are evaluated as specified by the semantics. An actual implementation need not evaluate part of an expression if it can deduce that its value is not used and that no needed side effects are produced (including any caused by calling a function or accessing a volatile object)."

As I understand it, the strlen implementation ("calling a function") is typically going to come from another object file (at link time), so it’s not clear that when compiling this file that “calling strlen has no side effects” is information available to the compiler.

strlen is a standard function (in a hosted environment). So it must do exactly what the standard says it does, and the standard doesn't say it has side-effects. The compiler could very well use a built-in implementation of strlen, or even omit the call entirely if it had another way to deduce its would-be return value.

Object files are an implementation detail not known by the C standard.

Re: Programming Idioms

#27
post #12
post #7

The random idiom I got was: > Idiom #120 Read integer from stdin > Read an integer value from the standard input into variable n int n[15]; fgets(n, 15, stdin); Really?

The longer I look at this example, the more weirdness I spot: - There are no standard integer types that take 15 (decimal) digits to represent. - The array contains ints instead of chars - Why would you use fgets() instead of just gets()? (Though I don't touch C very often so perhaps that is considered proper style) - Obviously no conversion of the digits into else, let alone specifying a base or handling a `0x` pref…

   man gets

   ...
   
   SECURITY CONSIDERATIONS
     The gets() function cannot be used securely.  Because of its lack of
     bounds checking, and the inability for the calling program to reliably
     determine the length of the next incoming line, the use of this function
     enables malicious users to arbitrarily change a running program's func-
     tionality through a buffer overflow attack.  It is strongly suggested
     that the fgets() function be used in all cases.  (See the FSA.)

Re: Programming Idioms

#30
post #22
post #12

Earlier quoted context omitted.

The longer I look at this example, the more weirdness I spot: - There are no standard integer types that take 15 (decimal) digits to represent. - The array contains ints instead of chars - Why would you use fgets() instead of just gets()? (Though I don't touch C very often so perhaps that is considered proper style) - Obviously no conversion of the digits into else, let alone specifying a base or handling a `0x` pref…

> Why would you use fgets() instead of just gets()? I assume it's because gets() ranks as "-10: It's impossible to get right" on Rusty's API Design Manifesto? ( http://sweng.the-davies.net/Home/rustys-api-design-manifesto )

TIL. I have been leading a sheltered life in languages with garbage collectors that obscured the true horror of gets() from me.
Post reply on HN