Convert std :: list to friendly type C

What is the most elegant way to return a std::list object from a generic lib function (implemented by C ++ code) to a C consumer? I know that for std::vector we can return the address of the 1st element of the vector and ask him to consider it as an array, but std :: list is implemented as a bound lis.

+6
c ++ c stl
source share
6 answers

Copy std::list to std::vector and return the address of the first element, as you already mentioned.

(Of course, this may mean that you do not want to use std::list in the first place.)

(This solution assumes that access to the object belongs to the C ++ library. If this is not the case, you may need to allocate memory from your C code and pass a pointer to the C ++ library to copy the data.)

+9
source share

you can always make a copy:

 list<int> lst; int *arr = new int[lst.size()]; copy(lst.begin(),lst.end(),arr); 
+4
source share

If you want client code to manipulate the list, you need to define type C, for example:

 struct Node { int foo; float bar; // more data struct Node* next; }; 

and return the pointer to the head of the list:

 struct Node* getData(); 

Which basically means that you will need to copy the contents of your std::list into a data structure of type C if you want client code to manipulate the list.

Otherwise, you can copy the contents of your std::list to an adjacent memory block, return this block to the caller, but in this case, the caller is responsible to free the memory. Returning the array also means that memory cleaning should be done using a function compatible with the function that you used to allocate the block: your implementation will most likely use malloc rather than new to allocate this block, so that the caller can later use free on block.

+2
source share

Try something like this, no need to use std::copy if you don't want to use it:

 std::list<int> aList; alist.push_back(1); alist.push_back(2); alist.push_back(3); std::vector<int> aVector(alist.begin(), alist.end()); cFunction(&aVector[0], aVector.size()); 
+2
source share

The only way to do this is to return void * in obejct. Then create a set of functions that accept void * and manipulate the list in C ++ code.

Edit:

For those who go Ehhh.

 std::list<int> plop; void* getPlopList() { retutn &plop; } void appendToCPPList(void* list,int val) { static_cast<std::list<int>*>(list)->push_back(val); } // Dont forget to declare the functions extern "C" 
+2
source share

You can get an iterator or list and fill it with vector:

 std::list<Node> nodes; // .. fill nodes std::vector<node> nodeVector; std::copy(nodes.begin(); nodes.end(); std::back_inserter(nodeVector)); 

And then you can work with it in the same way as with a vector with which you are familiar.

0
source share

All Articles