return vector<string> from C++ DLL, HOW?

Posted By: NeoNeper

return vector<string> from C++ DLL, HOW? - 05/15/13 22:11

HELLO BROTHERS.


partial code on C++
Code:
...
vector<vector<string> > result = db->query("SELECT a, b FROM a;");
for(vector<vector<string> >::iterator it = result.begin(); it < result.end(); ++it)
{
	vector<string> row = *it;
	cout << "Values: (A=" << row.at(0) << ", B=" << row.at(1) << ")" << endl;
}



Ok. But I need to change this looping for liteC.

Code:
vector<vector<string> > result;

DLLFUNC void* sqlte3_feetArray()
{

//Simple sample
result = db->query("SELECT a, b FROM a;");
return &result;

}



that way I can capture this vector return ?
Posted By: Wiseguy

Re: return vector<string> from C++ DLL, HOW? - 05/15/13 22:17

Code:
return &result[0];



Or, if you work with C++11, you can also use the data() method on the std::vector<>. Keep in mind, unlike std::deque or the like, std::vector is guaranteed to allocate a contiguous memory block!

Though, there is one problem... First of all, Lite-C doesn't know about std::string, and second of all... Well, you are using an auto variable there, which goes out of scope once you return from the function.

A better way to handle these kinds of things, is letting the user supply a buffer and a size to the function, and then filling the buffer in your method with the designated data type.


(By the way, in case you use C++11: Use the new auto type for your loop, and use >> in your template. And just in general, it looks like you've put "using namespace std" somewhere... Don't do that, "using namespace" is inherently flawed, avoid it and just type the "std::" (and if you have to use it, make sure that the translation unit is private!)).
Posted By: NeoNeper

Re: return vector<string> from C++ DLL, HOW? - 05/16/13 14:30

Hi Wiseguy, tanks brother.
ITS

&result[0]; WORK FINE. (^.^)
Posted By: Wiseguy

Re: return vector<string> from C++ DLL, HOW? - 05/16/13 14:57

No, it doesn't! result is an auto variable! Once it goes out of scope, the object will be deleted and your function will return a dangling pointer!
Posted By: NeoNeper

Re: return vector<string> from C++ DLL, HOW? - 05/24/13 22:07

Ok. i CHANGE.
Look now:

ON C++
Code:
char* teste = (char*)result[r][c].c_str(); //result[index row][index column]
 return teste;



ON LITE C
Code:
char *test= sqlite_select(1,2);
printf("%s",test); //Its WORK.



Now this correct? Tankz brother.
Posted By: Wiseguy

Re: return vector<string> from C++ DLL, HOW? - 05/25/13 18:19

Originally Posted By: NeoNeper
Now this correct?

Definitely not. The pointer returned by c_str() belongs to the hosting std::string, and it becomes invalid when calling a non const member function or upon the destructor call. The second your string goes out of scope, the returned pointer becomes invalid. Please read up on RAII and auto variables in C++.
Also, please use new style casts in C++, and don't cast away constness if you don't need to (and if you need to, keep in mind that altering any character of the returned string results in undefined behaviour!).

There simply is no way around making a copy of the string, though, and I want to stress this right now, allocating memory in one library and freeing it in another doesn't work. In debug builds, an assertion will be raised, in release builds all you get is a memory leak (actually, it does work, but you'd need control over the generation of both binaries, not just one. So just assume it doesn't work).
© 2024 lite-C Forums