warning: comparison with string literal results in unspecified behaviour

Andrew Haley aph@redhat.com
Tue Jan 22 12:32:00 GMT 2008

Russell Shaw wrote:
> Andrew Haley wrote:
>> Russell Shaw wrote:
>>> How do i disable that? My code explicitly compares string pointers.
>> The best way is to intern all your strings.  String interning is a
>> fairly common efficient technique and you can look it up in Google.
> I've seen that in Xlib code. There's no reason to do it if you're
> not storing strings on a remote host.

Not necessarily.

> I store pointers to const strings in various objects and use them
> much like magic numbers, so direct pointer comparisons are the ideal
> thing to do.
> This warning never happened on older gcc versions.
> Infact, the warning is incorrect for this valid use.
> It's just unnecessary nannying.
> I've been using it in thousands of lines of code for 5 years.

OK, this might be a bug in gcc.  I just looked at the gcc source
(appended below) and as far as I can see that message is only
generated when there is a real error such as

    if (str == "Hello")

Do you have a case where conforming code triggers this warning?


   /* Warn about comparisons against string literals, with the exception
      of testing for equality or inequality of a string literal with NULL.  */
   if (code == EQ_EXPR || code == NE_EXPR)
       if ((code1 == STRING_CST && !integer_zerop (arg2.value))
	  || (code2 == STRING_CST && !integer_zerop (arg1.value)))
	warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");
   else if (TREE_CODE_CLASS (code) == tcc_comparison
	   && (code1 == STRING_CST || code2 == STRING_CST))
     warning (OPT_Waddress, "comparison with string literal results in unspecified behavior");

More information about the Gcc-help mailing list