uint16_t * getRegister(std::string reg) {
// unordered_map<string, vector<uint16_t>> 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 = "4400";
uint16_t *tmp = reader.getRegister2(regname, tmp);
std::cout<<"result "<<tmp[0]<<"\t"<<tmp[1]<<std::endl;
}
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.
tmpinmain, it is a dangling pointer. If you cannot change the call sites, you have a problem. You could useuint16_t * regval = new uint16_t[current_reg_map[reg].size()];, but then you need adelete [] tmp;somewhere in the call site.datamethod to get the pointer to the underlying array (and thesizemethod to get it's size).