H
Hon Seng Phuah
Hi all,
I do not understand why my code can cause segmentation fault in Unix Gcc.
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *directory,
*pos,
file_name[] = "Geco/ich7/A0/abc/cell_name/mail/good.geco";
int count = 0;
pos = strtok(file_name, "/");
directory = (char *) malloc(sizeof(char) * (strlen(file_name) + 1));
directory[0] = '\0';
while (pos)
{
strcat(directory, pos);
pos = strtok(NULL, "/");
}
free(directory); /* segmentation faults occurs here */
return 0;
}
If you know what I did wrong, please point out for me. Thanks.
I do not understand why my code can cause segmentation fault in Unix Gcc.
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *directory,
*pos,
file_name[] = "Geco/ich7/A0/abc/cell_name/mail/good.geco";
int count = 0;
pos = strtok(file_name, "/");
directory = (char *) malloc(sizeof(char) * (strlen(file_name) + 1));
directory[0] = '\0';
while (pos)
{
strcat(directory, pos);
pos = strtok(NULL, "/");
}
free(directory); /* segmentation faults occurs here */
return 0;
}
If you know what I did wrong, please point out for me. Thanks.