Reputation: 1407
I am getting an exception of a bad pointer (0xcccccccc ) for temp below under case 's':
string Logger::format(const char *str, va_list args)
{
ostringstream output;
for(int i = 0; i < strlen(str); ++i)
{
if(str[i] == '%' && str[i+1] != '%' && i+1 < strlen(str))
{
switch(str[i+1])
{
case 's':
{
char *temp = va_arg(args, char*);
output << temp;
break;
}
case 'i':
{
int temp = va_arg(args, int);
output << temp;
break;
}
case 'd':
{
double temp = va_arg(args, double);
output << temp;
break;
}
case 'f':
{
float temp = va_arg(args, float);
output << temp;
break;
}
default:
output << str[i];
}
i++;
}
else
{
output << str[i];
}
}
return output.str();
}
The above function is called by this:
void Logger::debugFormat(string message, ...)
{
const char* cstr = message.c_str();
va_list args;
va_start(args, cstr);
record(DEBUGGING, format(cstr, args));
va_end(args);
}
I am calling the above this way in all my code
Logger::debugFormat("Loading Image %s", path.c_str());
Any other type (int, double, float) work all fine. Any help is appreciated.
Upvotes: 1
Views: 1097
Reputation: 90175
You aren't using va_start
properly. The second argument is supposed to be the function parameter after which the variable list of arguments (represented by ...
) starts. That is, it should be:
va_start(args, message);
Upvotes: 4