Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The approach you are taking is very sane, safe and sound by itself. For example, the below program is using variadic template arguments and forwards them to <code>std::printf()</code>, which is a C-style function that accepts variable number of arguments:</p> <pre><code>#include &lt;utility&gt; #include &lt;cstdio&gt; template &lt;unsigned int S, typename ...T&gt; void my_printf(const char (&amp;fmt)[S], T&amp;&amp;... args) { std::printf(fmt, std::forward&lt;T&gt;(args)...); } int main(int argc, const char* argv[]) { my_printf("Hello, %s!\n", "World!"); my_printf("I will count to %u...\n", 10); for (int i = 0; i &lt; 10; ++i) my_printf("%s %s %u\n", "...", "and", i + 1); my_printf("And here are my arguments :)\n"); for (int i = 0; i &lt; argc; ++i) my_printf("argv[%u] == %s\n", i, argv[i]); } </code></pre> <p>The problem with undefined behavior and possibly stack corruption is caused by something else. In C++, this is most likely caused by passing non-POD types though C's variable argument list (i.e. passing <code>std::string</code> to <code>printf()</code> can do this).</p> <p>Unfortunately, compiler extensions like <a href="http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html" rel="noreferrer">"format attribute"</a> won't help here. But compiler might warn you about non-POD types. For example, call <code>my_printf()</code> with <code>std::string</code> and you will be rewarded with a warning similar to this:</p> <pre><code>./test.cc:7:46: error: cannot pass objects of non-trivially-copyable type ‘class std::basic_string&lt;char&gt;’ through ‘...’ std::printf(fmt, std::forward&lt;T&gt;(args)...); ^ </code></pre> <p>Of course, there could be something else going on that compiler won't be able to catch for you. Unfortunately, there is no better way than to debug your program and see what exactly is causing it to crash.</p> <p>Since you are entering dark waters of C and unsafe arguments passing, debugger is your friend here, and I'd also recommend you use <a href="http://valgrind.org/" rel="noreferrer">Valgrind</a> - it is very helpful catching things like that.</p> <p>Hope it helps. Good Luck!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload