c++ - Problem when converting char* to string -
when convert char* string gives bad memory allocation error in 'new.cpp' . used following method convert char* called 'strdata' , 'strorg' string.
const char* strdata = dt.data(); int length2 = dt.length(); string s1(strdata);
first time work without problem. in second convertion gives above error. when swap 2 conversion in order, give error in second conversion regardless of char* converting. whole code shown in following.
mysqlpp::query query = conn.query("select data,origin image id =2"); mysqlpp::usequeryresult res = query.use(); mysqlpp::row eee= res.fetch_row(); mysqlpp::row::reference dt = eee.at(0); mysqlpp::row::reference org = eee.at(1); const char* strdata = dt.data(); int length2 = dt.length(); string s1(strdata); istringstream is1(s1); char * imgdata = new char; is1.read(reinterpret_cast<char *> (imgdata), length2); delete [] strdata; const char* strorg = org.data(); int length3 = org.length(); string s2(strorg); istringstream is2(s2); char * imgorg = new char; is2.read(reinterpret_cast<char *> (imgorg), length3); delete [] strorg;
this error comes from
void *__crtdecl operator new(size_t size) _throw1(_std bad_alloc) { void *p; while ((p = malloc(size)) == 0) if (_callnewh(size) == 0) { // report no memory static const std::bad_alloc nomem; _raise(nomem); } return (p); }
please me solve problem. urgent
instead of
char * imgdata = new char; is1.read(reinterpret_cast<char *> (imgdata), length2);
try
char * imgdata = new char[length2]; is1.read(reinterpret_cast<char *> (imgdata), length2);
when read data istringstream
using read
, buffer provide must have enough space hold results!
if call new char;
space 1 char
. use new char[n];
space n.
Comments
Post a Comment