Re: My own fuction: Access Violation error...

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Note that strtok is not a particularly well-designed library function, but its use here is
OK.

And yes, you are doing something blatantly wrong. Read the description of _tcstok VERY
carefully. On subsequent calls, you need to pass NULL in as the first argument.

There are many potential problems here. I assume there is a deep and compelling reason
for using the obsolete 'char' type to represent text? Modern code is always
Unicode-aware, and would be written as

vector<LPTSTR> Split(LPTSTR input, LPCTSTR sep)
{
vector<LPTSTR>v;
LPTSTR token = _tcstok(input, sep);
while(token != NULL)
{
v.push_back(token);
token = _tcstok(NULL, sep);
}
return v;
}

Until this bug is fixed, looking for other problems does not matter. If you still have
problems after fixing the code, post again.
joe

On Mon, 10 Mar 2008 13:13:19 -0700 (PDT), Blue Streak <rdlebreton@xxxxxxxxxxx> wrote:

Hello,

I am trying to implement my own version a split function in VC6 using
a vector and strtok().

vector<char *> Split(char *input, const char *sep)
{ //implementation of VB split function using vectors
vector<char *> v;
char *token;
token = " ";
while (token != NULL)
{
token = strtok(input, sep);
v.push_back(token);
}
return v;
}

The only problem is when I trace it through the first token and I have
a space as the separator (haven't tried another separator, yet) it
crashes and burns (i.e. Access Violation) when it reaches the first
separator / space.

Am I doing something blatantly wrong?
Any suggestions to improve this function?

TIA...
Joseph M. Newcomer [MVP]
email: newcomer@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: split a /path/filename into components
    ... find an equal in the linux world ... Us '/' as that char, ... These functions cannot be used on constant strings. ... The strtok() function uses a static buffer while parsing, ...
    (comp.os.linux.development.apps)
  • Re: pointer from integer?
    ... char *p1, *p2; ... You need to research strtok in your reference. ... makes pointer ... Is there a way to get gdb to give more detailed output? ...
    (comp.lang.c)
  • Re: problem with strtok()
    ... It seems that if I make a call to strtok(), then make a call to another function that also makes use of strtok, the original call is somehow confused or upset. ... int tokenize_input(Sale *sale, char *string){ ... rv = strcpy(dest, source); ...
    (comp.lang.c)
  • Re: strtok causes Segmentation fault
    ... Joe Smith wrote: ... When I run the program I get a segmentation fault on the first strtok. ... int createvarible(const int sock, char *name, char *type, char *value, ...
    (comp.lang.c)