c++ - Are character pointer allowed in python? -
i using c++ based library return type char pointer. calling c++ function within python.
my question can delete memory allocated in c++ function python?
python code
lib.taggetname.restype = c_char_p lib.taggetname.argtypes=[c_int] data = 1 cmt = (b"this comment voltage") result = lib.taggetname(data)
python output
kernel process terminated restart. (0) python 3.4.3 (v3.4.3:9b73f1c3e601, feb 24 2015, 22:43:06) on windows (32 bits). iep interpreter. type 'help' help, type '?' list of *magic* commands.
running script: "d:\qtdev\test.py"
1 select stagname tagdatainfo itagid = 1; return value--> b'voltage\xfd\xfd\xfd\xfd\xdd`\x18x' print ("return value-->" , result)
c++ function code
use_math char* taggetname(int itagid) { char* cname; //string cname = ""; sqlite3_stmt *stmtcreate = null; sqlite3 *m_pdbobj; cout<<itagid<<endl; if (sqlite3_open_v2("d:\\qtdev\\forpythondll\\neha_test_use\\tagdata", &m_pdbobj, sqlite_open_readwrite, null) != sqlite_ok) { return cname; } char cquery[1024] = {0}; sprintf(cquery,"select stagname tagdatainfo itagid = %d;",itagid); cout<<cquery<<endl; if (sqlite3_prepare_v2(m_pdbobj, cquery, -1, &stmtcreate, 0) == sqlite_ok) { sqlite3_step(stmtcreate); if(sqlite3_column_text(stmtcreate, 0)) { cname = new char[strlen((char*)sqlite3_column_text(stmtcreate, 0))+1]; memset(cname,0x00,(strlen((char*)sqlite3_column_text(stmtcreate, 0))+1)); memcpy(cname,(char*)sqlite3_column_text(stmtcreate, 0),strlen((char*)sqlite3_column_text(stmtcreate, 0))); cout<<"cname="<<cname<<endl; cout<<"database name="<<(char*)sqlite3_column_text(stmtcreate, 0)<<endl; } sqlite3_finalize(stmtcreate); } else { return cname; } sqlite3_exec(m_pdbobj, "commit", 0, 0, 0); return cname; }
in python can't delete memory being allocated in c++. have export function c++ wrap free()/delete call, , call python.
as aside, remember python using utf-16 , c++ may using utf-8, , string conversion needed ( ctype can )
Comments
Post a Comment