Hello.
I guess this is a c++ issue, but I don’t know where to post my question, so here it is.
I’m trying to use a vector in a cgo binding, but the first index of output always show weird data like below. structs is cgo binary and company.exe is cpp binary.
I am not a C++ programmer, so I may be wrong here, but I think this might be related to the vector itself going out of scope at the end of generate_employee_list_vector at which point the vector (and its backing array) are destroyed, so when you use emp_list later, I think you’re experiencing undefined behavior. I see you have a second version that uses malloc; are you seeing the same thing there?
No, there is no transfer of ownership of the array from the vector to your employee_list. The data member function returns an “unowned” pointer that can be used while the vector exists. There is no way that I am aware of to move the vector’s backing array out of the vector.
I would recommend putting the vector into the company so that its lifetime is preserved as long as the company exists. I submitted a pull request with the changes that I would suggest if you want to use the vector. I am not 100% certain that I got it right, but it did work for me.