Why does this combination of strtol and strtok not work?

795 views Asked by At

Could anyone tell me what is wrong with this code?

for(int i=0;i<4;i++)
{
    long int a = strtol(strtok("1-2-3-4","-"),(char**)NULL,10);
    cout << a <<endl
}

I'm running on Solaris Unix. It's giving me a segmentation fault.

The fault is in strtol().

3

There are 3 answers

0
Jonathan Leffler On

The problems are legion.

I would expect that the core dump is because the string "1-2-3-4" is stored in read-only memory, so when strtok() modifies it (to isolate the first token), the program crashes. You say the crash is in strtol(); that would suggest that the return value from strtok() is NULL.

The first call to strtok() uses the string as an argument; the second call passes NULL in its place to indicate 'continue where you left off last time'. As written, if the string was modifiable, then you'd parse 1 four times.

This is closer to correct (though untested):

char  input[] = "1-2-3-4";
char *data = input;
for (int i = 0; i < 4; i++)
{
    char *token = strtok(data, "-");
    if (token != 0)
    {
        long int a = strtol(token, NULL, 10);
        cout << a << endl;
    }
    data = NULL;
}

In general, you need to do error detection from strtol(); further, doing so is quite fraught. However, with the sample string, you would not have to worry about that.

0
Carl Norum On

The error is with the strtok call, not with strtol. You can't call strtok on a string literal, since it's going to try to modify the string. Modifying a string literal causes undefined behaviour in C++.

0
COD3BOY On

As the problem is already discussed, I'd like to show an alternate approach :

#include <stdio.h>
#include <string.h>

    int main ()
    {
      long int a;
      char str[] ="1-2-3-4";
      char * pch;

      pch = strtok (str,"-");
      while (pch != NULL)
      {
         a = strtol(pch,(char**)NULL,10);
         cout << a <<endl;

        pch = strtok (NULL, "-");
      }
      return 0;
     }