On 05/10/2017 23:14, Kevin Lyda wrote:
> That's not actually correct in practice.

And top posting is?


> If you'd like to see that I'm
> correct take the following two programs:
> 
> foo.c:
> #include <string.h>
> #include <stdio.h>
> 
> int main(int argc, char *argv[]) {
>   if (strlen(argv[0]) == 0) {
>     printf("Command empty");
>   } else {
>     printf("Command not empty");
>   }
> }
> bar.c:
> #include <string.h>
> #include <stdio.h>
> 
> int main(int argc, char *argv[]) {
>   if (*argv[0] == '\0') {
>     printf("Command empty");
>   } else {
>     printf("Command not empty");
>   }
> }
> 
> Next compile them to assembly like so:
> 
> for f in {foo,bar}.c; do gcc -O2 -S $f; done

Interesting that you assume I use bash and gcc.
This is more portable.
for f in foo bar; do cc -S $f.c; done

> And then compare them:
> 
> diff {foo,bar}.s
>
> They should be the same (possibly different by a ".file" line). And if
> you inspect the resulting code, there won't be a call to strlen or to
> any function at all (well, except printf).

netbsd$ diff foo.s bar.s
2c2
<       .file   "foo.c"
---
>       .file   "bar.c"
22,24c22,24
<       movq    (%rsi), %rdi
<       callq   strlen
<       cmpq    $0, %rax
---
>       movq    (%rsi), %rsi
>       movsbl  (%rsi), %edi
>       cmpl    $0, %edi

Now, if I do apply -O2 then clang (my system compiler) does indeed
optimise it away.
What is worring is that even with -O0 or no -O gcc *always* optimises it
away. What if I want to call the function to be called regardless?

> The reason is that gcc, like most C compilers since the 90s, optimises a
> number of common functions in the standard C library. Which means that
> developers can stick with writing code that will be well-optimised *and*
> highly readable.

Spoken like a man who has never dealt with compiler issues!

if (strlen(foo) != 0)
if the function to calculate the length of the string is not zero

if (*foo != '\0')
if the first character of the string is not NUL

I'm having a hard time beliving that the former is more readable AND
just as performant.
It has more text for the brain to digest and a man page to read per
platform.

Or are you going to assume that gcc is the only compiler ever used anywhere?

Roy

_______________________________________________
Dnsmasq-discuss mailing list
Dnsmasq-discuss@lists.thekelleys.org.uk
http://lists.thekelleys.org.uk/mailman/listinfo/dnsmasq-discuss

Reply via email to