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 145565
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T08:26:46+00:00 2026-05-11T08:26:46+00:00

A previous question showed a nice way of printing to a string. The answer

  • 0

A previous question showed a nice way of printing to a string. The answer involved va_copy:

std::string format (const char *fmt, ...); {    va_list ap;    va_start (ap, fmt);    std::string buf = vformat (fmt, ap);    va_end (ap);    return buf; }   std::string vformat (const char *fmt, va_list ap) {    // Allocate a buffer on the stack that's big enough for us almost    // all the time.    s ize_t size = 1024;    char buf[size];     // Try to vsnprintf into our buffer.    va_list apcopy;    va_copy (apcopy, ap);    int needed = vsnprintf (&buf[0], size, fmt, ap);     if (needed <= size) {        // It fit fine the first time, we're done.        return std::string (&buf[0]);    } else {        // vsnprintf reported that it wanted to write more characters        // than we allotted.  So do a malloc of the right size and try again.        // This doesn't happen very often if we chose our initial size        // well.        std::vector <char> buf;        size = needed;        buf.resize (size);        needed = vsnprintf (&buf[0], size, fmt, apcopy);        return std::string (&buf[0]);    } 

}

The problem I’m having is that the above code doesn’t port to Visual C++ because it doesn’t provide va_copy (or even __va_copy). So, does anyone know how to safely port the above code? Presumably, I need to do a va_copy copy because vsnprintf destructively modifies the passed va_list.

  • 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. 2026-05-11T08:26:47+00:00Added an answer on May 11, 2026 at 8:26 am

    You should be able to get away with just doing a regular assignment:

    va_list apcopy = ap; 

    It’s technically non-portable and undefined behavior, but it will work with most compilers and architectures. In the x86 calling convention, va_lists are just pointers into the stack and are safe to copy.

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

Sidebar

Related Questions

The answer to a previous question showed that Nexus implement a custom authentication helper
This question is extended part of my previous question, Finding number position in string
This question is based on my previous question which I got a working answer
(This question is related to my previous question , or rather to my answer
A previous question showed that fld in the following code is nil . procedure
My previous question asked for a way to keep a text view and slider
Previous question: (Thank you Tonny Madsen to answer me) Add keyevent to switch ViewPart
In previous question of mine, someone had meantioned that using Semaphores were expensive in
This question is related to a previous question of mine That's my current code
This question follows on from a previous question, that has raised a further issue.

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.