D
dicaprio martens
Hello,
I am trying to split `get tokens' from a string. However it
keeps segfaulting on me after processing prox. 1mb of plain
text data. According to the debugger it segfaults on the
free() call.
I examend the memory free() try's to give back, but it's clearly
allocated by a malloc call. Could it be, that calling free() so
many times is not a good idea ? Or.. ?
What would be the right way of doing stuff like this ?
I have listed the funtion that splits the string up into tokens
below. If anyone could comment what I am missing ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTOKENS 128
#define MAXLINE 1024
/* split string, return token array */
char **split(char *string, char *delim);
int main(void) {
char *delim = ".,:;`'\"+-_(){}[]<>*&^%$#@!?~/|\\= \t\r\n1234567890";
char **tokens = NULL;
char line[MAXLINE];
int i = 0;
while(fgets(line, MAXLINE, stdin) != NULL) {
if(strlen(line) < 3)
continue;
tokens = split(line, delim);
for(i = 0; tokens != NULL; i++)
printf("%02d: %s\n", i, tokens);
for(i = 0; tokens != NULL; i++)
free(tokens);
}
return 0;
}
/* split string, return token array */
char **split(char *string, char *delim) {
char **tokens = NULL;
char *working = NULL;
char *token = NULL;
int idx = 0;
tokens = malloc(sizeof(char *) * MAXTOKENS);
working = malloc((strlen(string) + 1) * sizeof(char));
if(tokens == NULL || working == NULL)
return NULL;
strcpy(working, string);
for(idx = 0; idx < MAXTOKENS; idx++)
tokens[idx] = NULL;
token = strtok(working, delim);
idx = 0;
while((idx < MAXTOKENS) && (token != NULL)) {
tokens[idx] = malloc(sizeof(char) * strlen(token));
if(tokens[idx] != NULL) {
strcpy(tokens[idx], token);
idx++;
token = strtok(NULL, delim);
}
}
free(working);
return tokens;
}
I am trying to split `get tokens' from a string. However it
keeps segfaulting on me after processing prox. 1mb of plain
text data. According to the debugger it segfaults on the
free() call.
I examend the memory free() try's to give back, but it's clearly
allocated by a malloc call. Could it be, that calling free() so
many times is not a good idea ? Or.. ?
What would be the right way of doing stuff like this ?
I have listed the funtion that splits the string up into tokens
below. If anyone could comment what I am missing ?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAXTOKENS 128
#define MAXLINE 1024
/* split string, return token array */
char **split(char *string, char *delim);
int main(void) {
char *delim = ".,:;`'\"+-_(){}[]<>*&^%$#@!?~/|\\= \t\r\n1234567890";
char **tokens = NULL;
char line[MAXLINE];
int i = 0;
while(fgets(line, MAXLINE, stdin) != NULL) {
if(strlen(line) < 3)
continue;
tokens = split(line, delim);
for(i = 0; tokens != NULL; i++)
printf("%02d: %s\n", i, tokens);
for(i = 0; tokens != NULL; i++)
free(tokens);
}
return 0;
}
/* split string, return token array */
char **split(char *string, char *delim) {
char **tokens = NULL;
char *working = NULL;
char *token = NULL;
int idx = 0;
tokens = malloc(sizeof(char *) * MAXTOKENS);
working = malloc((strlen(string) + 1) * sizeof(char));
if(tokens == NULL || working == NULL)
return NULL;
strcpy(working, string);
for(idx = 0; idx < MAXTOKENS; idx++)
tokens[idx] = NULL;
token = strtok(working, delim);
idx = 0;
while((idx < MAXTOKENS) && (token != NULL)) {
tokens[idx] = malloc(sizeof(char) * strlen(token));
if(tokens[idx] != NULL) {
strcpy(tokens[idx], token);
idx++;
token = strtok(NULL, delim);
}
}
free(working);
return tokens;
}