ts-7000
[Top] [All Lists]

Re: [ts-7000] malloc()

To:
Subject: Re: [ts-7000] malloc()
From: Joe Bouchard <>
Date: Thu, 19 Oct 2006 21:07:27 -0400
> Now, a co-worker has raised a question about if this is a result of me 
> misusing strtok_r(), I think I'm using it correctly but not sure.

Yeah, I can see a couple of issues.  I'm surprised you aren't getting segfaults.

When using both strtok and strtok_r it's a 2 step process.  Typically the first
call is before the loop, and subsequant calls are inside a loop.
char * p = strtok(char * s, char * delimiters);
       p = strtok(NULL, delimiters);
In the first call you give it the real "s", and after that, you give it NULL.
No it doesn't make sense. I didn't write it, but that's how it works.  And
remember that it destroys your original string, so if you need to keep it for
another use, you need to make a copy (which you were doing).

The thread safe version is strtok_r, and it's similar, except you need to
allocate another buffer, and pass that as the last parameter.

Here is a working example:

int main(void)
{
  char msg[] = "SEE:SPOT:RUN";
  char msg2[]= "RUN:SPOT:RUN";
  char * place_keeper = (char*)malloc(1024);

  // strtok uses only the original string and a spare pointer.
  // it looks like this the first time with strtok
  char * p = (char *) strtok(msg, ":");
  if (p)
    printf("Command=%s\n",p);

  while(p)
    {
      // it passes NULL on subsequent calls
      p = (char*)strtok(NULL, ":");
      if (p)
        printf("Command=%s\n",p);
    }

  printf("------------------------------------------\n");

  // strtok_r uses the original string,
  // and a spare pointer, and another buffer
  // it looks like this the first time with strtok_r
  p = (char*) strtok_r(msg2, ":", place_keeper);
  if (p)
    printf("Command=%s\n",p);

  while(p)
    {
      // it passes nulls on subsequant calls.
      p = (char*)strtok_r(NULL, ":", place_keeper);
      if (p)
        printf("Command=%s\n",p);
    }

  free(place_keeper);
  return 0;
}

Here is the result.
Command=SEE
Command=SPOT
Command=RUN
------------------------------------------
Command=RUN
Command=SPOT
Command=RUN


 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
    http://groups.yahoo.com/group/ts-7000/

<*> Your email settings:
    Individual Email | Traditional

<*> To change settings online go to:
    http://groups.yahoo.com/group/ts-7000/join
    (Yahoo! ID required)

<*> To change settings via email:
     
    

<*> To unsubscribe from this group, send an email to:
    

<*> Your use of Yahoo! Groups is subject to:
    http://docs.yahoo.com/info/terms/
 

<Prev in Thread] Current Thread [Next in Thread>
Admin

Disclaimer: Neither Andrew Taylor nor the University of NSW School of Computer and Engineering take any responsibility for the contents of this archive. It is purely a compilation of material sent by many people to the birding-aus mailing list. It has not been checked for accuracy nor its content verified in any way. If you wish to get material removed from the archive or have other queries about the archive e-mail Andrew Taylor at this address: andrewt@cse.unsw.EDU.AU