Bifalgorithm post · Computer science one · Tokenizing

Why is strtok() not reentrant, and what alternative does C provide?

Why is strtok() not reentrant, and what alternative does C provide? (Computer science one · Tokenizing) — "It should be noted that strtok() is not reentrant. I…

Ideas in this flowchart

  • strtok() n’est pas réentrante et son utilisation nécessite des précautions.
  • strtok() ne peut traiter qu’une seule chaîne à la fois.
  • Dans une application multithread, deux threads ne doivent pas utiliser strtok() simultanément.
  • Sinon, les deux threads risquent de traiter les chaînes l’un de l’autre.
  • Même dans une application sans threads, il faut rester prudent.
  • Il ne faut pas traiter une chaîne avec strtok() dans une fonction, puis appeler une autre fonction qui l’utilise également.
  • Cette situation peut interrompre ou perturber le traitement de la première chaîne.
  • La fonction réentrante strtok_r() peut être utilisée à la place de strtok().
Feed/

Why is strtok() not reentrant, and what alternative does C provide?

No cover

Channel

Computer science one

1 page

"It should be noted that strtok() is not reentrant. It can only work on one string at a time. In a multithreaded application, two threads cannot both use strtok() or they would end up processing each other’s strings. Even in a non-threaded application, we need to be careful. For example, we cannot process a string using strtok() in one function and then call another function that does the same. C does provide a reentrant version, strtok_r() that can be used."

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.