• <tfoot id='H5bNr'></tfoot>
  • <small id='H5bNr'></small><noframes id='H5bNr'>

  • <i id='H5bNr'><tr id='H5bNr'><dt id='H5bNr'><q id='H5bNr'><span id='H5bNr'><b id='H5bNr'><form id='H5bNr'><ins id='H5bNr'></ins><ul id='H5bNr'></ul><sub id='H5bNr'></sub></form><legend id='H5bNr'></legend><bdo id='H5bNr'><pre id='H5bNr'><center id='H5bNr'></center></pre></bdo></b><th id='H5bNr'></th></span></q></dt></tr></i><div id='H5bNr'><tfoot id='H5bNr'></tfoot><dl id='H5bNr'><fieldset id='H5bNr'></fieldset></dl></div>

        <bdo id='H5bNr'></bdo><ul id='H5bNr'></ul>
      <legend id='H5bNr'><style id='H5bNr'><dir id='H5bNr'><q id='H5bNr'></q></dir></style></legend>
      1. va_copy——移植到visual C++?

        时间:2023-12-03

        <small id='t1Xba'></small><noframes id='t1Xba'>

        <legend id='t1Xba'><style id='t1Xba'><dir id='t1Xba'><q id='t1Xba'></q></dir></style></legend>

          <tbody id='t1Xba'></tbody>
            • <bdo id='t1Xba'></bdo><ul id='t1Xba'></ul>

              <tfoot id='t1Xba'></tfoot>
                <i id='t1Xba'><tr id='t1Xba'><dt id='t1Xba'><q id='t1Xba'><span id='t1Xba'><b id='t1Xba'><form id='t1Xba'><ins id='t1Xba'></ins><ul id='t1Xba'></ul><sub id='t1Xba'></sub></form><legend id='t1Xba'></legend><bdo id='t1Xba'><pre id='t1Xba'><center id='t1Xba'></center></pre></bdo></b><th id='t1Xba'></th></span></q></dt></tr></i><div id='t1Xba'><tfoot id='t1Xba'></tfoot><dl id='t1Xba'><fieldset id='t1Xba'></fieldset></dl></div>

                  本文介绍了va_copy——移植到visual C++?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  上一个问题 展示了一种打印到字符串的好方法.答案涉及 va_copy:

                  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]);
                     }
                  

                  }

                  我遇到的问题是上面的代码不能移植到 Visual C++,因为它不提供 va_copy(甚至 __va_copy).那么,有谁知道如何安全地移植上述代码?据推测,我需要做一个 va_copy 副本,因为 vsnprintf 破坏性地修改了传递的 va_list.

                  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.

                  推荐答案

                  你应该能够摆脱只是做一个常规的任务:

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

                  va_list apcopy = ap;
                  

                  从技术上讲,它是不可移植和未定义的行为,但它适用于大多数编译器和体系结构.在 x86 调用约定中,va_list 只是指向堆栈的指针,可以安全复制.

                  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.

                  这篇关于va_copy——移植到visual C++?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:VC++中如何添加外部库进行编译? 下一篇:Windows 控制台上的 UTF-8 输出

                  相关文章

                  <legend id='LXLiB'><style id='LXLiB'><dir id='LXLiB'><q id='LXLiB'></q></dir></style></legend>
                  <tfoot id='LXLiB'></tfoot>
                1. <small id='LXLiB'></small><noframes id='LXLiB'>

                      <bdo id='LXLiB'></bdo><ul id='LXLiB'></ul>
                    <i id='LXLiB'><tr id='LXLiB'><dt id='LXLiB'><q id='LXLiB'><span id='LXLiB'><b id='LXLiB'><form id='LXLiB'><ins id='LXLiB'></ins><ul id='LXLiB'></ul><sub id='LXLiB'></sub></form><legend id='LXLiB'></legend><bdo id='LXLiB'><pre id='LXLiB'><center id='LXLiB'></center></pre></bdo></b><th id='LXLiB'></th></span></q></dt></tr></i><div id='LXLiB'><tfoot id='LXLiB'></tfoot><dl id='LXLiB'><fieldset id='LXLiB'></fieldset></dl></div>