Wednesday, January 6, 2010

Why a programmer need to take care of freeing the memory?

Why a programmer need to take care of freeing the memory?


If you have a subroutine like this

int subroutine(int param){
char example_variable [200];

whatever the routine does....

}

is necessary to release the memory used by the variable example_variable??? or is this automatically released when the variable get out of scope???


variables are created in different spaces within a program.
the example[100] variable would be created within a stack which would be deleted as soon as the program terminates so the programmer doesn't need to take care of freeing that. but any address dynamically obtained is obtained from the heap which is a pool of memory of all the programs running and not specific to one. so a programmer needs to take care of freeing or deleting (basically returning the space back to the pool) when the purpose of that variable is served and no longer required.

2 comments: