Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #101986

Re: Program to check for valid names

From Richard Heathfield <rjh@cpax.org.uk>
Newsgroups comp.lang.c
Subject Re: Program to check for valid names
Date 2017-01-16 17:18 +0000
Organization Fix this later
Message-ID <o5iv60$7e9$1@dont-email.me> (permalink)
References (9 earlier) <4ae98115-b764-4b96-8929-670781347b7e@googlegroups.com> <o5in9l$90n$1@dont-email.me> <0303faae-cbb2-4dc7-a5e2-67e116628712@googlegroups.com> <o5it31$urk$1@dont-email.me> <13d9eec0-fb8b-4530-99c0-7da1d8ca402a@googlegroups.com>

Show all headers | View raw


On 16/01/17 17:03, Alla _ wrote:
> On Monday, January 16, 2017 at 7:42:52 PM UTC+3, Richard Heathfield wrote:
>> On 16/01/17 16:29, Alla _ wrote:
>>> On Monday, January 16, 2017 at 6:03:56 PM UTC+3, Richard Heathfield wrote:
>>
>> <snip>
>>
>>>> In any case, I'd still avoid moving this pointer along the string.
>>>> Rather, use a temp:
>>>>
>>>> char *token = line;
>>>>
>>>> or
>>>>
>>>> char *token = skipleadingspaces(line);
>>>>
>>>> or whatever. Then you *know* that free(line); will work fine at the end.
>>>>
>>> Done. The first version above, because if I use char *token = skipleadingspaces(line);
>>> I'd still need to "rewind" line back to the beginning,
>>
>> Why?
>>
>> This is a REALLY IMPORTANT QUESTION, and it seems to me that it probably
>> betrays a fundamental misunderstanding on your point of a vital part of
>> C programming.

(I wasn't sure whether my diagnosis was correct, which was why I used 
weasel words like "probably". I am even less sure now that it is true, 
but I'm keeping an open mind, just in case. We shall see...)

>> So: WHY do you think this?
>
> At the beginning of the while loop (if I don't use a temp char *token) my
> line points at the beginning of the line, read from a file. When I call skip_spaces
> function and then assign to line the return value by
> line = skip_spaces(line);
> I get the address of the first character after all beginning spaces.
> For example,
> spacespacespaceline
> after skip_spaces line will point at the address of the character "l";

Yes, but I'm suggesting that you leave line pointing at the original 
memory-allocated space, and use token instead:

token = skip_spaces(line);

so that line remains untouched.

Use token throughout. That way, line never changes, and you can 
free(line) at the end. Do you see that?

> therefore when I call free() at the end (if I have not used any other function
> to move line further), I have to rewind line back to the beginning,

Not if you never moved it in the first place, which is what I'm 
suggesting you do.

To be clear, my suggestion is this:

char *line = malloc(whatever);
if(line != NULL)
{
   while(fgets(line, len, fp) != NULL)
   {
     token = skip_spaces(line);
     do_stuff_with(token);
     token += whatever;
     token = new_value(token); etc etc

     NB line never changes
   } /* end of while loop */

   free(line);
}

Thus, you leave line ALWAYS pointing at the allocated memory, and use 
token to do your skipping along the data.

Do you see why this is easier to keep track of? Do you see that, this 
way, you don't have to remember to reset line, because you never changed 
it in the first place?

(If you do see this, then my previous SHOUTY WARNING was a 
misunderstanding on my part. I will gladly tell you what I was worried 
about if you wish, but it probably doesn't matter now.)

-- 
Richard Heathfield
Email: rjh at cpax dot org dot uk
"Usenet is a strange place" - dmr 29 July 1999
Sig line 4 vacant - apply within

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-14 04:35 -0800
  Re: Program to check for valid names Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 15:02 +0000
    Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-14 07:29 -0800
      Re: Program to check for valid names supercat@casperkitty.com - 2017-01-14 10:17 -0800
      Re: Program to check for valid names Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 18:29 +0000
        Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 02:16 -0800
          Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 11:40 +0000
            Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 03:50 -0800
              Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 04:21 -0800
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 12:56 +0000
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 12:59 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 05:03 -0800
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 13:06 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 06:40 -0800
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 15:03 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 08:29 -0800
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 16:42 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:03 -0800
                Re: Program to check for valid names Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-16 17:17 +0000
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 17:20 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:26 -0800
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 17:18 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:23 -0800
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 17:30 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 06:42 -0800
                Re: Program to check for valid names Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-16 16:23 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 08:46 -0800
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:33 -0800
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-16 17:37 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 09:54 -0800
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-17 03:44 -0800
                Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-17 08:16 -0800
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-18 10:21 -0800
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-18 10:32 -0800
                Re: Program to check for valid names Richard Heathfield <rjh@cpax.org.uk> - 2017-01-18 19:38 +0000
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-18 12:52 -0800
                Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-18 18:52 -0800
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-20 02:00 -0800
                Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-20 12:09 -0800
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-20 02:45 -0800
                Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-20 12:24 -0800
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-21 09:02 -0800
                Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-18 18:05 -0800
                Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-16 10:23 -0800
                Re: Program to check for valid names Alla _ <modelling.data@gmail.com> - 2017-01-16 23:03 -0800
                Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-17 00:19 -0800
  Re: Program to check for valid names Barry Schwarz <schwarzb@dqel.com> - 2017-01-14 18:23 -0800

csiph-web