[Return to Bookshelf] [Contents] [Previous Section] [Next Section] [Index] [Help]


3.3.1.3 Heap

Dynamic memory is allocated by the program as a result of a call to some memory management function (for example, the C language run-time function malloc() or the OpenVMS common run-time function LIB$GET_VM).

Dynamic memory is referenced through pointer variables. Although the pointer variables are scoped depending on their declaration, the dynamic memory itself has no intrinsic scope or lifetime. It can be accessed from any routine or thread that is given its address and will exist until explicitly made free. In a language supporting automatic garbage collection, it will exist until the run-time system detects that there are no references to it. (If your language supports garbage collection, be sure the garbage collector is thread-safe.)

Heap is usually appropriate to manage persistent context. For example, in a thread-reentrant routine that is called multiple times to return a stream of information (for example, to list all active connections to a server or to return a list of users), using dynamic memory allows multiple contexts that are independent of threads. Multiple threads may be able to share a given context, or a single thread may have more than one context.

The scope of dynamic memory is anywhere a pointer containing the address can be referenced. The lifetime is from allocation to deallocation.