Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 21 Mar 2004 19:45:43 +1100
From:      Peter Jeremy <peterjeremy@optushome.com.au>
To:        Matt Emmerton <matt@gsicomp.on.ca>
Cc:        Garance A Drosihn <drosih@rpi.edu>
Subject:   Re: Adventures with gcc: code vs object-code size
Message-ID:  <20040321084543.GA48068@server.vk2pj.dyndns.org>
In-Reply-To: <002f01c40f14$f4406540$1200a8c0@gsicomp.on.ca>
References:  <p0602044cbc827481888c@[128.113.24.47]> <002f01c40f14$f4406540$1200a8c0@gsicomp.on.ca>

next in thread | previous in thread | raw e-mail | index | archive | help
On Sun, Mar 21, 2004 at 02:20:13AM -0500, Matt Emmerton wrote:
>
>----- Original Message ----- 
>From: "Garance A Drosihn" <drosih@rpi.edu>
>To: <hackers@freebsd.org>
>Sent: Saturday, March 20, 2004 5:45 PM
>Subject: Adventures with gcc: code vs object-code size
>> if (strcmp(elemcopy, ":") == 0)
...
>I don't know why the code bloats so much on i386, but I do question the use
>of strcmp() for a single-character compare.
>Something like the following would probably be better (and would avoid your
>problem):
>
>if (elemcopy[0] == ':')
>  inf->count = 0;
>else
>  inf->addelem(inf, elemcopy);

That code isn't equivalent in general.  Your code just checks that the
first character is ':'.  It doesn't verify that the string is exactly
one character long.  It's possible that in this particular context,
elemcopy may be constrained such that it must be ":" if the first
character is ':' but this isn't clear from Garances posting.  The
equivalent code would be:

if (elemcopy[0] == ':' && elemcopy[1] == '\0')
  inf->count = 0;
else
  inf->addelem(inf, elemcopy);

But (IMHO) this is a lot less clear than the former code (thought I admit
I'm guilty of doing this quite a lot in my code).  Note that a modern C
compiler is free to convert
  strcpy(elemcopy, ":") == 0
into
  elemcopy[0] == ':' && elemcopy[1] == '\0'
assuming the relevant header (<string.h>) is in scope.  (I was under the
impression that gcc did this).

Peter



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20040321084543.GA48068>