strTok function (thread safe, supports empty tokens, doesn't change string) The 2019 Stack Overflow Developer Survey Results Are InString case reverse function in CGet line from string functionTDD: String Calculator KataC - K&R getint() variationSimple function to generate an HTML-safe stringGeneric Pairing Heap PerformancePattern for writing a generic string transformation functionChange a string into a function/def activatorC++ string tokenizing without streams, with certain conditionsRead consecutive blanks in array

How to translate "being like"?

Identify boardgame from Big movie

What information about me do stores get via my credit card?

What's the name of these plastic connectors

Kerning for subscripts of sigma?

Why don't hard Brexiteers insist on a hard border to prevent illegal immigration after Brexit?

How to obtain a position of last non-zero element

What to do when moving next to a bird sanctuary with a loosely-domesticated cat?

Correct punctuation for showing a character's confusion

Dropping list elements from nested list after evaluation

How to display lines in a file like ls displays files in a directory?

Straighten subgroup lattice

Falsification in Math vs Science

Is it ok to offer lower paid work as a trial period before negotiating for a full-time job?

Geography at the pixel level

Why does the nucleus not repel itself?

Did Scotland spend $250,000 for the slogan "Welcome to Scotland"?

What is this sharp, curved notch on my knife for?

How much of the clove should I use when using big garlic heads?

What does もの mean in this sentence?

How to notate time signature switching consistently every measure

Deal with toxic manager when you can't quit

What is the meaning of Triage in Cybersec world?

Why are there uneven bright areas in this photo of black hole?



strTok function (thread safe, supports empty tokens, doesn't change string)



The 2019 Stack Overflow Developer Survey Results Are InString case reverse function in CGet line from string functionTDD: String Calculator KataC - K&R getint() variationSimple function to generate an HTML-safe stringGeneric Pairing Heap PerformancePattern for writing a generic string transformation functionChange a string into a function/def activatorC++ string tokenizing without streams, with certain conditionsRead consecutive blanks in array



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








4












$begingroup$


I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string



char* strTok(char** newString, char* delimiter)

char* string = *newString;
char* delimiterFound = (char*) 0;
int tokLenght = 0;
char* tok = (char*) 0;

if(!string) return (char*) 0;

delimiterFound = strstr(string, delimiter);

if(delimiterFound)
tokLenght = delimiterFound-string;
else
tokLenght = strlen(string);


tok = malloc(tokLenght + 1);
memcpy(tok, string, tokLenght);
tok[tokLenght] = '';

*newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

return tok;



I designed it to be used like



char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &input;
char* tok;
while( (tok=strTok(inputP, ",")) )
printf("%sn", tok);










share|improve this question









$endgroup$







  • 1




    $begingroup$
    Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
    $endgroup$
    – Neil Edelman
    Apr 6 at 1:59










  • $begingroup$
    @NeilEdelman thanks I never saw this function before, I will check it.
    $endgroup$
    – Accountant م
    Apr 6 at 2:09






  • 1




    $begingroup$
    It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:17

















4












$begingroup$


I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string



char* strTok(char** newString, char* delimiter)

char* string = *newString;
char* delimiterFound = (char*) 0;
int tokLenght = 0;
char* tok = (char*) 0;

if(!string) return (char*) 0;

delimiterFound = strstr(string, delimiter);

if(delimiterFound)
tokLenght = delimiterFound-string;
else
tokLenght = strlen(string);


tok = malloc(tokLenght + 1);
memcpy(tok, string, tokLenght);
tok[tokLenght] = '';

*newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

return tok;



I designed it to be used like



char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &input;
char* tok;
while( (tok=strTok(inputP, ",")) )
printf("%sn", tok);










share|improve this question









$endgroup$







  • 1




    $begingroup$
    Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
    $endgroup$
    – Neil Edelman
    Apr 6 at 1:59










  • $begingroup$
    @NeilEdelman thanks I never saw this function before, I will check it.
    $endgroup$
    – Accountant م
    Apr 6 at 2:09






  • 1




    $begingroup$
    It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:17













4












4








4





$begingroup$


I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string



char* strTok(char** newString, char* delimiter)

char* string = *newString;
char* delimiterFound = (char*) 0;
int tokLenght = 0;
char* tok = (char*) 0;

if(!string) return (char*) 0;

delimiterFound = strstr(string, delimiter);

if(delimiterFound)
tokLenght = delimiterFound-string;
else
tokLenght = strlen(string);


tok = malloc(tokLenght + 1);
memcpy(tok, string, tokLenght);
tok[tokLenght] = '';

*newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

return tok;



I designed it to be used like



char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &input;
char* tok;
while( (tok=strTok(inputP, ",")) )
printf("%sn", tok);










share|improve this question









$endgroup$




I'm new to C language and want to explode a string like we do in PHP explode() function, I searched for a built-in function with the C standard library, and I found strtok , but It doesn't support empty tokens like 1,2,3,,5 . Inspired by the answers I found in this SO question I made this function, it is supposed to be thread safe and support empty tokens and doesn't change the original string



char* strTok(char** newString, char* delimiter)

char* string = *newString;
char* delimiterFound = (char*) 0;
int tokLenght = 0;
char* tok = (char*) 0;

if(!string) return (char*) 0;

delimiterFound = strstr(string, delimiter);

if(delimiterFound)
tokLenght = delimiterFound-string;
else
tokLenght = strlen(string);


tok = malloc(tokLenght + 1);
memcpy(tok, string, tokLenght);
tok[tokLenght] = '';

*newString = delimiterFound ? delimiterFound + strlen(delimiter) : (char*)0;

return tok;



I designed it to be used like



char* input = "1,2,3,4,5,6,7,,,10,";
char** inputP = &input;
char* tok;
while( (tok=strTok(inputP, ",")) )
printf("%sn", tok);







beginner c strings






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Apr 6 at 0:03









Accountant مAccountant م

24119




24119







  • 1




    $begingroup$
    Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
    $endgroup$
    – Neil Edelman
    Apr 6 at 1:59










  • $begingroup$
    @NeilEdelman thanks I never saw this function before, I will check it.
    $endgroup$
    – Accountant م
    Apr 6 at 2:09






  • 1




    $begingroup$
    It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:17












  • 1




    $begingroup$
    Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
    $endgroup$
    – Neil Edelman
    Apr 6 at 1:59










  • $begingroup$
    @NeilEdelman thanks I never saw this function before, I will check it.
    $endgroup$
    – Accountant م
    Apr 6 at 2:09






  • 1




    $begingroup$
    It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:17







1




1




$begingroup$
Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
$endgroup$
– Neil Edelman
Apr 6 at 1:59




$begingroup$
Better user-interface then the original strtok. You may be interested in strsep, too. code.woboq.org/userspace/glibc/string/strsep.c.html
$endgroup$
– Neil Edelman
Apr 6 at 1:59












$begingroup$
@NeilEdelman thanks I never saw this function before, I will check it.
$endgroup$
– Accountant م
Apr 6 at 2:09




$begingroup$
@NeilEdelman thanks I never saw this function before, I will check it.
$endgroup$
– Accountant م
Apr 6 at 2:09




1




1




$begingroup$
It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
$endgroup$
– Neil Edelman
Apr 6 at 2:17




$begingroup$
It's not in the standard C libraries, but in POSIX, (any type of gcc.) However, like strtok, it obliterates the char to replace it with , so it's not the same.
$endgroup$
– Neil Edelman
Apr 6 at 2:17










2 Answers
2






active

oldest

votes


















3












$begingroup$

  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....







share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    Apr 6 at 1:41










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    Apr 6 at 1:52










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    Apr 6 at 2:09










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    Apr 6 at 2:14


















2












$begingroup$

From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.






share|improve this answer









$endgroup$












  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    Apr 6 at 1:42







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم For the spelling, you used G-H-T when the correct spelling is G-T-H (the last two letters are swapped). I've made that typo before. I find having identifiers spelled correctly helps with reading and finding them, although the autocomplete in IDEs mitigates that a little but propagates the misspellings.
    $endgroup$
    – 1201ProgramAlarm
    Apr 6 at 14:12










  • $begingroup$
    @1201ProgramAlarm ooh, 😮 how come I didn't notice this after revising it multiple times!, thanks.
    $endgroup$
    – Accountant م
    Apr 6 at 14:38











Your Answer





StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216956%2fstrtok-function-thread-safe-supports-empty-tokens-doesnt-change-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









3












$begingroup$

  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....







share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    Apr 6 at 1:41










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    Apr 6 at 1:52










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    Apr 6 at 2:09










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    Apr 6 at 2:14















3












$begingroup$

  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....







share|improve this answer









$endgroup$








  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    Apr 6 at 1:41










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    Apr 6 at 1:52










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    Apr 6 at 2:09










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    Apr 6 at 2:14













3












3








3





$begingroup$

  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....







share|improve this answer









$endgroup$



  • delimiterFound + strlen(delimiter) sounds like a bug. If the delimiter is longer than one character, *newString will point too far into the original, maybe even beyond the end. Correct me if I am wrong, delimiterFound + 1 is what you are actually after.



  • Modern C allows, and strongly encourages, to declare variables as close to their use a possible. Consider



    char * delimiterFound = strstr(string, delimiter);
    ....
    char * tok = malloc(tokLenght + 1);


    etc.



  • Always test that malloc didn't fail.



  • More spaces - around keywords, braces, etc - definitely improve readability:



     if (....) 
    ....
    else
    ....








share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 6 at 1:24









vnpvnp

40.7k234103




40.7k234103







  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    Apr 6 at 1:41










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    Apr 6 at 1:52










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    Apr 6 at 2:09










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    Apr 6 at 2:14












  • 1




    $begingroup$
    Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
    $endgroup$
    – Accountant م
    Apr 6 at 1:41










  • $begingroup$
    @Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
    $endgroup$
    – vnp
    Apr 6 at 1:52










  • $begingroup$
    I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
    $endgroup$
    – Accountant م
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
    $endgroup$
    – vnp
    Apr 6 at 2:09










  • $begingroup$
    It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
    $endgroup$
    – Accountant م
    Apr 6 at 2:14







1




1




$begingroup$
Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
$endgroup$
– Accountant م
Apr 6 at 1:41




$begingroup$
Thaaank you very much for these precious points, regarding the delimiter length bug, ummmm, I want to support long delimiters more than 1 characters like the boundary string in http requests that has content-type multi-part, and I don't think it's a bug because delimiterFound + strlen(delimiter) can never be after the 0 byte that terminates the original string!, right ? "fooDELIMITER" 3 + 12
$endgroup$
– Accountant م
Apr 6 at 1:41












$begingroup$
@Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
$endgroup$
– vnp
Apr 6 at 1:52




$begingroup$
@Accountantم Long delimiters here refer to, say ",;.", in where any character delimits the string on its own right.
$endgroup$
– vnp
Apr 6 at 1:52












$begingroup$
I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
$endgroup$
– Accountant م
Apr 6 at 2:03




$begingroup$
I didn't get it, I'm sorry, can you please give me an example input that can break this code, exploiting this bug ?
$endgroup$
– Accountant م
Apr 6 at 2:03




1




1




$begingroup$
@Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
$endgroup$
– vnp
Apr 6 at 2:09




$begingroup$
@Accountantم Sorry for not being clear. I should realize that your intentions are different (and read man strstr more carefully). Consider it my blinder - since you mentioned strtok, I expected the strtok semantics.
$endgroup$
– vnp
Apr 6 at 2:09












$begingroup$
It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
$endgroup$
– Accountant م
Apr 6 at 2:14




$begingroup$
It's my fault because I said it's strtok, I wanted strtok that can support delimiters more than 1 characters, because I need this feature a lot. So do you mean it's not a bug ??
$endgroup$
– Accountant م
Apr 6 at 2:14













2












$begingroup$

From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.






share|improve this answer









$endgroup$












  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    Apr 6 at 1:42







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم For the spelling, you used G-H-T when the correct spelling is G-T-H (the last two letters are swapped). I've made that typo before. I find having identifiers spelled correctly helps with reading and finding them, although the autocomplete in IDEs mitigates that a little but propagates the misspellings.
    $endgroup$
    – 1201ProgramAlarm
    Apr 6 at 14:12










  • $begingroup$
    @1201ProgramAlarm ooh, 😮 how come I didn't notice this after revising it multiple times!, thanks.
    $endgroup$
    – Accountant م
    Apr 6 at 14:38















2












$begingroup$

From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.






share|improve this answer









$endgroup$












  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    Apr 6 at 1:42







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم For the spelling, you used G-H-T when the correct spelling is G-T-H (the last two letters are swapped). I've made that typo before. I find having identifiers spelled correctly helps with reading and finding them, although the autocomplete in IDEs mitigates that a little but propagates the misspellings.
    $endgroup$
    – 1201ProgramAlarm
    Apr 6 at 14:12










  • $begingroup$
    @1201ProgramAlarm ooh, 😮 how come I didn't notice this after revising it multiple times!, thanks.
    $endgroup$
    – Accountant م
    Apr 6 at 14:38













2












2








2





$begingroup$

From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.






share|improve this answer









$endgroup$



From a readability viewpoint, you should use NULL instead of (char*) 0 as it is easier to recognize what you're trying to do. Also, the tokLenght misspells "length", and should probably be tokLength.



You leak memory, as the memory allocated to hold the returned string is never freed.







share|improve this answer












share|improve this answer



share|improve this answer










answered Apr 6 at 0:57









1201ProgramAlarm1201ProgramAlarm

3,7232925




3,7232925











  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    Apr 6 at 1:42







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم For the spelling, you used G-H-T when the correct spelling is G-T-H (the last two letters are swapped). I've made that typo before. I find having identifiers spelled correctly helps with reading and finding them, although the autocomplete in IDEs mitigates that a little but propagates the misspellings.
    $endgroup$
    – 1201ProgramAlarm
    Apr 6 at 14:12










  • $begingroup$
    @1201ProgramAlarm ooh, 😮 how come I didn't notice this after revising it multiple times!, thanks.
    $endgroup$
    – Accountant م
    Apr 6 at 14:38
















  • $begingroup$
    Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
    $endgroup$
    – Accountant م
    Apr 6 at 1:42







  • 1




    $begingroup$
    It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
    $endgroup$
    – Neil Edelman
    Apr 6 at 2:03






  • 1




    $begingroup$
    @Accountantم For the spelling, you used G-H-T when the correct spelling is G-T-H (the last two letters are swapped). I've made that typo before. I find having identifiers spelled correctly helps with reading and finding them, although the autocomplete in IDEs mitigates that a little but propagates the misspellings.
    $endgroup$
    – 1201ProgramAlarm
    Apr 6 at 14:12










  • $begingroup$
    @1201ProgramAlarm ooh, 😮 how come I didn't notice this after revising it multiple times!, thanks.
    $endgroup$
    – Accountant م
    Apr 6 at 14:38















$begingroup$
Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
$endgroup$
– Accountant م
Apr 6 at 1:42





$begingroup$
Thank you very much I will use NULL from now on, and I will remember to free() memory , 'I miss PHP garbage collector :(', I didn't get the tokLength spelling note, aren't they the same ?
$endgroup$
– Accountant م
Apr 6 at 1:42





1




1




$begingroup$
It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
$endgroup$
– Neil Edelman
Apr 6 at 2:03




$begingroup$
It's a matter of style, and including .h if you want to use NULL. However, you don't have to cast (char *)0, just use 0 (or NULL.) It knows from the return type.
$endgroup$
– Neil Edelman
Apr 6 at 2:03




1




1




$begingroup$
@Accountantم For the spelling, you used G-H-T when the correct spelling is G-T-H (the last two letters are swapped). I've made that typo before. I find having identifiers spelled correctly helps with reading and finding them, although the autocomplete in IDEs mitigates that a little but propagates the misspellings.
$endgroup$
– 1201ProgramAlarm
Apr 6 at 14:12




$begingroup$
@Accountantم For the spelling, you used G-H-T when the correct spelling is G-T-H (the last two letters are swapped). I've made that typo before. I find having identifiers spelled correctly helps with reading and finding them, although the autocomplete in IDEs mitigates that a little but propagates the misspellings.
$endgroup$
– 1201ProgramAlarm
Apr 6 at 14:12












$begingroup$
@1201ProgramAlarm ooh, 😮 how come I didn't notice this after revising it multiple times!, thanks.
$endgroup$
– Accountant م
Apr 6 at 14:38




$begingroup$
@1201ProgramAlarm ooh, 😮 how come I didn't notice this after revising it multiple times!, thanks.
$endgroup$
– Accountant م
Apr 6 at 14:38

















draft saved

draft discarded
















































Thanks for contributing an answer to Code Review Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f216956%2fstrtok-function-thread-safe-supports-empty-tokens-doesnt-change-string%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

រឿង រ៉ូមេអូ និង ហ្ស៊ុយលីយេ សង្ខេបរឿង តួអង្គ បញ្ជីណែនាំ

QGIS export composer to PDF scale the map [closed] Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Announcing the arrival of Valued Associate #679: Cesar Manara Unicorn Meta Zoo #1: Why another podcast?Print Composer QGIS 2.6, how to export image?QGIS 2.8.1 print composer won't export all OpenCycleMap base layer tilesSave Print/Map QGIS composer view as PNG/PDF using Python (without changing anything in visible layout)?Export QGIS Print Composer PDF with searchable text labelsQGIS Print Composer does not change from landscape to portrait orientation?How can I avoid map size and scale changes in print composer?Fuzzy PDF export in QGIS running on macSierra OSExport the legend into its 100% size using Print ComposerScale-dependent rendering in QGIS PDF output

PDF-ში გადმოწერა სანავიგაციო მენიუproject page