Contents    Page-10    Prev    Next    Page+10    Index   

Memory Management

Many languages can allocate heap storage at runtime.

type person = record ... end;
var p: ^person;
The compiler must insert the size of the person record into the call to new. If type safety is to be guaranteed, it may be necessary to initialize the new record.

For our assignments, we will assume the following conversion:

new(p) p := new(sizeof(p^))

For example, source code new(p) would be converted to intermediate code (:= p (funcall new 40)), where 40 is the size of the record type p points to.

Pascal uses the form new(p) and prohibits expressions involving pointers in order to prohibit pointer arithmetic (which destroys type safety). C allows pointer arithmetic; in C, malloc is called with the record size as a parameter, and a type cast is used to set the result type.