?
=?ISO-8859-1?Q?Une_b=E9vue?=
I'm still a neby in C, i do have two versions of about the same algo.
The first one works fine but de second. I don't see what's the prob with
the second.
the purpose to cut out in sections a unix path.
the test source :
--- essai_token.c ------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
const char *unix_sep = "/";
char *token = malloc(sizeof (*token) * 256);
if(token == NULL) return EXIT_FAILURE;
//
// Version 1
//
printf("// Version 1\n");
char *path_v1 = strdup("/path_v1/to/something");
printf("path_v1 = %s\n", path_v1);
token = strtok(path_v1, unix_sep);
printf("token = %s\n", token);
while(token != NULL) {
token = strtok(NULL, unix_sep);
printf("token = %s\n", token);
}
free(path_v1);
path_v1 = NULL;
printf("\n");
//
// Version 2
//
printf("// Version 2\n");
char *path_v2 = malloc(sizeof (*path_v2) * 256);
if(path_v2 == NULL) return EXIT_FAILURE;
path_v2 = "/path_v2/to/something";
printf("path_v2 = %s\n", path_v2);
token = strtok(path_v2, unix_sep);// <= broken HERE
printf("token = %s\n", token);
while(token != NULL) {
token = strtok(NULL, unix_sep);
printf("token = %s\n", token);
}
free(path_v2);
path_v2 = NULL;
free(token);
token = NULL;
return EXIT_SUCCESS;
}
------------------------------------------------------------------------
run output :
~/work/C/developpez/listes_chainees_double%> ./essai_token
// Version 1
path_v1 = /path_v1/to/something
token = path_v1
token = to
token = something
token = (null)
// Version 2
path_v2 = /path_v2/to/something
zsh: bus error ./essai_token
then, the broken line is "token = strtok(path_v2, unix_sep);"
no warnings nor errors at the compil time using the following gcc
options :
alias ycc='cc -W -Wall -Wextra -Wuninitialized -Wstrict-prototypes
-Wmissing-prototypes -pedantic -std=c99 -O2 -pipe -o '
pratically i make use only of the first version, however i'd like to
understand what's my mistake in the second one...
i know that working with strtok needs a writable mem zone, i assume
malloc does that ???
my setup :
Mac OS X 10.4.7
~/work/C/developpez/listes_chainees_double%> gcc --version
powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build
5363)
The first one works fine but de second. I don't see what's the prob with
the second.
the purpose to cut out in sections a unix path.
the test source :
--- essai_token.c ------------------------------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main (void)
{
const char *unix_sep = "/";
char *token = malloc(sizeof (*token) * 256);
if(token == NULL) return EXIT_FAILURE;
//
// Version 1
//
printf("// Version 1\n");
char *path_v1 = strdup("/path_v1/to/something");
printf("path_v1 = %s\n", path_v1);
token = strtok(path_v1, unix_sep);
printf("token = %s\n", token);
while(token != NULL) {
token = strtok(NULL, unix_sep);
printf("token = %s\n", token);
}
free(path_v1);
path_v1 = NULL;
printf("\n");
//
// Version 2
//
printf("// Version 2\n");
char *path_v2 = malloc(sizeof (*path_v2) * 256);
if(path_v2 == NULL) return EXIT_FAILURE;
path_v2 = "/path_v2/to/something";
printf("path_v2 = %s\n", path_v2);
token = strtok(path_v2, unix_sep);// <= broken HERE
printf("token = %s\n", token);
while(token != NULL) {
token = strtok(NULL, unix_sep);
printf("token = %s\n", token);
}
free(path_v2);
path_v2 = NULL;
free(token);
token = NULL;
return EXIT_SUCCESS;
}
------------------------------------------------------------------------
run output :
~/work/C/developpez/listes_chainees_double%> ./essai_token
// Version 1
path_v1 = /path_v1/to/something
token = path_v1
token = to
token = something
token = (null)
// Version 2
path_v2 = /path_v2/to/something
zsh: bus error ./essai_token
then, the broken line is "token = strtok(path_v2, unix_sep);"
no warnings nor errors at the compil time using the following gcc
options :
alias ycc='cc -W -Wall -Wextra -Wuninitialized -Wstrict-prototypes
-Wmissing-prototypes -pedantic -std=c99 -O2 -pipe -o '
pratically i make use only of the first version, however i'd like to
understand what's my mistake in the second one...
i know that working with strtok needs a writable mem zone, i assume
malloc does that ???
my setup :
Mac OS X 10.4.7
~/work/C/developpez/listes_chainees_double%> gcc --version
powerpc-apple-darwin8-gcc-4.0.1 (GCC) 4.0.1 (Apple Computer, Inc. build
5363)