I am working on a commnication between two different codes and I have a problem passing arrays and vectors as follows:
1 There is an interface header file contains different functions and I want to call the following function to receive some 1d arrays from it:
void EMPIRE_API_recvMesh(char *name, int *numNodes, int *numElems, double **nodes, int **nodeIDs,int **numNodesPerElem, int **elem);
2. I have included the header file as well as the lib.a that contains the defination of all the functions and I declared the variables as follows:
1 2 3 4 5 6 7 8
char* MeshName = "Mesh";
int nNodes;
int nElements;
std::vector<double*> nodes;
std::vector<int*> nodeIDs;
std::vector<int*> numNodesPerElem;
std::vector<int*> elem;
I got no compilation errors but I got segmentation fault when I run the codes. I think the problem is related to calling this function and how to pass the vectors.
Other problem is the last four vectors are of nNodes and nNodes*3 size at the calling time but I don't know how to call them without resizing them.
You can't pass addresses of elements in a vector like that. Vectors manage their own memory, so you can't rely on the address of an element in a vector staying valid like that.
Also, please use code tags when posting code, to make it readable:
Why are you using a C wrapper around a C++ API from C++?
Looking at the source, those pointers-to-pointers are expected to be addresses of pointers that don't point anywhere in particular. The function attempts to reassign those pointers to different addresses, and if you pass it the address.
Just using [] on an empty vector is enough to invoke undefined behavior.