[comp.lang.pascal] TP Dynamic variable disposal

6600putz%ucsbuxa@hub.ucsb.edu (11/22/89)

 Does Turbo Pascal (TP compiled programs) automatically dispose of all
dynamically allocated memory created during execution of the program, or
do I, the programmer, have to manually dispose of this memory, at the
end of the program?  I have a linked-list set up and of course wish to
dispose of all the memory allocated to its use at the close of my program
after saving the data file.  If this is in the manual, I missed it...

 Scott Kral
 UCSB

unkydave@shumv1.uucp (David Bank) (11/22/89)

In article <21510@adm.BRL.MIL> 6600putz%ucsbuxa@hub.ucsb.edu writes:
>
> Does Turbo Pascal (TP compiled programs) automatically dispose of all
>dynamically allocated memory created during execution of the program, or
>do I, the programmer, have to manually dispose of this memory, at the
>end of the program?  I have a linked-list set up and of course wish to
>dispose of all the memory allocated to its use at the close of my program
>after saving the data file.  If this is in the manual, I missed it...
>
> Scott Kral
> UCSB

    My response is based on TP 3.0, as I do not (yet) have extensive
experience with TP 4.0 or 5.0 (one of these days I'll break the
shrinkwrap on the latter).

    Anyway, as I understand dynamic memory allocation on the PC,   
requests for memory are made to DOS. DOS returns a pointer to the
allocated memory or nothing if there was a problem (like no more
available memory)

    Inside your TP program, you can return allocated memory to the
heap using the Dispose() standard function. This frees memory as far
as both the program and DOS are concerned.

    If you program terminates and does not stay resident, all memory
allocated to that process (program) is immediately returned by DOS
to the free heap. Again, the program musn't stay resident. The
memory is returned to the heap regardless of whether it was
dynamically allocated or not. DOS doesn't care - it belonged to
the terminated program, now it is free memory.

    Hope this helps.

Unky Dave
unkydave@shumv1.ncsu.edu

dhinds@portia.Stanford.EDU (David Hinds) (11/23/89)

In article <4620@ncsuvx.ncsu.edu>, unkydave@shumv1.uucp (David Bank) writes:
>     Anyway, as I understand dynamic memory allocation on the PC,   
> requests for memory are made to DOS. DOS returns a pointer to the
> allocated memory or nothing if there was a problem (like no more
> available memory)
> 
>     Inside your TP program, you can return allocated memory to the
> heap using the Dispose() standard function. This frees memory as far
> as both the program and DOS are concerned.
> 
    Actually, allocating and disposing of memory within a pascal program
doesn't go through the DOS memory manager.  When your program is loaded
by DOS, it is typically given control of all available memory unless you
specify otherwise (by compiling with switches to limit memory consumption).
After allowing for the space occupied by the program, static variables,
and the stack, the rest of the memory DOS has allocated goes for TP's heap.
Allocating and disposing dynamic variables is managed by TP's runtime
library.  When a program terminates, the memory allocated when it was
initiated is automatically freed by DOS, so yes, it doesn't matter if you
forget to dispose of some variables.
    As far as entering and exiting a program are concerned, then, it doesn't
matter how the allocation is handled.  However, it is important that while
a program is running, Dispose() doesn't actually return anything to DOS's
heap.  There won't be any memory available for running another program from
within a TP program, regardless of how empty TP's heap may be.  Memory has
to be explicitly freed for this purpose, either at compile time, or by
shrinking a program's DOS memory allocation as soon as it starts to execute.
I think there is a TP procedure to do this, but I don't remember the name.
    The information isn't obvious in the manuals; I think there is an index
entry for "memory map" somewhere.
				- David Hinds
				  dhinds@portia.stanford.edu

unkydave@shumv1.uucp (David Bank) (11/24/89)

    To Mr. David Hinds:

       In reference to your response about Turbo Pascal dynamic memory
allocation, please specify which version(s) you comments applied to.
It was my understanding, and it may be in error, that dynamic memory
allocation handling underwent significant alteration between versions
3.0 and 4.0 (along with just about everything else).

       Thank you.

Unky Dave
unkydave@shumv1.ncsu.edu

dhinds@portia.Stanford.EDU (David Hinds) (11/25/89)

In article <4645@ncsuvx.ncsu.edu>, unkydave@shumv1.uucp (David Bank) writes:
> 
> It was my understanding, and it may be in error, that dynamic memory
> allocation handling underwent significant alteration between versions
> 3.0 and 4.0 (along with just about everything else).
> 
    My description applies to all versions of TP, as far as I know.  I don't
remember for sure, but I think the early TP dispose() routine wasn't capable
of reclaiming the disposed space for later calls to new().  The newer versions
maintain a list of free blocks of memory within the heap.  Heap variables are
created starting at the base of the heap, and the free list grows downward
from the top.  The early versions (3.0 or earlier?) didn't maintain a free
list.  The DOS-level memory allocation is basically the same in all versions.
In fact, for early versions of DOS, the "dispose block" routine never worked
right, and it would have been disasterous for TP to use it for heap memory
management.

David Hinds
dhinds@portia.stanford.edu

unkydave@shumv1.uucp (David Bank) (11/27/89)

   OK, thanks. I wasn't too clear on that. I started programming in
Pascal when TP 2.0 came out, but had never investigated that end of
it too closely (mainly because I have had little call, thus far, for
dynamic memory allocation in Pascal).  

Unky Dave
unkydave@shumv1.ncsu.edu