J
Joakim Hove
Hello,
I have written the following (stripped down) code:
/********************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char * path_fmt_alloc_path(const char * fmt , ...) {
char * new_path;
va_list ap;
va_start(ap , fmt);
{
int path_length = vsnprintf(new_path , 0 , fmt , ap);
new_path = malloc(path_length + 1);
vsnprintf(new_path , path_length + 1 , fmt , ap);
}
va_end(ap);
return new_path;
}
int test(const char * fmt , int id) {
char * s = path_fmt_alloc_path(fmt , id);
printf("%d -> %s \n",id ,s);
free(s);
}
int main(int argc, char **argv) {
const char * fmt = "%d";
test(fmt , 1 );
test(fmt , 2 );
test(fmt , 3 );
}
/*********************************/
When compiling with:
bash% gcc -m32 va_test.c
I get the result:
1 -> 1
2 -> 2
3 -> 3
i.e. what I expected. However it fails when compiling in 64 bit mode:
bash% gcc va_test.c
1 -> -
2 -> 0
3 -> 0
This is done with gcc version 3.4.6...
/***************************/
I am in some invoking undefined behaviour, or is this a bug somewhere
in my 64 bit toolchain?
Best Regards
Joakim Hove
I have written the following (stripped down) code:
/********************************/
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
char * path_fmt_alloc_path(const char * fmt , ...) {
char * new_path;
va_list ap;
va_start(ap , fmt);
{
int path_length = vsnprintf(new_path , 0 , fmt , ap);
new_path = malloc(path_length + 1);
vsnprintf(new_path , path_length + 1 , fmt , ap);
}
va_end(ap);
return new_path;
}
int test(const char * fmt , int id) {
char * s = path_fmt_alloc_path(fmt , id);
printf("%d -> %s \n",id ,s);
free(s);
}
int main(int argc, char **argv) {
const char * fmt = "%d";
test(fmt , 1 );
test(fmt , 2 );
test(fmt , 3 );
}
/*********************************/
When compiling with:
bash% gcc -m32 va_test.c
I get the result:
1 -> 1
2 -> 2
3 -> 3
i.e. what I expected. However it fails when compiling in 64 bit mode:
bash% gcc va_test.c
1 -> -
2 -> 0
3 -> 0
This is done with gcc version 3.4.6...
/***************************/
I am in some invoking undefined behaviour, or is this a bug somewhere
in my 64 bit toolchain?
Best Regards
Joakim Hove