c++ - Allocate chunk of memory for array of structs -
i need array of struct allocated in 1 solid chunk of memory. length of "char *extension" , "char *type" not known @ compile time.
struct mimetype { char *extension; char *type; };
if used "new" operator initialize each element itself, memory may scattered. how tried allocate single contiguous block of memory it:
//numtypes = total elements of array //maxextension , maxtype needed lengths (char*) in struct //std::string ext, type; unsigned int size = (maxextension+1 + maxtype+1) * numtypes; mimetypes = (mimetype*)heapalloc(getprocessheap(), heap_zero_memory, size);
but, when try load data in this, data out of order , scattered when try access later.
for(unsigned int = 0; < numtypes; i++) { //get data file getline(fin, line); stringstream parser.str(line); parser >> ext >> type; //point pointers @ spot in memory allocated mimetypes[i].extension = (char*)(&mimetypes[i]); mimetypes[i].type = (char*)((&mimetypes[i]) + maxextension); //copy data elements strcpy(mimetypes[i].extension, ext.c_str()); strcpy(mimetypes[i].type, type.c_str()); }
can me out?
edit:
unsigned int size = (maxextension+1 + maxtype+1); mimetypes = (mimetype*)heapalloc(getprocessheap(), heap_zero_memory, size * numtypes); for(unsigned int = 0; < numtypes; i++) strcpy((char*)(mimetypes + (i*size)), ext.c_str()); strcpy((char*)(mimetypes + (i*size) + (maxextension+1)), type.c_str());
i'll put aside point premature optimization (and ought use std::string, std::vector, etc), since others have stated that.
the fundamental problem i'm seeing you're using same memory both mimetype structs , strings they'll point to. no matter how allocate it, pointer , data points cannot occupy exact same place in memory.
lets needed array of 3 types , had mimetype* mimetypes
pointing memory allocated them.
that means you're treating memory if contains:
8 bytes: mime type 0 8 bytes: mime type 1 8 bytes: mime type 2
now, consider you're doing in next line of code:
mimetypes[i].extension = (char*)(&mimetypes[i]);
extension
being set point same location in memory mimetype struct itself. not going work. when subsequent code writes location extension
points to, overwrites mimetype structs.
similarly, code:
strcpy((char*)(mimetypes + (i*size)), ext.c_str());
is writing string data in same memory otherwise want mimetype structs occupy.
if want store necessary memory in 1 contiguous space, doing bit more complicated. need allocate block of memory contain mimetype array @ start of it, , string data afterwards.
as example, lets need 3 types. lets max length extension string (maxextension) 3 , max length type string (maxtype) 10. in case, block of memory needs laid out as:
8 bytes: mime type 0 8 bytes: mime type 1 8 bytes: mime type 2 4 bytes: extension string 0 11 bytes: type string 0 4 bytes: extension string 1 11 bytes: type string 1 4 bytes: extension string 2 11 bytes: type string 2
so allocate, setup, , fill correctly want like:
unsigned int mimetypestringssize = (maxextension+1 + maxtype+1); unsigned int totalsize = (sizeof(mimetype) + mimetypestringssize) * numtypes; char* data = (char*)heapalloc(getprocessheap(), heap_zero_memory, totalsize); mimetype* mimetypes = (mimetype*)data; char* stringdata = data + (sizeof(mimetype) * numtypes); for(unsigned int = 0; < numtypes; i++) { //get data file getline(fin, line); stringstream parser.str(line); parser >> ext >> type; // set pointers proper locations mimetypes[i].extension = stringdata + (mimetypestringssize * i); mimetypes[i].type = stringdata + (mimetypestringssize * i) + maxextension+1; //copy data elements strcpy(mimetypes[i].extension, ext.c_str()); strcpy(mimetypes[i].type, type.c_str()); }
(note: i've based byte layout explanations on typical behavior of 32-bit code. 64-bit code have more space used pointers, principle same. furthermore, actual code i've written here should work regardless of 32/64-bit differences.)
Comments
Post a Comment