0

When we pass arg which is of type va_list as a parameter to a different function which calls vsprintf and vfprint giving one of the parameter as args, It gives core dump. But when we make a copy (i.e va_copy) and pass copy to one of vsprintf or vfprintf and args to other it work fines.
This happens in 64 bit compiler and works fine with 32 bit compiler.
Can anyone explain be the reason?
For example below code will cause core dump

func(int a, char *preBuf,va_list arg)
{
// Initialization and memory allocation Code
    len = vfprintf (file,preBuf,arg);
// Some code
    vsprintf (file,preBuf,arg);
// Other part of Code
}

And if we use va_copy it works fine.
For example if below changes are made it works fine.

func(int a, char *preBuf,va_list arg)
{
// Initialization and memory allocation Code
va_copy (copy , arg);
len = vfprintf (file,preBuf,copy);
// Some code
vsprintf (file,preBuf,arg);
// Other part of Code
}

Edited by Learningvinit

3
Contributors
2
Replies
13
Views
3 Years
Discussion Span
Last Post by L7Sqr
0

From the man pages:

   int vfprintf(FILE *stream, const char *format, va_list ap);
   int vsprintf(char *str, const char *format, va_list ap);

The two functions take differenet argument types for the first argument. I don't know what file is in your example but it certainly can not be pointer to both a FILE and char at the same time. My guess is that you are ignoring compiler warnings and the fact that it seems to work on a 32-bit system is simply luck.

Perhaps providing a full example of your code would help us investigate further.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.