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


Groups > comp.lang.c > #101796

Re: strtok() and the mistery of the same address strings with different lengths...

From Ben Bacarisse <ben.usenet@bsb.me.uk>
Newsgroups comp.lang.c
Subject Re: strtok() and the mistery of the same address strings with different lengths...
Date 2017-01-14 19:51 +0000
Organization A noiseless patient Spider
Message-ID <87mvetl8s0.fsf@bsb.me.uk> (permalink)
References <94292e94-bcb9-40ea-8b8a-2a8563eeb33e@googlegroups.com> <6e096b3f-1e35-49ae-8887-637f94fc6d51@googlegroups.com> <0151fc5f-a2eb-4ff3-b520-a6a0e424b6bc@googlegroups.com> <877f5xn06f.fsf@bsb.me.uk> <eb3a5bf1-6672-4c3f-a853-48fb0a67b3a3@googlegroups.com>

Show all headers | View raw


"Fernando D. Bozzo" <fdbozzo@gmail.com> writes:
<snip>
> My implementation corrupts the malloc'ed segment, this it what I've
> did:

Yes, it does.

<snip>
> char *strduplicate(const char *string)
> {
>     int len = strlen(string);

I'd use size_t len here.

>     printf("len = %d\n", len);

and then I'd need %zu as the format.

>     char *newstr = malloc((len + 1) * sizeof(char));

sizeof (char) is guaranteed to be 1.  It looks a little odd to put it
there but it's not an error.

>     printf("newstr len = %lu, addr = %p\n", sizeof(newstr), newstr);

sizeof newstr is not the length of newstr so the message you print is
potentially confusing.

>     char *buff = &newstr[0];

Or just char *buff = newstr;

>     printf("buff len = %lu, addr = %p\n", sizeof(buff), buff);

See above.

>     while (*string != '\0')
>     {
>         printf("string = %c, addr = %p   ", *string, string);
>         *buff++ = *string++;
>         printf("buff = %c, addr = %p\n", *buff, buff);

Here you print the value in a location you have not set.  It won't tell
you anything interesting (and valgrind will complain about it).

>     }
>     printf("string = %c, addr = %p   ", *string, string);
>     buff[len] = '\0';
>     printf("buff = %c, addr = %p\n", *buff, buff);
>     return newstr;
> }

Here's the body simplified.  Can you see the problem now?

     size_t len = strlen(string);
     char *newstr = malloc(len + 1);
     char *buff = &newstr[0];
     while (*string != '\0')
         *buff++ = *string++;
     buff[len] = '\0';
     return newstr;

Hint...
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
You've moved buff as you copy, so buf[len] is way too far away for the
null byte to go!

<snip>
-- 
Ben.

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


Thread

strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 16:16 -0800
  Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 16:30 -0800
  Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 00:44 +0000
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 17:43 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... Manfred <noname@invalid.add> - 2017-01-14 16:52 +0100
      Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 19:38 +0000
  Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-13 16:49 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-13 17:39 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-13 22:00 -0500
        Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 03:11 -0800
          Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-14 10:50 -0500
            Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:27 -0800
              Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:21 -0800
              Re: strtok() and the mistery of the same address strings with different lengths... Les Cargill <lcargill99@comcast.com> - 2017-01-15 11:48 -0600
                Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-15 15:24 -0800
                Re: strtok() and the mistery of the same address strings with different lengths... Les Cargill <lcargill99@comcast.com> - 2017-01-15 22:11 -0600
      Re: strtok() and the mistery of the same address strings with different lengths... Manfred <noname@invalid.add> - 2017-01-14 16:48 +0100
  Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 03:18 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 04:16 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 15:14 +0000
        Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 09:58 -0800
          Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 19:51 +0000
            Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:18 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:17 -0800
    Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 15:09 +0000
      Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 11:09 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... luser droog <luser.droog@gmail.com> - 2017-01-14 11:41 -0800
        Re: strtok() and the mistery of the same address strings with different lengths... Ben Bacarisse <ben.usenet@bsb.me.uk> - 2017-01-14 20:04 +0000
        Re: strtok() and the mistery of the same address strings with different lengths... James Kuyper <jameskuyper@verizon.net> - 2017-01-14 15:09 -0500
          Re: strtok() and the mistery of the same address strings with different lengths... "Fernando D. Bozzo" <fdbozzo@gmail.com> - 2017-01-14 15:22 -0800
      Re: strtok() and the mistery of the same address strings with different lengths... Keith Thompson <kst-u@mib.org> - 2017-01-14 14:21 -0800

csiph-web