Groups | Search | Server Info | Keyboard shortcuts | Login | Register
Groups > comp.os.linux.misc > #37436
| Subject | Re: Contents of file changed while application is reading it |
|---|---|
| Newsgroups | comp.os.linux.misc |
| References | (15 earlier) <ttsejp$279gm$64@dont-email.me> <qPadnQSthu_CfJ_5nZ2dnZfqn_ednZ2d@earthlink.com> <wwvcz5o7sfd.fsf@LkoBDZeT.terraraq.uk> <CtScnbUpEtjMcp75nZ2dnZfqnPWdnZ2d@earthlink.com> <tu264t$vn7i$18@dont-email.me> |
| From | "28B.I874" <28B.I874@noabzba.net> |
| Organization | gear transect |
| Date | 2023-03-06 01:06 -0500 |
| Message-ID | <DfWcncAlN-hkH5j5nZ2dnZfqn_qdnZ2d@earthlink.com> (permalink) |
On 3/5/23 8:40 AM, The Natural Philosopher wrote:
> On 05/03/2023 01:24, 28B.I874 wrote:
>> On 3/4/23 4:54 AM, Richard Kettlewell wrote:
>>> "28A.I873" <28A.I873@noabzba.net> writes:
>>>> Ah ... an interesting technical question about 'C' ...
>>>> assume you're looking for the position of a given
>>>> character in a string .......
>>>>
>>>> for(x=0;x<strlen(TheStr);x++)
>>>> {
>>>> if(TheStr[x]==TheChar) return(x);
>>>> }
>>>> return(-1);
>>>>
>>>> is a pretty common approach. HOWEVER, the QUESTION,
>>>> is "strlen(TheStr)" calculated over and over again
>>>> for every iteration ??? That seems SLOW, esp if
>>>> TheStr is BIG.
>>>
>>> The short answer to your question is: stop hand-coding the search and
>>> use strchr.
>>
>>
>> The above was just an example. Usually you'd want
>> to do a lot more than JUST find the index of TheChar.
>> strchr() gets rid of those few lines of code, but
>> what is IT doing, how they code it ? Same thing ?
>> Kinda has to be. I can dig into the .h and see ...
>>
> I would code it - if its what think it is, as
>
> char *strchr( char *p, char a)
> {
> do {
> if (a==*p) return p;
> }
> while (*p++);
> return NULL;
> }
Also very good. Saves a line or two and a local var, so
maybe faster.
But I do prefer clear indexes to pointer math ...
However in a "simple" case like this, either is
fairly clear. Now if you start nesting, or worse
recurring, it gets yucky quick :-)
> I think I DID code it once years ago, when I was asked to create stdlib
> and basic I/O functions for an embedded processor board.
>
> A modern c compiler would take that and make about 5 lines of assembler,
> and caching would make it lightning fast.
Not MUCH cache in microcontrollers. Sometimes none.
However I've seen compilers recognize when they can
"re-use" registers that aren't part of what you're
doing at the moment - and accessing those is fast.
>> Now just a few more lines and you have a "find Nth
>> instance of TheChar" and a few more lines you have
>> strtok() and a few more you have "return Nth ascii-
>> delimited field of TheStr" - which I needed yesterday
>> to parse-out stuff generated by a system() call so
>> I whipped this together >>
>> void StrField(char *ss,char *dlims,int fn)
>> {
>> int cnt=0;
>> char *p;
>> p=strtok(ss,dlims);
>> while (p != NULL)
>> {
>> if(cnt++>=fn) { strcpy(ss,p); return; } // found, return
>> p=strtok(NULL,dlims); // get another chip
>> }
>> ss[0]=0; // else return nothing
>> }
>>
>> which is a bit clunk but worked ok for what I needed.
>>
>>> The long answer is that it depends on the type of the pointer.
>>>
>>> If it’s ‘char *TheStr’ then the compiler is entitled to assume that
>>> the string doesn’t change during the execution of the code fragment. GCC
>>> and Clang will therefore call strlen only once.
>>
>> Hmmm ... but look at strtok():
>>
>> char *strtok(char *str, const char *delim)
>>
>> and a number of other "mod in-place" functions that DO
>> change the length of your *TheStr before all is done.
>> Sometimes you need to waste memory creating a safe copy
>> before you call such functions. Fine if TheStr is 255
>> bytes, not so great if it's megabytes.
>>
>>> If it’s ‘volatile char *TheStr’ then this assumption is prohibited - the
>>> string “may be modified in ways unknown to the implementation” in the
>>> language of the spec. (The intention is support memory-mapped hardware.)
>>> In this case a compiler must call strlen every time round the loop (and
>>> indeed GCC and Clang do so).
>>
>> Got it. The bugaboo with compilers (and lots of other things)
>> is that it is difficult to write a 'general solution' when
>> the INTENT of the user may not be so clear. 'volatile char*'
>> is one way to help with that but you never REALLY know what
>> the user has in mind.
>>
>> Anyhow, just for fun, I'm gonna actually CHECK to see when
>> strlen() is and is not re-evoked in loops like the sample
>> above.
>
Back to comp.os.linux.misc | Previous | Next — Previous in thread | Next in thread | Find similar
Contents of file changed while application is reading it Clark Smith <noaddress@nowhere.net> - 2023-02-26 18:52 +0000
Re: Contents of file changed while application is reading it Rich <rich@example.invalid> - 2023-02-26 19:20 +0000
Re: Contents of file changed while application is reading it Robert Heller <heller@deepsoft.com> - 2023-02-26 21:45 +0000
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-26 23:05 +0100
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-26 23:38 +0000
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-26 23:32 +0000
Re: Contents of file changed while application is reading it "David W. Hodgins" <dwhodgins@nomail.afraid.org> - 2023-02-26 19:06 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-27 00:26 +0000
Re: Contents of file changed while application is reading it "David W. Hodgins" <dwhodgins@nomail.afraid.org> - 2023-02-26 20:48 -0500
Re: Contents of file changed while application is reading it Richard Kettlewell <invalid@invalid.invalid> - 2023-02-27 09:03 +0000
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-27 09:58 +0000
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-27 11:16 +0100
Re: Contents of file changed while application is reading it Richard Kettlewell <invalid@invalid.invalid> - 2023-02-27 15:20 +0000
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-27 10:34 +0100
Re: Contents of file changed while application is reading it Richard Kettlewell <invalid@invalid.invalid> - 2023-02-27 09:49 +0000
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-27 11:13 +0100
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-27 10:02 +0000
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-27 11:13 +0100
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-27 10:17 +0000
Re: Contents of file changed while application is reading it "David W. Hodgins" <dwhodgins@nomail.afraid.org> - 2023-02-27 12:27 -0500
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-27 19:19 +0100
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-02-27 21:46 -0500
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-28 13:33 +0100
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-02-28 10:49 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-28 16:21 +0000
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-01 01:05 -0500
Re: Contents of file changed while application is reading it Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2023-03-01 21:57 +0000
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-01 22:53 -0500
Re: Contents of file changed while application is reading it Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2023-03-02 19:06 +0000
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-02 23:08 -0500
Re: Contents of file changed while application is reading it Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2023-03-03 05:23 +0000
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-03 09:36 +0000
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-03-03 11:19 +0100
Re: Contents of file changed while application is reading it Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2023-03-03 15:58 +0000
Re: Contents of file changed while application is reading it "Carlos E. R." <robin_listas@es.invalid> - 2023-03-03 18:47 +0100
Re: Contents of file changed while application is reading it Rich <rich@example.invalid> - 2023-03-04 16:24 +0000
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-03-04 18:40 +0100
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-03 18:44 +0000
Re: Contents of file changed while application is reading it Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2023-03-04 01:25 +0000
Re: Contents of file changed while application is reading it Rich <rich@example.invalid> - 2023-03-04 16:21 +0000
Re: Contents of file changed while application is reading it "28B.I874" <28B.I874@noabzba.net> - 2023-03-04 23:55 -0500
Re: Contents of file changed while application is reading it Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2023-03-05 05:35 +0000
Re: Contents of file changed while application is reading it Allodoxaphobia <trepidation@example.net> - 2023-03-05 13:48 +0000
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-05 14:01 +0000
Re: Contents of file changed while application is reading it Charlie Gibbs <cgibbs@kltpzyxm.invalid> - 2023-03-05 19:16 +0000
Re: Contents of file changed while application is reading it "28B.I874" <28B.I874@noabzba.net> - 2023-03-06 00:43 -0500
Re: Contents of file changed while application is reading it "28B.I874" <28B.I874@noabzba.net> - 2023-03-06 00:42 -0500
Re: Contents of file changed while application is reading it "David W. Hodgins" <dwhodgins@nomail.afraid.org> - 2023-02-28 12:01 -0500
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-01 01:17 -0500
Re: Contents of file changed while application is reading it Robert Riches <spamtrap42@jacob21819.net> - 2023-02-28 04:13 +0000
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-28 13:16 +0100
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-01 01:32 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-01 10:30 +0000
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-01 22:48 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-02 09:41 +0000
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-02 23:39 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-03 09:28 +0000
Re: Contents of file changed while application is reading it "David W. Hodgins" <dwhodgins@nomail.afraid.org> - 2023-03-03 12:16 -0500
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-04 01:36 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-04 07:15 +0000
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-04 02:47 -0500
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-04 01:12 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-04 06:51 +0000
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-03-04 03:32 -0500
Re: Contents of file changed while application is reading it Richard Kettlewell <invalid@invalid.invalid> - 2023-03-04 09:54 +0000
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-04 10:09 +0000
Re: Contents of file changed while application is reading it Richard Kettlewell <invalid@invalid.invalid> - 2023-03-04 11:22 +0000
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-04 11:58 +0000
Re: Contents of file changed while application is reading it Richard Kettlewell <invalid@invalid.invalid> - 2023-03-05 16:43 +0000
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-06 09:20 +0000
Re: Contents of file changed while application is reading it "28B.I874" <28B.I874@noabzba.net> - 2023-03-04 20:24 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-05 13:40 +0000
Re: Contents of file changed while application is reading it Dan Espen <dan1espen@gmail.com> - 2023-03-05 11:29 -0500
Re: Contents of file changed while application is reading it "28B.I874" <28B.I874@noabzba.net> - 2023-03-06 01:21 -0500
Re: Contents of file changed while application is reading it "28B.I874" <28B.I874@noabzba.net> - 2023-03-06 01:06 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-03-06 09:28 +0000
Re: Contents of file changed while application is reading it "28B.I874" <28B.I874@noabzba.net> - 2023-03-06 08:54 -0500
Re: Contents of file changed while application is reading it Dan Espen <dan1espen@gmail.com> - 2023-02-26 17:35 -0500
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-26 23:39 +0000
Re: Contents of file changed while application is reading it "28A.I873" <28A.I873@noabzba.net> - 2023-02-26 22:42 -0500
Re: Contents of file changed while application is reading it "Carlos E.R." <robin_listas@es.invalid> - 2023-02-27 10:45 +0100
Re: Contents of file changed while application is reading it The Natural Philosopher <tnp@invalid.invalid> - 2023-02-26 23:31 +0000
csiph-web