c++visual-c++strtok

Splitting a string with strtok_s


I am trying to split a string by a specified delimiter according to this example: http://msdn.microsoft.com/en-us/library/ftsafwz3(v=VS.90).aspx

My code compiles without errors in Visual C++ 2010, but when I want to run it, I get this error message:

Unhandled exception at 0x773a15de in Test.exe: 0xC0000005: Access violation reading location 0x00000000.

Here's my code:

#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <regex>

using namespace std;

vector<char *> splitString(char in[])
{
vector<char *> parts(15);
char seps[]   = " ,\t\n";
char *next_token1 = NULL;
char *token1 = NULL;
token1 = strtok_s(in, seps, &next_token1);
while ((token1 != NULL))
{
    if (token1 != NULL)
    {
        token1 = strtok_s( NULL, seps, &next_token1);
                    //printf( " %s\n", token1 );
        parts.push_back(token1);
    }
}
return parts;
}

int main(int argc, char * argv[])
{
char string1[] =
    "A string\tof ,,tokens\nand some  more tokens";
vector<char *> parts=splitString(string1);
cout << parts[0] <<endl;
cout << parts[1] <<endl;
return 0;
}

It seems to be illegal that I try to display the vector's elements, but why?

The vector's capacity should be sufficient and a

printf( " %s\n", token1 );

in the while loop prints out the tokens!


Solution

  • The use of vector is incorrect. It is constructed with 15 elements and then push_back() is used to add the strings which adds new elements after the initial 15. This means the first 15 elements are unassigned (null):

    std::cout << parts[0] << end; // parts[0] is null
    

    Either:

    (Consider changing to std::vector<std::string>.)

    Just to mention boost::split() that can produces a list of tokens (std::vector<std::string>) from an input string and permits specification of multiple delimiters.