View Single Post
  #4 (permalink)  
Old June 15th, 2005, 03:14 AM
Alan-LB Alan-LB is offline
Authorized User
 
Join Date: Mar 2005
Posts: 58
Thanks: 0
Thanked 1 Time in 1 Post
Send a message via MSN to Alan-LB Send a message via Yahoo to Alan-LB
Default

Local variables are allocated on stack memory (the stack frame for the function)only when the function starts and the memory is automatically released when the function returns. This means that there are not large chunks of memory allocated that may not be used at the time.

Global variables are allocated memory in the Data Segment when the program loads and stay allocated for the whole life of the program. This may be OK if you have variables that are used by many functions and if it makes sense to have them as Globals. In general, global variables should be avoided if it is reasonable to do so.

You may also allocate Global (outside the main() function)Dynamic Variables on the Heap memory using the "new" function and the memory must be released by using the "delete" function when the program terminates. Dynamic Variables may also be allocated from within any function but they must be released before leaving the function otherwise they memory stays allocated but you may not access it because it is no longer in scope. This is a "memory leak".

Alan


Reply With Quote