For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > August 2006 > strtok buffer question









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author strtok buffer question
soccertl

2006-08-25, 7:00 pm

What exactly does strtok store in it's static buffer? I am curious
since I read somewhere that you should finish out the loop until you
get NULL before going on to the next strtok. Is this really necessary
if I don't care about the first token string anymore once I find what I
want?

Paul Pluzhnikov

2006-08-25, 7:00 pm

"soccertl" <lambert54@cox.net> writes:

> What exactly does strtok store in it's static buffer?


Pointer to the start of the next string.

It might help you to either read actual implementation in glibc
source, or in solaris source here:
http://cvs.opensolaris.org/source/x...rt/gen/strtok.c

> I read somewhere that you should finish out the loop until you
> get NULL before going on to the next strtok. Is this really necessary
> if I don't care about the first token string anymore once I find what I
> want?


No.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
Paul Pluzhnikov

2006-08-25, 7:00 pm

Paul Pluzhnikov <ppluzhnikov-nsp@charter.net> writes:

> It might help you to either read actual implementation in glibc
> source, or in solaris source here:
> http://cvs.opensolaris.org/source/x...rt/gen/strtok.c


[Oops. I didn't finish the sentence.]
or write your own implementation so you'll understand how it works.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
soccertl

2006-08-25, 7:00 pm

>From looking at the source code it looks like tsdalloc allocates memory
with lmalloc but it is not clear when this memory gets freed. This
makes me worried that the caller is suppose to do something to free
this memory but I have not seen that mentioned anywhere.

I wasn't aware that strtok actually allocated memory.

soccertl

2006-08-25, 7:00 pm


soccertl wrote:
> with lmalloc but it is not clear when this memory gets freed. This
> makes me worried that the caller is suppose to do something to free
> this memory but I have not seen that mentioned anywhere.
>
> I wasn't aware that strtok actually allocated memory.


Well, I found a different strtok in the open source link that I think
works more like I expected. It doesn't allocate memory and just saves
the pointer in a static variable. That makes more sense and I see how
it works now.

Paul Pluzhnikov

2006-08-26, 3:59 am

"soccertl" <lambert54@cox.net> writes:

> From looking at the source code it looks like tsdalloc allocates memory
> with lmalloc but it is not clear when this memory gets freed.


It never gets free()d; it just stays in thread-local array forever.

> This
> makes me worried that the caller is suppose to do something to free
> this memory but I have not seen that mentioned anywhere.


No, the caller isn't supposed to do anything ...
The reason for this memory is to make strtok() thread-safe.

Cheers,
--
In order to understand recursion you must first understand recursion.
Remove /-nsp/ for email.
soccertl

2006-08-28, 7:00 pm

Thanks for the help Paul and thanks for the link. Very helpful.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com