[wplug] C compiling question

Mark Dalrymple wplug at badgertronics.com
Tue Oct 1 08:09:43 EDT 2002


> static const char true[] = "true";
> char *foo = NULL;

> foo = setfoo();

> if(foo == true){
>     // do stuff if foo is true
> }

> so I'm all set in terms of getting it to work, but I'm curious if gcc
> ever compiled this so it would work?

There's a lot of "it depends" caveats in answering the question.

If whomever wrote that expected '==' to do a string comparison test
(vs string identity check) the code is incorrect.  gcc being a C
compiler doesn't do anything convenient for you like implicitly
writing a loop to step through the strings character by character
comparing them.

setfoo() is the big question.  Does setfoo() return just an arbitrary
string (say something from a config file, or from a parsed url or HTTP
request)?  If so, the code is incorrect since the contents of the
two strings may be the same, the addresses where those strings live could
(and probably are) different.

If setfoo() does some work, like getting the string value from a hash
table specifically constructed to always return the same string
address for a particular sequence of characters (specifically the
address of 'true', since it's a static variable the address will be
constant), then the equality check will give the correct results.
(this is a handy optimization when you're using character strings as
constants and only checking for equality.  You can compare by address
when you use this hash table trick rather than walking the entire
string each time)

There is a potential third case if in the case above true[] were
just a literal string rather than being an array initialization. 
In such a situation, it'd be an implementation detail whether literal
constants defined in multiple C files would map to the same address
at run time (the linker has the option of coalescing string constants)

What I'd do in this case is stick a little tracing code
before the (foo == true) test:


printf ("foo: '%s'   true: '%s'   foo == true: %d   foo <-> true: %d\n",
        foo, true, foo == true, strcmp(foo, true) == 0);

If you get a whole lot of:

foo: 'true'   true: 'true'   foo == true: 1  foo <-> true: 1

Then you know that it's most likely doing the constant-string trick in
setfoo().

If you get any

foo: 'true'   true: 'true'   foo == true: 0  foo <-> true: 1

Then that isn't the case and the code is in error.


ObShameless wPlug for those who have read this far:

I'm doing the tutorial this month (October 12), presenting part of a 
unix programming course I'm in the process of building, specifically
the "C Refresher" parts targeted at programmers who may or may
not be comfortable with the C programming language.  If you're
a Perl or Java (or even awk :-)  programmer wanting to see a little
more what C is like, feel free to come on over.  Bring a machine
with your favorite text editor and a C compiler.  Printed course 
materials will be provided.


Wheeeee,
++Mark Dalrymple, markd at badgertronics.com.  http://badgertronics.com
  "This manual page is still confusing."
     -- man sigvec(2) / BSD



More information about the wplug mailing list