B
Bob
I have been working on the following program. The goal is to have a
tokenizing routine that avoids some of the problems of strtok(), the
comments should explain the features.
This runs fine on Solaris/gcc, but crashes when run from VC++ (C mode).
The problem occurs after the first true reallocation (second pass
through main loop). The little debug part at the end of the loop prints
one time, it crashes before a second is displayed. Seems likely that
something is trashing the heap but I haven't spotted it.
Code follows:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void cleanup(char **array, size_t n)
{
if (array != 0)
{
unsigned int i;
for (i = 0; i <= n; i++)
{
free(array);
}
free(array);
}
}
/* Divides a string into tokens
Does not alter input string
Single delimiter char
Does not merge adjacent delimiters
Returns null-ptr-terminated array of substring pointers
*/
char** tokenize (const char *instring, int delimiter)
{
char **strarray = 0;
char **tmp = 0;
char *tok = 0;
const char *start = instring;
const char *end = 0;
int done = 0;
int n = 0;
size_t len = 0;
int i;
if (start == 0)
{
return 0;
}
while (!done)
{
end = strchr(start, delimiter);
if (end == 0) /* end of string */
{
len = strlen(start);
done = 1;
}
else
{
len = end - start;
}
n++; /* reflects num elements in array */
/* allocates one extra pointer for null */
tmp = realloc(strarray, n+1);
if (tmp == 0) /* allocation failed */
{
cleanup(strarray, n);
strarray = 0;
done = 1;
}
else
{
strarray = tmp;
tok = malloc(len+1);
if (tok == 0)
{
cleanup(strarray, n);
strarray = 0;
done = 1;
}
else
{
strncpy(tok, start, len);
tok[len] = 0;
strarray[n-1] = tok;
strarray[n] = 0;
}
}
start = end + 1;
/* debug code intermediate state of array */
for(i=0;strarray;i++)
puts(strarray);
}
return strarray;
}
int main(void)
{
char *s1 = "one and two and three";
char **arr;
int i = 0;
arr = tokenize(s1, ' ');
if (arr == 0)
{
puts("Error");
}
else
{
while (arr != 0)
{
puts(arr);
i++;
}
}
return 0;
}
tokenizing routine that avoids some of the problems of strtok(), the
comments should explain the features.
This runs fine on Solaris/gcc, but crashes when run from VC++ (C mode).
The problem occurs after the first true reallocation (second pass
through main loop). The little debug part at the end of the loop prints
one time, it crashes before a second is displayed. Seems likely that
something is trashing the heap but I haven't spotted it.
Code follows:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
void cleanup(char **array, size_t n)
{
if (array != 0)
{
unsigned int i;
for (i = 0; i <= n; i++)
{
free(array);
}
free(array);
}
}
/* Divides a string into tokens
Does not alter input string
Single delimiter char
Does not merge adjacent delimiters
Returns null-ptr-terminated array of substring pointers
*/
char** tokenize (const char *instring, int delimiter)
{
char **strarray = 0;
char **tmp = 0;
char *tok = 0;
const char *start = instring;
const char *end = 0;
int done = 0;
int n = 0;
size_t len = 0;
int i;
if (start == 0)
{
return 0;
}
while (!done)
{
end = strchr(start, delimiter);
if (end == 0) /* end of string */
{
len = strlen(start);
done = 1;
}
else
{
len = end - start;
}
n++; /* reflects num elements in array */
/* allocates one extra pointer for null */
tmp = realloc(strarray, n+1);
if (tmp == 0) /* allocation failed */
{
cleanup(strarray, n);
strarray = 0;
done = 1;
}
else
{
strarray = tmp;
tok = malloc(len+1);
if (tok == 0)
{
cleanup(strarray, n);
strarray = 0;
done = 1;
}
else
{
strncpy(tok, start, len);
tok[len] = 0;
strarray[n-1] = tok;
strarray[n] = 0;
}
}
start = end + 1;
/* debug code intermediate state of array */
for(i=0;strarray;i++)
puts(strarray);
}
return strarray;
}
int main(void)
{
char *s1 = "one and two and three";
char **arr;
int i = 0;
arr = tokenize(s1, ' ');
if (arr == 0)
{
puts("Error");
}
else
{
while (arr != 0)
{
puts(arr);
i++;
}
}
return 0;
}