E
eliweiq001
I am trying to implement my own lib function fprintf, but there's some
problem.
the function is
int fprintf(FILE *fp, char *fmt, ...)
{
........
}
when I call fprintf(fp, "%sabc", "efg"), it is OK.
but when I call fprintf(fp, "abc") there will be Segmentation fault ,
same as fprintf(fp, "%s", "abc").
I am using gcc, the error will occur in both linux and windows.
I think there no problem with my function fprintf.
I've wrote a basic version of fprintf below which will also cause the
same error:
// test for fprintf;
typedef int FILE; // just for testing, so
make FILE as INT
FILE *fp;
//fp = 0;
int fprintf(FILE *fp, char *fmt, ...) // the basic version of
fprintf, for testing
{
return 0;
}
int main(int argc, char *argv[])
{
fprintf(fp, "%sz", "aa"); // It's OK
fprintf(fp, "%s", "aa"); // Segmentation Fault ! Why?
fprintf(fp, "aa"); // Segmentation Fault !
return 0;
}
Will these codes cause Segmentation fault in your environment? (please
let me know your compiler and operating system)
problem.
the function is
int fprintf(FILE *fp, char *fmt, ...)
{
........
}
when I call fprintf(fp, "%sabc", "efg"), it is OK.
but when I call fprintf(fp, "abc") there will be Segmentation fault ,
same as fprintf(fp, "%s", "abc").
I am using gcc, the error will occur in both linux and windows.
I think there no problem with my function fprintf.
I've wrote a basic version of fprintf below which will also cause the
same error:
// test for fprintf;
typedef int FILE; // just for testing, so
make FILE as INT
FILE *fp;
//fp = 0;
int fprintf(FILE *fp, char *fmt, ...) // the basic version of
fprintf, for testing
{
return 0;
}
int main(int argc, char *argv[])
{
fprintf(fp, "%sz", "aa"); // It's OK
fprintf(fp, "%s", "aa"); // Segmentation Fault ! Why?
fprintf(fp, "aa"); // Segmentation Fault !
return 0;
}
Will these codes cause Segmentation fault in your environment? (please
let me know your compiler and operating system)