ok...i always wanted to know this deeply..
whats the difference between static and off the heap memory...?Code:int number = 5; //stores in static memory int* pnum = new int; *pnum = 5; //stores off the heap
ok...i always wanted to know this deeply..
whats the difference between static and off the heap memory...?Code:int number = 5; //stores in static memory int* pnum = new int; *pnum = 5; //stores off the heap
nextus, the samurai warrior
static memory is allocated on the stack while dynamic is on the heap. how this works exactly depends on the OS but usually the stack starts right before Kernel space and grows downwards. the heap will start at a lower address and grow upward (towards the stack) when a function or subroutine is called it will allocate its neccessary stack space (referenced by a frame pointer) and all local 'static' vars are stored there. because the program does not allways know how much space to allocate for dynamic variables, it does not use stack space for this, instead, each dynamically allocated variable will be stored in the heap with a reference (the pointer that 'new' returns) to the memory address. when a subroutine finishes, its stack space is no longer needed so the frame pointer is moved back and the stack space can be reused. however, memory on the heap is not managed this way which is why you must explicitly need to delete dynamically allocated variables.
ive left alot out but i wasnt sure how much info you wanted.. hope that helps.