[comp.lang.c] Should I free strtok

pcb@gator.cacs.usl.edu (Peter C. Bahrs) (04/10/90)

The string functions that return pointers to memory, should their
results be freed?

Most (if not all) of the sample code in QuickC using these do
not free the results.  In fact, most code that I have seen
anywhere, these are not freed.  It only makes sense to free them though.

Any ideas?


/*----------- Thanks in advance... --------------------------------------+
| Peter C. Bahrs                                                         |
| The USL-NASA Project                                                   |
| Center For Advanced Computer Studies      INET: pcb@gator.cacs.usl.edu |
| 2 Rex Street                                                           |
| University of Southwestern Louisiana      ...!uunet!dalsqnt!gator!pcb  | 
| Lafayette, LA 70504                                                    |
+-----------------------------------------------------------------------*/

cjc@ulysses.att.com (Chris Calabrese) (04/10/90)

In article <6501@rouge.usl.edu>, pcb@gator.cacs.usl.edu (Peter C. Bahrs) writes:
> The string functions that return pointers to memory, should their
> results be freed?

These functions don't use the malloc library routines unless they
explicitly say so in the man page.

Strtok, in particular, uses the original string you pass it as its
scratch buffer.  That's why the man page says that it modifies the
original string.

Most of the other string library routines do something similar.
For instance, strcat cats the second string onto the end of the first
string - no internal buffer used here either.
-- 
Name:			Christopher J. Calabrese
Brain loaned to:	AT&T Bell Laboratories, Murray Hill, NJ
att!ulysses!cjc		cjc@ulysses.att.com
Obligatory Quote:	``Anyone who would tell you that would also try and sell you the Brooklyn Bridge.''

bengsig@oracle.nl (Bjorn Engsig) (04/10/90)

Article <6501@rouge.usl.edu> by pcb@gator.cacs.usl.edu (Peter C. Bahrs) says:
|The string functions that return pointers to memory, should their
|results be freed?
All string functions that are part of ANSI C (see K&R 2, appendix B3) do
not allocate any memory, and therefore nothing should be freed (strtok is
among these).

Please note, that strdup is not among these (why isn't it in ANSI C?), and
that you should free the memory returned by it when it is no longer in use.
-- 
Bjorn Engsig,	Domain:		bengsig@oracle.nl, bengsig@oracle.com
		Path:		uunet!mcsun!orcenl!bengsig

henry@utzoo.uucp (Henry Spencer) (04/11/90)

In article <6501@rouge.usl.edu> pcb@gator.cacs.usl.edu (Peter C. Bahrs) writes:
>The string functions that return pointers to memory, should their
>results be freed?

"String" is not a real data type in C; those functions do not return
strings, they return pointers into the strings *you* supplied as arguments.
They do not allocate any storage, and consequently it is an error to apply
free() to their results.
-- 
Apollo @ 8yrs: one small step.|     Henry Spencer at U of Toronto Zoology
Space station @ 8yrs:        .| uunet!attcan!utzoo!henry henry@zoo.toronto.edu

boyd@necisa.ho.necisa.oz (Boyd Roberts) (04/11/90)

In article <6501@rouge.usl.edu> pcb@gator.cacs.usl.edu (Peter C. Bahrs) writes:
>The string [strtok] functions that return pointers to memory, should their
>results be freed?
>

No.  The string(3) functions that return pointers, return pointer values
to data that you have supplied as an argument.  Freeing their return
values will result in catastrophe.

More generally, never free returned memory that is static in nature.
Only free things that are explicitly malloced.  Read the manual.

Another thing to watch out for is a routine that has a partner that may
deallocate the object returned.  Eg:

    p = get_me_a_widgit();

    /* ... use *p */

    finished_with_widget(p);

The manual entry for the routine should be explicit enough to
state where it gets its storage from.  Most C library routines are fairly
simple and either use static data or operate on stuff passed as an argument.
The stuff you pass you should know when to free.



Boyd Roberts			boyd@necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''