receiving vectors of unknow size in function call

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;

then I called that function as follows:

EMPIRE_API_recvMesh(MeshName,&nNodes,&nElements,&nodes[0],&nodeIDs[0],&numNodesPerElem[0],&elem[0])

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.

I hope the someone can help me
best regards
Last edited on
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:

http://www.cplusplus.com/articles/z13hAqkS/
Thank you MikeyBoy, I am new here and I did not know that. I edited it using code tags.

If I can't pass addresses, do you know how to make it works?

How to call this function to receive its arrguments?
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.

The C++ code called by the C API:

http://empire.st.bv.tum.de/projects/empire/repository/revisions/master/entry/EMPIRE_API/src/Empire.cpp


Topic archived. No new replies allowed.