pdah
June 19th, 2009, 04:06 AM
Hi everybody, I'm having a problem with function strtok_r, it seems that free() function always throws Seg Fault after I use strtok_r.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char* hostport = (char *)malloc(100);
char* buf = (char *)malloc(100);
memcpy(hostport,"192.168.0.10:9000/12345678",26);
strtok_r(hostport, ":", &buf);
printf("buf=%s\n",buf);
free(hostport);
free(buf);
return 0;
}
The error message thrown by this script when executing on Ubuntu :
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer
If I compile this script and run it on Centos, the error message is
*** glibc detected *** ./a.out: free(): invalid pointer
If I comment out the line "free(buf)", there's no error anymore. Can you help me to figure out this problem ?
Thanks in advance.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char* hostport = (char *)malloc(100);
char* buf = (char *)malloc(100);
memcpy(hostport,"192.168.0.10:9000/12345678",26);
strtok_r(hostport, ":", &buf);
printf("buf=%s\n",buf);
free(hostport);
free(buf);
return 0;
}
The error message thrown by this script when executing on Ubuntu :
*** glibc detected *** ./a.out: munmap_chunk(): invalid pointer
If I compile this script and run it on Centos, the error message is
*** glibc detected *** ./a.out: free(): invalid pointer
If I comment out the line "free(buf)", there's no error anymore. Can you help me to figure out this problem ?
Thanks in advance.