Friday, 15 March 2013

c++ - memcpy extremely unexpected behavior -



c++ - memcpy extremely unexpected behavior -

i'm using memcpy re-create info , array getting written on disk. i'm storing a char* of 256 bytes (name array)*, 3 long numbers, , a short number. thought going work correctly until tried , didn't. when saving info disk, seems work fine. when trying read off information, memcpy malfunctions, leaving variable straight after name array @ 0, , when programme needs load else, end seg fault.

at first thought info could've been getting corrupted when written disk, or when beingness loaded it, couldn't find huge issues saving or loading procedures, did next print out every single byte on array (with qt's qdebug), see if spot wrong it.

needless say, didn't work out planned since qdebug printed non null characters, , "name array" has lot of those, did ridiculously surprising: somehow made memcpy work , not work, since re-create first long number, corrupt sec long number. thing is, whole thing pretty much unrelated!

char* block = new char[freeblock::tablamtd_v]; //3958 bytes long fread.seekg(toload, ios::beg); //toload beingness unsigned long file pointer, fread beingness working ifstream fread.read(block, freeblock::tablamtd_v); //reading 3958 bytes block fread.close(); short bsize = 0, pos = 0; memcpy(&bsize, block, 2); pos+=2; for(short = 0; < bsize; i++){ char* tname = new char[257]; unsigned long ptrmtd = 0, ptrdt = 0, ptrind = 0; unsigned short indmtd = 0; memcpy(tname, &block[pos], 256); tname[256] = 0; pos+=256; memcpy(&ptrmtd, &block[pos], 8); //without qdebug loop, stays @ 0. otherwise, has right value pos+=8; memcpy(&ptrdt, &block[pos], 8); //with qdebug loop, stays @ 0. otherwise, has right value pos+=8; memcpy(&ptrind, &block[pos], 8); pos+=8; memcpy(&indmtd, &block[pos], 2); pos+=2; for(int = 0; < freeblock::tablamtd_v; i++){ qdebug()<<block[i]; //miracle solution, somehow } campomtdb* cmtd = nextcampomtdb(ptrmtd, true); //based on ptrmtd, if 0, returns null pointer unsigned long nextcmtdb = cmtd->next; //seg fault due null pointer

since loop thing have added, , commenting it/uncommenting changes outcome of entire procedure, i'm quite baffled. sort of sets new record me on "unexpected behavior" plane.

taking consideration i'm new c++, , i've until heard of random unsafe practices i'm using here apparently should not using, still don't see how or why adding random loop after memcpy operations have impact whatsoever on these operations, or why memcpy wouldn't work should in first place.

also note after saved file, didn't touch again, hasn't changed @ all, yet output reading changes.

in case helps know, i'm using vc2013 in qt 5.3, x64 windows 7

since you're using vc2013 under windows 7, size of unsigned long 4 bytes (not 8 bytes).

and in case, own safety, should utilize sizeof instead of constant values of 8 , 2.

change this:

memcpy(&ptrmtd, &block[pos], 8); pos+=8; memcpy(&ptrdt, &block[pos], 8); pos+=8; memcpy(&ptrind, &block[pos], 8); pos+=8; memcpy(&indmtd, &block[pos], 2); pos+=2;

to this:

memcpy(&ptrmtd, &block[pos], sizeof(ptrmtd)); pos+=sizeof(ptrmtd); memcpy(&ptrdt, &block[pos], sizeof(ptrdt)); pos+=sizeof(ptrdt); memcpy(&ptrind, &block[pos], sizeof(ptrind)); pos+=sizeof(ptrind); memcpy(&indmtd, &block[pos], sizeof(indmtd)); pos+=sizeof(indmtd);

c++

No comments:

Post a Comment