-
May 6th, 2009, 07:05 PM
#1
Either I'm getting dumber or C++ is getting harder !
I cannot figure out why the same function performs differently in debug and release configurations (VS 2005 Std Edition, C++). Take this simple console application.
Code:
#include <string>
#include <cstdlib>
#include <cstdio>
#include <iostream>
using namespace std;
#include <cstdarg> // needed to use ellipses
void xWrite(char* logline, ...)
{
va_list argList;
char cbuffer[1024];
va_start(argList, logline);
// int n = vsnprintf_s(cbuffer, 1024, strlen(cbuffer), logline, argList);
int n = _vsnprintf_s(cbuffer, 1024, strlen(cbuffer), logline, argList);
va_end(argList);
cout << cbuffer << endl;
cout << "n = " << n << endl; // 43 in debug mode, -1 in release mode
}
int main()
{
xWrite("Gimme %d and I'll take %0.2f to get to %s.", 3, 4.56, "seven");
return 0;
}
When run in Release configuration:
G
n = -1
When run in Debug configuration:
Gimme 3 and I'll take 4.56 to get to seven.
n = 43
I don't know how you folks feel about this kind of thing, but I find it rather disheartening. And I find myself loosing confidence in the Microsoft compiler.
Microsoft provides the usual confusing harangue regarding the function, vsnprintf_s.
http://msdn.microsoft.com/en-us/libr...zz(VS.80).aspx
As you can see if you try to run this program, using the template version doesnt alter the results.
Your comments and advice greatly appreciated.
mpliam
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|