G
Goh, Yong Kwang
I'm trying to create a function that given a string, tokenize it and
put into a dynamically-sized array of char* which is in turn also
dynamically allocated based on the string token length.
I call the function using this code fragement in my main function:
---
char** arg_array;
arg_count = create_arg_array(command, argument, arg_array);
for(count = 0; count < arg_count; count++)
printf("arg_array[%d]: %s\n", count, arg_array[count]);
---
And it always crashes with a segmentation fault when going into the
for loop to print out the array of strings. Why does it crash when
I've already called malloc and realloc (inside create_arg_array()
function) to reserve some memory for the string array?
The create_arg_array is given below.
What's wrong with this function?
---
size_t create_arg_array(char* file, char* arg, char** array){
size_t count;
char* argument;
count = 1;
/* 1 for the filename and 1 for the terminating NULL ptr. */
array = (char**) malloc(count * sizeof(char*));
array[0] = (char*) malloc(strlen(file) + 1);
strcpy((*array)[0], file);
if(arg != NULL){
argument = strtok(arg, " \n");
if(argument != NULL){
array = (char**) realloc(array, (count + 1) * sizeof(char*));
array[count] = (char*) malloc(strlen(argument) + 1);
strcpy(array[count], argument);
count++;
}
while((argument = strtok(NULL, " \n")) != NULL){
array = (char**) realloc(array, (count + 1) * sizeof(char*));
array[count] = (char*) malloc(strlen(argument) + 1);
strcpy(array[count], argument);
count++;
}
}
++count;
array = (char**) realloc(array, (count * sizeof(char*)));
array[count-1] = NULL;
return count-1; /* Minus 1 for the ending NULL */
}
---
I thought the address returned by realloc and malloc is lost when I
return to main function and I've also tried changing the function to
accept a pointer to the array of string (char***) but it doesn't help.
Any advice appreciated.
Thank you.
Goh, Yong Kwang
Singapore
(e-mail address removed)
put into a dynamically-sized array of char* which is in turn also
dynamically allocated based on the string token length.
I call the function using this code fragement in my main function:
---
char** arg_array;
arg_count = create_arg_array(command, argument, arg_array);
for(count = 0; count < arg_count; count++)
printf("arg_array[%d]: %s\n", count, arg_array[count]);
---
And it always crashes with a segmentation fault when going into the
for loop to print out the array of strings. Why does it crash when
I've already called malloc and realloc (inside create_arg_array()
function) to reserve some memory for the string array?
The create_arg_array is given below.
What's wrong with this function?
---
size_t create_arg_array(char* file, char* arg, char** array){
size_t count;
char* argument;
count = 1;
/* 1 for the filename and 1 for the terminating NULL ptr. */
array = (char**) malloc(count * sizeof(char*));
array[0] = (char*) malloc(strlen(file) + 1);
strcpy((*array)[0], file);
if(arg != NULL){
argument = strtok(arg, " \n");
if(argument != NULL){
array = (char**) realloc(array, (count + 1) * sizeof(char*));
array[count] = (char*) malloc(strlen(argument) + 1);
strcpy(array[count], argument);
count++;
}
while((argument = strtok(NULL, " \n")) != NULL){
array = (char**) realloc(array, (count + 1) * sizeof(char*));
array[count] = (char*) malloc(strlen(argument) + 1);
strcpy(array[count], argument);
count++;
}
}
++count;
array = (char**) realloc(array, (count * sizeof(char*)));
array[count-1] = NULL;
return count-1; /* Minus 1 for the ending NULL */
}
---
I thought the address returned by realloc and malloc is lost when I
return to main function and I've also tried changing the function to
accept a pointer to the array of string (char***) but it doesn't help.
Any advice appreciated.
Thank you.
Goh, Yong Kwang
Singapore
(e-mail address removed)