Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7194959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T20:28:52+00:00 2026-05-28T20:28:52+00:00

I have written a function which tries to do an auto-allocating sprintf by returning

  • 0

I have written a function which tries to do an auto-allocating sprintf by returning a std::string instead of writing into a user-supplied char*. (Please, no answers recommending iostreams or Boost.Format or friends — I know they exist, I do use them in other contexts, but there is a requirement for this particular case.)

std::string FormatString(const std::string& format, va_list argList)
{
    char smallBuffer[500], *text = smallBuffer;
    int length = _countof(smallBuffer);

    // MSVC is not C99 conformant, so its vsnprintf returns -1
    // on insufficient buffer space
    int outputSize = _vsnprintf(text, length, format.c_str(), argList);
    while (outputSize < 0 && errno == ERANGE && length > 0)
    {
        length <<= 1;
        if (text != smallBuffer) { delete[] text; }
        text = new char[length];
        outputSize = _vsnprintf(text, length, format.c_str(), argList);
    }
    if (outputSize < 0)
    {
        throw std::runtime_error("Failed to format string.");
    }

    std::string ret(text);
    if (text != smallBuffer)
    {
        delete[] text;
    }
    return ret;
}

std::string FormatString(const std::string& format, ...)
{
    va_list argList;
    va_start(argList, format);

    std::string result;
    try
    {
        result = FormatString(format, argList);
    }
    catch(...)
    {
        va_end(argList);
        throw;
    }
    va_end(argList);

    return result;
}

int _tmain(int argc, _TCHAR* argv[])
{
    int foo = 1234;
    std::string bar = "BlaBla";
    std::cout << FormatString("%i (%s)", foo, bar.c_str()) << std::endl;
    return 0;
}

(And yes, I see the irony of piping a C-formatted string to a C++ iostream. This is just test code.)

Unfortunately, using VS2008, it’s crashing deep within the bowels of the printf internals, apparently because it’s reading the wrong arguments out of the va_list (according to the debugger, after the va_start it’s pointing at a four-byte null sequence immediately prior to the “real” first parameter).

Of particular note is that if in the variadic function I change the const std::string& format to just std::string format (ie. pass by value), it works properly; it also does so if I change it to a const char *, of course.

Is this some sort of compiler bug, or is it not legal to use a va_list with reference parameters?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T20:28:53+00:00Added an answer on May 28, 2026 at 8:28 pm

    I think you are out of luck if you want to pass a reference. Here is what the C++2011 standard has to say about the subject in 18.10 [support.runtime] paragraph 3:

    The restrictions that ISO C places on the second parameter to the va_start() macro in header are different in this International Standard. The parameter parmN is the identifier of the rightmost parameter in the variable parameter list of the function definition (the one just before the …).230 If the parameter parmN is declared with a function, array, or reference type, or with a type that is not compatible with the type that results when passing an argument for which there is no parameter, the behavior is undefined.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a simple jQuery.ajax function which loads a user control from the
I have a function written in C# which has one parameter type as Dictionary<string
I have written one JavaScript function which would rise the event when the User
I have written my own function, which in C would be declared like this,
I have written a javascript function that uses setInterval to manipulate a string every
I have written a small function, which uses ElementTree and xpath to extract the
I have written a little helper function which performs some sort of drawing operations,
I have written an SWT UI which has a primary function of displaying text
I have written this function which shuffles the contents of a NSString , and
I have written a function that gets a given number of random records from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.