Gamestudio Links
Zorro Links
Newest Posts
Trading Journey
by howardR. 04/24/24 20:04
M1 Oversampling
by Petra. 04/24/24 10:34
Zorro FIX plugin - Experimental
by flink. 04/21/24 07:12
Data from CSV not parsed correctly
by EternallyCurious. 04/20/24 21:39
Scripts not found
by juergen_wue. 04/20/24 18:51
zorro 64bit command line support
by 7th_zorro. 04/20/24 10:06
StartWeek not working as it should
by jcl. 04/20/24 08:38
folder management functions
by VoroneTZ. 04/17/24 06:52
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
2 registered members (AndrewAMD, howardR), 472 guests, and 7 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Mega_Rod, EternallyCurious, howardR, 11honza11, ccorrea
19048 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
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 Offline OP
Senior Member
NeoNeper  Offline OP
Senior Member

Joined: Nov 2007
Posts: 318
Brasil, Paraná
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 ?


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
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

Joined: May 2013
Posts: 23
Somewhere
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!)).


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 Offline OP
Senior Member
NeoNeper  Offline 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
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

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 Offline OP
Senior Member
NeoNeper  Offline OP
Senior Member

Joined: Nov 2007
Posts: 318
Brasil, Paraná
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.


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
W
Wiseguy Offline
Newbie
Wiseguy  Offline
Newbie
W

Joined: May 2013
Posts: 23
Somewhere
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).


His words are wise, his face is beard.

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1