c++ - Allocate memory for huge node tree dynamically -
i'm trying make function allocates memory in blocks , able assign memory pointer different structures linked together.
#define memsize 50*1024*1024*sizeof(char) #include "globals.h" void *addblock(void){ memstart = (char*) calloc(1,memsize); if(memstart==null){ printf("hittade inte minne...:\n"); getchar(); throw 1; } memptr = memstart; return memstart; } void* getspace(size_t size){ //gör nytt block eller putta fram pekaren //makes new block or increases ptr void *tmp = null;//where data should stored if(( memptr+size+1 >= memstart+memsize) ) tmp = addblock(); else { tmp = memptr; memptr+=size; } return tmp; } void initmem(void){ //init of memory globals addblock(); }
memptr
, memstart
extern char*
. memstart
start of block , memptr
at. initmem
run in main
upon start.
globals .h
extern char *memstart; extern char *memptr;
globals .cpp
char *memstart; char *memptr;
e.g. struct node* thenode = getspace(sizeof(struct node));
but code works bad , gives lot of glitches in program.
is there common way this? when allocate memory each structure malloc there way overhead , that's huge deal since tree consists of millions of nodes.
this leg of code has problem
if(( memptr+size+1 >= memstart+memsize) ) tmp = addblock();
because not advancing memptr size.
Comments
Post a Comment