[comp.windows.ms] Using strtok

kipnis@janus.Berkeley.EDU (Gary Kipnis) (06/06/90)

Hi, I need to use strtok() function in my program.  I know that this 
function allocates its own buffer probably using malloc(), which makes this
function rather unusable under MS-Windows environment.  Am I correct in the 
above assumptions and do I need to write my own version of strtok() or
can I safely use strtok() in my program?

Thank You,

gary

michaelt@microsoft.UUCP (Michael THURLKILL) (06/12/90)

In article <36818@ucbvax.BERKELEY.EDU> kipnis@janus.Berkeley.EDU (Gary Kipnis) writes:
>
>Hi, I need to use strtok() function in my program.  I know that this 
>function allocates its own buffer probably using malloc(), which makes this
>function rather unusable under MS-Windows environment.  Am I correct in the 
>above assumptions and do I need to write my own version of strtok() or
>can I safely use strtok() in my program?
>
It is generally ok to use strtok, and other C runtime functions,
under Windows. Malloc is replaced by the SDK setup program to make
a call to LocalAlloc. There are things to remember though. The
C runtime functions are passed near pointers, so you can only 
operate on things that are in dgroup, ie. in DS or on the stack,
ie. global variable and local variables and LocalAlloced memory.
It won't work for data contained in a GlobalAlloced block, nor
for data accessed via a far pointer returned by Windows or other
DLLs. Also, the C runtime is not compiled with the -Gw switch,
so in tight memory conditions, functions that call malloc may
die. Also, if you are calling the function from a DLL, you must
be aware of the DS != SS issue. Basically, you can only use
these functions with pointers to global variables and 
LocalAlloced data. Local variables are on the stack, and the 
DLL is running on the applications stack, so a near pointer
won't be able to access the data. 

The Win3 SDK will provide libraries that are compiled with -Gw.
They don't get rid of the DS!=SS problem though.

Mike Thurlkill

Disclaimer: These are my opinions. They should in no way be 
miscontrued as being correct or in any way related to my employer.

>gary