title: Array as out parameter in c++
body: <p>I created a function that returns an error code (<code>ErrCode</code> enum) and pass two output parameters. But when I print the result of the function, I don't get the correct values in the array.</p>
  
  <pre><code>// .. some codes here ..
  ErrCode err;
  short lstCnt;
  short lstArr[] = {};
  err = getTrimmedList(lstArr, &amp;lstCnt);
  
  // list returned array (for comparison)
  for (int i=0; i&lt;lstCnt; ++i)
   printf("lstArr[%3d] = %d", i, lstArr[i]);
  // .. some codes here ..
  </code></pre>
  
  <p>The <code>getTrimmedList</code> function is like this:</p>
  
  <pre><code>ErrCode getTrimmedList(short* vList, short* vCnt)
  {
    short cnt;
    ErrCode err = foo.getListCount(FOO_TYPE_1, &amp;cnt);
    if (NoError!=err) return err;
  
    short* list = new short [cnt];
  
    short total = 0;
    for (short i=0; i&lt;cnt; ++i)
    {
      FooBar bar = foo.getEntryByIndex(FOO_TYPE_1, i);
  
      if (bar.isDeleted) continue;
  
      list[total] = i;
      ++total;
    }
  
    *vCnt = total;
    //vList = (short*)realloc(index, sizeof(short)*total);
    vList = (short*)malloc(sizeof(short)*total);
    memcpy(vList, list, sizeof(short)*total)
  
    // list returned array (for comparison)
    for (int i=0; i&lt;lstCnt; ++i)
     printf("lstArr[%3d] = %d", i, lstArr[i]);
  
    return NoError;
  }
  </code></pre>
  
  <p>where:</p>
  
  <ul>
  <li><code>foo</code> is an object that holds arrays of <code>FooBar</code> objects</li>
  <li><code>foo.getListCount()</code> returns the number of objects with type <code>FOO_TYPE_1</code></li>
  <li><code>FOO_TYPE_1</code> is the type of object we want to take/list</li>
  <li><code>foo.getEntryByIndex()</code> returns the <code>i</code>th <code>FooBar</code> object with type <code>FOO_TYPE_1</code></li>
  <li><code>bar.isDeleted</code> is a flag that tells if <code>bar</code> is considered as 'deleted' or not</li>
  </ul>
  
  <p>What's my error?</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>Sorry, I copied a wrong line. I commented it above and put the correct line.</p>
  
  <p><strong>Edit 2</strong></p>
  
  <p>I don't have control over the returns of <code>foo</code> and <code>bar</code>. All their function returns are <code>ErrCode</code> and the outputs are passed through parameter.</p>