0 registered members (),
905
guests, and 3
spiders. |
Key:
Admin,
Global Mod,
Mod
|
|
|
return vector<string> from C++ DLL, HOW?
#422737
05/15/13 22:11
05/15/13 22:11
|
Joined: Nov 2007
Posts: 318 Brasil, Paraná
NeoNeper
OP
Senior Member
|
OP
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, Paraná
|
HELLO BROTHERS. partial code on C++
...
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.
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 ?
Please! Use easy words to be translated. because my English is not very good! Grateful. _______________________________________________________
|
|
|
Re: return vector<string> from C++ DLL, HOW?
[Re: NeoNeper]
#422738
05/15/13 22:17
05/15/13 22:17
|
Joined: May 2013
Posts: 23 Somewhere
Wiseguy
Newbie
|
Newbie
Joined: May 2013
Posts: 23
Somewhere
|
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!)).
His words are wise, his face is beard.
|
|
|
Re: return vector<string> from C++ DLL, HOW?
[Re: Wiseguy]
#422771
05/16/13 14:30
05/16/13 14:30
|
Joined: Nov 2007
Posts: 318 Brasil, Paraná
NeoNeper
OP
Senior Member
|
OP
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, Paraná
|
Hi Wiseguy, tanks brother. ITS
&result[0]; WORK FINE. (^.^)
Please! Use easy words to be translated. because my English is not very good! Grateful. _______________________________________________________
|
|
|
Re: return vector<string> from C++ DLL, HOW?
[Re: NeoNeper]
#422773
05/16/13 14:57
05/16/13 14:57
|
Joined: May 2013
Posts: 23 Somewhere
Wiseguy
Newbie
|
Newbie
Joined: May 2013
Posts: 23
Somewhere
|
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!
His words are wise, his face is beard.
|
|
|
Re: return vector<string> from C++ DLL, HOW?
[Re: Wiseguy]
#423167
05/24/13 22:07
05/24/13 22:07
|
Joined: Nov 2007
Posts: 318 Brasil, Paraná
NeoNeper
OP
Senior Member
|
OP
Senior Member
Joined: Nov 2007
Posts: 318
Brasil, Paraná
|
Ok. i CHANGE. Look now: ON C++
char* teste = (char*)result[r][c].c_str(); //result[index row][index column]
return teste;
ON LITE C
char *test= sqlite_select(1,2);
printf("%s",test); //Its WORK.
Now this correct? Tankz brother.
Please! Use easy words to be translated. because my English is not very good! Grateful. _______________________________________________________
|
|
|
Re: return vector<string> from C++ DLL, HOW?
[Re: NeoNeper]
#423225
05/25/13 18:19
05/25/13 18:19
|
Joined: May 2013
Posts: 23 Somewhere
Wiseguy
Newbie
|
Newbie
Joined: May 2013
Posts: 23
Somewhere
|
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).
His words are wise, his face is beard.
|
|
|
|