CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2002
    Posts
    1,798

    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

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Either I'm getting dumber or C++ is getting harder !

    Quote Originally Posted by Mike Pliam View Post
    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

  3. #3
    Join Date
    Aug 2007
    Posts
    858

    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.

  4. #4
    Join Date
    May 2002
    Posts
    1,798

    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

  5. #5
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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
  •  





Click Here to Expand Forum to Full Width

Featured