c++ - Insert a pointer into raw memory -
i working on memory manager school assignment. jist of assignment allocate large character array, split array blocks client can "allocate" or "free."
part of assignment involves header blocks, little block of information before each block block. tracks information block. 1 of types of header blocks need implement external header block. external header block, instead of storing header data in block itself, need dynamically allocate struct header data. problem further complicated struct having dynamically allocated label.
my teacher's driver getting null when tries access data, , when delete data, throws access violation, because i'm not storing data in raw memory properly.
in essence, problem is: need dynamically allocate struct dynamically allocated string , put pointer struct in first 8 bytes of block of raw memory.
here current implementation
char* newstring = new char[strlen(inputstring)]; //copy label passed fx allocated memory strcpy(newstring, inputstring); //allocate , initalize struct headerstruct* pheader = new headerstruct; pheader->active = true; pheader->string = newstring; //save first 8 bytes of block of memory headerstruct* headerstruct* haddress = reinterpret_cast<headerstruct(*)>(address); //make first 8 bytes point allocated struct haddress = pheader;
if i'm following right, headerloc in allocation needs pointer pointer, since need store pointer memory block @ address:
memblockinfo** headerloc = reinterpret_cast<memblockinfo **>(address); then dereference store header there.
Comments
Post a Comment