-
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
-
May 6th, 2009, 07:24 PM
#2
Re: Either I'm getting dumber or C++ is getting harder !
Originally Posted by Mike Pliam
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.
It isn't the compiler, it's your code.
Code:
char cbuffer[1024];
//...
strlen(cbuffer);
What does strlen() return? Please don't say "1024".
Regards,
Paul McKenzie
-
May 6th, 2009, 07:33 PM
#3
Re: Either I'm getting dumber or C++ is getting harder !
You're asking strlen to determine the length of an uninitialized c-string. There's no telling what number it may spit out. You're probably being saved in the debug build by some of the extra bounds checking stuff that the compiler does automagically. In the optimized release build, none of that exists.
-
May 7th, 2009, 12:23 AM
#4
Re: Either I'm getting dumber or C++ is getting harder !
Yes. This works in Release:
Code:
int n = _vsnprintf_s(cbuffer, 1024, 1024, logline, argList);
But I have always wondered what use such apparent redundancy serves.
I consider the question answered in the affirmative: Yes. I am getting dumber.
Thanks for your patience and kind help.
mpliam
-
May 7th, 2009, 12:59 AM
#5
Re: Either I'm getting dumber or C++ is getting harder !
It's an _s function. There's some rather silly redundancy in several of those.
My guess is that Microsoft thought some people would confuse "bytes" with "character count" if they didn't give them explicitly different fields. For chars it's the same; for wchar_ts, not so much.
Honestly I don't ever use the _s functions myself. I generally think the better solution is to just use C++ methods which sidestep the safety issues entirely.
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
|