* warning: comparison with string literal results in unspecified behaviour @ 2008-01-21 11:20 Russell Shaw 2008-01-21 21:59 ` Andrew Haley 0 siblings, 1 reply; 8+ messages in thread From: Russell Shaw @ 2008-01-21 11:20 UTC (permalink / raw) To: gcc-help How do i disable that? My code explicitly compares string pointers. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: warning: comparison with string literal results in unspecified behaviour 2008-01-21 11:20 warning: comparison with string literal results in unspecified behaviour Russell Shaw @ 2008-01-21 21:59 ` Andrew Haley 2008-01-22 11:29 ` Russell Shaw 0 siblings, 1 reply; 8+ messages in thread From: Andrew Haley @ 2008-01-21 21:59 UTC (permalink / raw) To: Russell Shaw; +Cc: gcc-help 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. Andrew. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: warning: comparison with string literal results in unspecified behaviour 2008-01-21 21:59 ` Andrew Haley @ 2008-01-22 11:29 ` Russell Shaw 2008-01-22 12:32 ` Andrew Haley 0 siblings, 1 reply; 8+ messages in thread From: Russell Shaw @ 2008-01-22 11:29 UTC (permalink / raw) Cc: gcc-help 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. 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. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: warning: comparison with string literal results in unspecified behaviour 2008-01-22 11:29 ` Russell Shaw @ 2008-01-22 12:32 ` Andrew Haley 2008-01-22 12:48 ` Russell Shaw 0 siblings, 1 reply; 8+ messages in thread From: Andrew Haley @ 2008-01-22 12:32 UTC (permalink / raw) To: Russell Shaw; +Cc: gcc-help 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? Andrew. /* 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"); ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: warning: comparison with string literal results in unspecified behaviour 2008-01-22 12:32 ` Andrew Haley @ 2008-01-22 12:48 ` Russell Shaw 2008-01-22 13:25 ` Russell Shaw 0 siblings, 1 reply; 8+ messages in thread From: Russell Shaw @ 2008-01-22 12:48 UTC (permalink / raw) Cc: gcc-help Andrew Haley wrote: > 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? > > Andrew. > > > /* 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"); Hi, I just found from the gcc list that Wno-address stops the complaints. I couldn't find that option on the gcc-3.4.3 manpage. Here is some code: #include<stdio.h> #include<stdlib.h> #define MAGIC "object" #define TYPE1 "type1" #define TYPE2 "type2" #define OB1(ob) (ob->type == TYPE1) #define OB2(ob) (ob->type == TYPE2) #define IS_OBJ (ob->magic == MAGIC) typedef struct { const char *magic; const char *type; } Object; static Object * object_new(const char *type) { Object *object = calloc(1, sizeof(Object)); object->magic = MAGIC; object->type = type; return object; } int main(int argc, char **argv) { Object *ob1 = object_new(TYPE1); Object *ob2 = object_new(TYPE2); Object *ob3 = ob2; if(ob1->type == ob2->type) { printf("same, %s %s\n", ob1->type, ob2->type); } else { printf("different, %s %s\n", ob1->type, ob2->type); } if(ob2->type == ob3->type) { printf("same, %s %s\n", ob2->type, ob3->type); } else { printf("different, %s %s\n", ob2->type, ob3->type); } if(ob3->magic == MAGIC) { printf("is object, %s\n", ob3->type); } } ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: warning: comparison with string literal results in unspecified behaviour 2008-01-22 12:48 ` Russell Shaw @ 2008-01-22 13:25 ` Russell Shaw 2008-01-22 13:31 ` Russell Shaw 0 siblings, 1 reply; 8+ messages in thread From: Russell Shaw @ 2008-01-22 13:25 UTC (permalink / raw) Cc: gcc-help Russell Shaw wrote: > Andrew Haley wrote: >> 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? >> >> Andrew. >> >> >> /* 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"); > > Hi, > I just found from the gcc list that Wno-address stops the complaints. > > I couldn't find that option on the gcc-3.4.3 manpage. > > > Here is some code: > > > #include<stdio.h> > #include<stdlib.h> > > #define MAGIC "object" > #define TYPE1 "type1" > #define TYPE2 "type2" > > #define OB1(ob) (ob->type == TYPE1) > #define OB2(ob) (ob->type == TYPE2) > > #define IS_OBJ (ob->magic == MAGIC) > > typedef struct { > const char *magic; > const char *type; > } Object; > > static Object * > object_new(const char *type) > { > Object *object = calloc(1, sizeof(Object)); > object->magic = MAGIC; > object->type = type; > return object; > } > > int > main(int argc, char **argv) > { > Object *ob1 = object_new(TYPE1); > > Object *ob2 = object_new(TYPE2); > > Object *ob3 = ob2; > > if(ob1->type == ob2->type) { > printf("same, %s %s\n", ob1->type, ob2->type); > } > else { > printf("different, %s %s\n", ob1->type, ob2->type); > } > > if(ob2->type == ob3->type) { > printf("same, %s %s\n", ob2->type, ob3->type); > } > else { > printf("different, %s %s\n", ob2->type, ob3->type); > } > > if(ob3->magic == MAGIC) { > printf("is object, %s\n", ob3->type); > } > } > Scrap that example. ob3->magic == MAGIC is two separate strings. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: warning: comparison with string literal results in unspecified behaviour 2008-01-22 13:25 ` Russell Shaw @ 2008-01-22 13:31 ` Russell Shaw 2008-01-22 13:45 ` Andrew Haley 0 siblings, 1 reply; 8+ messages in thread From: Russell Shaw @ 2008-01-22 13:31 UTC (permalink / raw) Cc: gcc-help Russell Shaw wrote: > Russell Shaw wrote: >> Andrew Haley wrote: >>> Russell Shaw wrote: >>>> Andrew Haley wrote: >>>>> Russell Shaw wrote: >>>>>> How do i disable that? My code explicitly compares string pointers. ... > Scrap that example. ob3->magic == MAGIC is two separate strings. My code is full of that problem and has been relying on gcc combining identical strings into one address. It has worked, but probably isn't guaranteed. I'll change to some kind of internment thing. Thanks for the help. ^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: warning: comparison with string literal results in unspecified behaviour 2008-01-22 13:31 ` Russell Shaw @ 2008-01-22 13:45 ` Andrew Haley 0 siblings, 0 replies; 8+ messages in thread From: Andrew Haley @ 2008-01-22 13:45 UTC (permalink / raw) To: Russell Shaw; +Cc: gcc-help Russell Shaw wrote: > Russell Shaw wrote: >> Russell Shaw wrote: >>> Andrew Haley wrote: >>>> Russell Shaw wrote: >>>>> Andrew Haley wrote: >>>>>> Russell Shaw wrote: >>>>>>> How do i disable that? My code explicitly compares string pointers. > > ... > >> Scrap that example. ob3->magic == MAGIC is two separate strings. > > My code is full of that problem and has been relying on gcc combining > identical strings into one address. It has worked, but probably isn't > guaranteed. It definitely isn't guaranteed. :-) > I'll change to some kind of internment thing. Thanks for > the help. Cool, good luck. Andrew. ^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2008-01-21 13:03 UTC | newest] Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2008-01-21 11:20 warning: comparison with string literal results in unspecified behaviour Russell Shaw 2008-01-21 21:59 ` Andrew Haley 2008-01-22 11:29 ` Russell Shaw 2008-01-22 12:32 ` Andrew Haley 2008-01-22 12:48 ` Russell Shaw 2008-01-22 13:25 ` Russell Shaw 2008-01-22 13:31 ` Russell Shaw 2008-01-22 13:45 ` Andrew Haley
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox; as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).