title: C++ returning uint16_t array from function causes segmentation fault
body: <pre><code>uint16_t * getRegister(std::string reg) {
      // unordered_map&lt;string, vector&lt;uint16_t&gt;&gt; current_reg_map;// defined globally.
      // vector current_reg_map has minimum 2 values;
  
      uint16_t regval[current_reg_map[reg].size()];
      std::copy(current_reg_map[reg].begin(), current_reg_map[reg].end(), regval);
      return regval;
  }
  
  int main() {
      std::string regname = &quot;4400&quot;;
      uint16_t *tmp = reader.getRegister2(regname, tmp);
      std::cout&lt;&lt;&quot;result &quot;&lt;&lt;tmp[0]&lt;&lt;&quot;\t&quot;&lt;&lt;tmp[1]&lt;&lt;std::endl;
  }
  </code></pre>
  <p>Throws segmentation fault at runtime. A lot of posts in stack overflow suggests returning a vector instead. But I explicitly require a uint16_t array as this module is part of a larger project.</p>