c++ - char pointer to string, then into array of strings. *** Error in `./a.out': malloc(): memory corruption: 0x0900c3b0 *** -



c++ - char pointer to string, then into array of strings. *** Error in `./a.out': malloc(): memory corruption: 0x0900c3b0 *** -

i error: * error in `./a.out': malloc(): memory corruption: 0x0900c3b0 *

i trying convert char pointer string, , set string array of strings later use. don't understand why not working. assuming string set array gets deleted, , might reason.

error happens when new string(firstbyte)

here code:

char *entries[16] = {nullptr}; string *strentries[16] = {nullptr}; char * firstbyte = 0; stringstream s; size_t len; string sfb; firstbyte = new char[sizeof(char)]; count = (firstrootdirsec*512) + 32; lseek(fd, count, seek_set); //takes 32 bytes after root directory, or first entry //so either find way read in 1 byte @ time, or //take first character of firstbyte. firstbyte[0]. that's good. for(int = 0; i<16; i++){ //check first byte //if first byte 41 or 40, long directory, , can jump ahead 32 bytes, or 0x20 lseek(fd, count, seek_set); //takes 32 bytes after read(fd, firstbyte, count); count+=32; if(firstbyte[2] != '\0'){ //then not long entry, , can set in entries. // string str(firstbyte); //error happens when new string(firstbyte) **entries[i] = firstbyte; strentries[i] = new string(firstbyte); cout<<entries[i]<<"blah"<<endl;** } }

you allocate array big plenty single byte:

firstbyte = new char[sizeof(char)];

where sizeof(char) rather convoluted way of writing 1.

then seek read more 1 byte array:

read(fd, firstbyte, count);

writing off end of array, , corrupting heap.

it looks count wrong number of bytes read here, since you've used same variable set in file seek to. need figure out how many bytes want read each time, , create sure have array big plenty that.

c++ string abort

Comments

Popular posts from this blog

java - How to set log4j.defaultInitOverride property to false in jboss server 6 -

c - GStreamer 1.0 1.4.5 RTSP Example Server sends 503 Service unavailable -

Using ajax with sonata admin list view pagination -