Subj : Re: String memory Allocation To : borland.public.cpp.borlandcpp From : maeder Date : Sun Oct 03 2004 06:20 pm "M. Finch" writes: > char input[16] = "abc,d"; > char *p; > > p = strtok(input, ","); > > My question is how or where is memory allocated for the retuned string? The function allocates the appropriate amount of memory. It is unspecified how it does this. > If it is allocated in the function, how does one know that? By reading the documentation. > After a bazillion calls to this function can it actually burn up all of > the memory available? Unlikely. The ISO C and C++ Standards allow one call to strtok() to reuse the memory used for the previous call and to write over the string pointed to by the first argument. I'm confident that most or all implementations do this. This also means that you must be very careful when using this function in multi-threaded programs. Re-entering it has undefined behavior. Even in single-threaded programs, it's very easy to make errors, e.g. to store strtok()'s return value and access it later, after another call to strtok(). The man page on my Linux box says (under "BUGS"): "Never use this function." While this is exaggerated, it's certainly a very good idea not to use it in new code. > How do you release this memory if it is being used? You don't. .