Article View: comp.lang.c
Article #201Re: compare literals / if( "cat"== "dog") {}
From: Johann 'Myrkraverk' Oskarsson
Date: Sun, 11 Oct 2026 10:38
Date: Sun, 11 Oct 2026 10:38
119 lines
5497 bytes
5497 bytes
On 10/11/2026 5:34 PM, fir wrote:
> Richard Harnden pisze:
>> On 11/10/2026 08:50, fir wrote:
>>> fir pisze:
>>>> fir pisze:
>>>>> fir pisze:
>>>>>> fir pisze:
>>>>>>> Johann 'Myrkraverk' Oskarsson pisze:
>>>>>>>> On 10/11/2026 5:02 AM, fir wrote:
>>>>>>>>> if("cat" == "cat") will return true and
>>>>>>>>> if("cat" == "dog") will return false
>>>>>>>>>
>>>>>>>>>
>>>>>>>>> i probably asked that but if so once again
>>>>>>>>>
>>>>>>>>> is there way to enforce it to work?
>>>>>>>>
>>>>>>>> Sort of. If you're /only/ comparing literals, you can apply
>>>>>>>> the /GF
>>>>>>>> switch in MSVC, and then all the literals will compiled into a
>>>>>>>> single
>>>>>>>> pool, and you can compare by the pointers.
>>>>>>>>
>>>>>>>> Of course, the pointers still point to the pool, and that's
>>>>>>>> important
>>>>>>>> to remember.
>>>>>>>>
>>>>>>>> I'm sure GCC and clang have equivalent switches.
>>>>>>>
>>>>>>> there is -megre-constants but im not sure if it 1) will
>>>>>>> guarantee (at least ractically) that it will work 2) if it will
>>>>>>> work at least in my own case
>>>>>>>
>>>>>>> i could eventually write a wrapper tag_eq(char*, char*) or
>>>>>>> something which will be comparing by == and != and have fallback
>>>>>>> ino strcomp eventually, possibly its worth trying
>>>>>>
>>>>>> i mean something like that (which has ugly form but i got no time
>>>>>> to think on form)
>>>>>>
>>>>>>
>>>>>>
>>>>>> enum {COMPARE_STRING_TAGS_BY_POINTERS = 1}; //may be dangerous, it
>>>>>> assumes same text literals have same pointers
>>>>>> enum {TEST_TAG_COMPARISONS =1};
>>>>>>
>>>>>> static inline int tag_eq(const char *a,const char *b)
>>>>>> {
>>>>>>
>>>>>> if(TEST_TAG_COMPARISONS)
>>>>>> {
>>>>>> int p = a==b;
>>>>>> int s = strcmp(a,b)==0;
>>>>>> if(p!=s) ERROR_("pointer comparison of string tag !=
>>>>>> strcopy version (which means it fails)");
>>>>>> return p;
>>>>>> }
>>>>>>
>>>>>> if(COMPARE_STRING_TAGS_BY_POINTERS)
>>>>>> return a==b;
>>>>>> else
>>>>>> return strcmp(a,b)==0;
>>>>>> }
>>>>>>
>>>>>>
>>>>>> i mean if app uses extensively this string literal tags and tag_eq
>>>>>> then if test is on then if there is difference there will be
>>>>>> rontime error so if there is no error in non debug version thet
>>>>>> test can be disabled and it should work
>>>>>>
>>>>> i tested in slightly in my app and it work i mean test dont fail
>>>>> but i only began to use this literal tags form yesterday and use it
>>>>> very little
>>>>
>>>> oh no i ommited some change (from strcmp to tag eq) it fails
>>>>
>>>> probably depending on compilation flags
>>>>
>>>>
>>> nah it showed that in a code i got strcopy based solution before so
>>> it should fail
>>>
>>> changed on proper char* and now it runs ok, so as for now at least
>>> this tags based on literal pointers seem to work
>>>
>>
>> If you want to compare strings by their pointers, then those pointers
>> will need to come from a stable pool, like, say, a hash table.
>>
>> const char *hash_str_insert(hash *hash, const char *s);
>>
>> ...
>>
>> const char *a = hash_str_insert(hash, "cat");
>> const char *b = hash_str_insert(hash, "dog");
>> const char *c = hash_str_insert(hash, "cat");
>>
>> if ( a == b ) /* no */
>>
>> if ( a == c ) /* yes */
>>
>>
> it is an option, maybe not insert it but ancode it in some key
>
> unsigned key = encode("asdasdas");
If you go this route, you can always check out Mailund's book, /The
Joys of Hashing/, but he does not cover the actual hashing function,
so you'll have to find that elsewhere. Lookup3() and MurmurHash are
not bad choices. See
https://en.wikipedia.org/wiki/Non-cryptographic_hash_function
for a list.
--
Johann | email: invalid -> com | http://www.myrkraverk.com/blog/
I'm not from the Internet, I just work there. | via Easynews.com
https://bsky.app/profile/myrkraverk.bsky.social | for ( ;; ) _:;
Federated at https://fed.brid.gy/bsky/myrkraverk.bsky.social
Technical Details
Message-ID:
<hGJyS.25112$YNTa.1959@fx01.ams4>Path:
mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx01.ams4.POSTED!not-for-mailReferences:
<11ae962$2365v$1@dont-email.me> <9tyyS.21373$VDn8.13923@fx14.ams4> <11afcku$2dli3$1@dont-email.me> <11afe36$2e577$1@dont-email.me> <11afe6j$2e577$2@dont-email.me> <11afedm$2e577$3@dont-email.me> <11aff4q$2egej$1@dont-email.me> <11afjjk$2g358$1@dont-email.me> <11afl7v$2giqh$2@dont-email.me>Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!peer03.ams4!peer.am4.highwinds-media.com!news.highwinds-media.com!fx01.ams4.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla Thunderbird
Subject: Re: compare literals / if( "cat"== "dog") {}
Newsgroups: comp.lang.c
References: <11ae962$2365v$1@dont-email.me> <9tyyS.21373$VDn8.13923@fx14.ams4>
<11afcku$2dli3$1@dont-email.me> <11afe36$2e577$1@dont-email.me>
<11afe6j$2e577$2@dont-email.me> <11afedm$2e577$3@dont-email.me>
<11aff4q$2egej$1@dont-email.me> <11afjjk$2g358$1@dont-email.me>
<11afl7v$2giqh$2@dont-email.me>
Content-Language: en-US
From: Johann 'Myrkraverk' Oskarsson <johann@myrkraverk.invalid>
Organization: Watcom Pro Ltd.
In-Reply-To: <11afl7v$2giqh$2@dont-email.me>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Lines: 119
Message-ID: <hGJyS.25112$YNTa.1959@fx01.ams4>
X-Complaints-To: abuse@easynews.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Sun, 11 Oct 2026 18:38:03 +0800
X-Received-Bytes: 5312