title: What is the appropriate way to pass given vararg parameter to another function?
body: <p>I've begun works on a hook/event system in C++. This system is supposed to handle all sorts of events as notified by other parts of the application.</p>
  
  <p>The issue I've faced is with the way I want it to run.
  Generally, I want it to be so that you call a specific function with all arguments you wish to pass on, and then that function handles calling all registered hooks of that specific event, passing them on the arguments, retrieving their result values and returning it to the original caller.</p>
  
  <p>Generally, this is how it was supposed to look:</p>
  
  <pre><code>CHookReturn* bInitializationStatus = Hook::Run("Initialize", gGame);
  </code></pre>
  
  <pre><code>CHookReturn* bThinkSuccessful = Hook::Run("Think");
  </code></pre>
  
  <p><br>
  <br>
  However, I ran into one issue.
  I set it up in such way that the <code>Run</code> function in the Hook namespace, calling the Run function of the CHookData_t structure, needs to pass on varargs. I couldn't find any other way. This is how it ended up:</p>
  
  <pre><code>union CHookReturn
  {
      const char* m_pszValue;
      int m_iValue;
      float m_flValue;
      double m_dlValue;
      bool m_bValue;
  };
  
  struct CHookData_t
  {
      virtual void Run(CHookReturn* ret, ...) = 0;
  };
  
  namespace Hook
  {
      std::unordered_map&lt;const char*, std::unordered_map&lt;const char*, CHookData_t*&gt;&gt; umHookList;
  
      bool Add(const char*, const char*, CHookData_t*);
      bool Exists(const char*, const char*);
      bool Remove(const char*, const char*);
      int Count(const char*);
      CHookReturn* Run(const char*, ...);
  };
  </code></pre>
  
  <p>CPP file segment of the Hook::Run function:</p>
  
  <pre><code>CHookReturn* Hook::Run(const char* eventName, ...)
  {
      // FIXME: Look into alternative execution.
      // This code seems more like a workaround
      // than what I originally wanted it to be.
  
      int count = Hook::Count(eventName);
      CHookReturn* returnValues = new CHookReturn[count];
      int c = 0;
  
      unordered_map&lt;const char*, CHookData_t*&gt;::iterator itr;
      unordered_map&lt;const char*, CHookData_t*&gt; res;
      res = umHookList.at(eventName);
  
      va_list valist;
      void* args;
      va_copy(args, valist);
  
      for (itr = res.begin(); itr != res.end(); itr++)
      {
          CHookReturn returnData;
          itr-&gt;second-&gt;Run(&amp;returnData, args);
  
          returnValues[c] = returnData;
          ++c;
      }
  
      return returnValues;
  }
  </code></pre>
  
  <p>The above code brings up two warnings which make me question if it is a good idea to execute it in this way, as well as whether there are any alternatives I should look into.</p>
  
  <p>The warnings I received were:<br>
  <code>Warning    C6001   Using uninitialized memory 'valist'.</code><br>
  <code>Warning    C6386   Buffer overrun while writing to 'returnValues':  the writable size is 'count*8' bytes, but '16' bytes might be written.</code></p>
  
  <p>Is there a better way to do this?</p>