Sujet : Re: Buffer contents well-defined after fgets() reaches EOF ?
De : jameskuyper (at) *nospam* alumni.caltech.edu (James Kuyper)
Groupes : comp.lang.cDate : 16. Feb 2025, 07:04:11
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vorv4r$emcm$2@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13
User-Agent : Mozilla Thunderbird
On 2/15/25 22:29, Janis Papanagnou wrote:
On 15.02.2025 18:29, Michael S wrote:
On Fri, 14 Feb 2025 20:51:38 +0100
Janis Papanagnou <janis_papanagnou+ng@hotmail.com> wrote:
>
Actually, in the same code, I'm also using the strtok() function
>
strtok() is one of the relatively small set of more problemetic
functions in C library that are not thread-safe.
>
I know that it's not thread-safe. (You can't miss that information
if you look up the man page to inspect the function interface.)
>
If you only care about POSIX target, the I'd reccomend to avoid strtok
and to use strtok_r().
If you cannot assume POSIX, but can assume C2011 or later, you might be
able to use strtok_s() instead. You need to add
#ifdef __STDC_LIB_EXT1__
#define __STDC_WANT_LIB_EXT1__ 1
// strtok_s() will be declared in <string.h>
#endif
#include <string.h>
But since I don't use threads - neither here nor did I ever needed
them generally in my "C" contexts - that's unnecessary. Isn't it?
No. What makes strtok() problematic can come up without any use of
threads. Consider for the moment a bug I had to investigate. A function
that was looping through strtok() calls to parse a string called a
utility function during each pass through the loop. The utility function
also called strtok() in a loop to parse an entirely different string for
a different purpose. Exercise for the student: figure out what the
consequences were.