Advanced Data Representation and File Organisation: Question 8

Syllabus 13.1

Structured A2 8 marks

A hospital's walk-in clinic wants to create a record for a new patient only at the moment that patient actually arrives, rather than reserving space in advance for a fixed number of patients who may never show up. Once created, the system must still be able to access and update that one patient's record for the rest of the session.

This pseudocode convention is used below: NEW <type> creates space in memory for a new variable of that type and returns a pointer to it. DECLARE <name> : POINTER TO <type> declares a pointer variable that can store a reference to a variable of that type. If Ptr is a pointer variable, Ptr^ means "the variable that Ptr currently points to", and Ptr^.FieldName accesses a field of that variable through the pointer.

A record type has already been defined:

TYPE PatientRecord
    DECLARE NHSNumber : STRING
    DECLARE Name : STRING
    DECLARE Age : INTEGER
ENDTYPE

(a) State what is meant by a pointer data type. [2]

(b) Write a pseudocode declaration for PatientPtr, a pointer variable that can reference a variable of type PatientRecord. [1]

(c) Write pseudocode that uses NEW to dynamically create a new PatientRecord variable, makes PatientPtr reference it, and then sets the Name field of that new record, through PatientPtr, to "Grace Oduya". [3]

(d) State one advantage of creating this patient's record dynamically via a pointer, rather than declaring a fixed array of PatientRecord variables in advance. [2]

Show worked solution Hide worked solution

Worked solution

Part (a): What a pointer data type is

A pointer is a data type whose stored value is not a piece of data itself, but a reference to (the memory address of) another variable. Rather than accessing data directly through its own identifier, a program accesses it indirectly, by following the address stored in the pointer.

[2 marks]: [1] for stating that a pointer stores a memory address / reference to another variable, [1] for contrasting this with holding the data value directly (i.e. access is indirect, via the pointer).

Part (b): Declaring the pointer variable

A pointer variable is declared with its own data type, POINTER TO <type>, naming the type of variable it is allowed to reference:

DECLARE PatientPtr : POINTER TO PatientRecord

PatientPtr itself does not yet reference any PatientRecord. It is simply able to, once it is assigned one.

[1 mark] for the correct declaration, PatientPtr : POINTER TO PatientRecord.

Part (c): Creating a record dynamically and setting a field

NEW PatientRecord allocates space in memory for one new PatientRecord variable and returns a pointer to it, which is assigned to PatientPtr:

PatientPtr ← NEW PatientRecord
PatientPtr^.Name ← "Grace Oduya"

The first line creates the new record and makes PatientPtr reference it. The second line accesses that new record’s Name field through PatientPtr, using PatientPtr^ to mean “the PatientRecord variable that PatientPtr points to”, then .Name to reach its Name field. This is different from writing PatientPtr.Name, which would incorrectly treat the pointer itself as if it were the record.

[3 marks]: [1] for PatientPtr ← NEW PatientRecord creating the record and assigning the pointer, [1] for dereferencing with PatientPtr^ before accessing a field, [1] for correctly setting Name to "Grace Oduya".

Part (d): Advantage of dynamic creation over a fixed array

If a fixed array of PatientRecord variables were declared in advance (for example, ARRAY[1:100] OF PatientRecord), memory for all 100 records would be reserved immediately, even on a quiet day when only a handful of patients walk in. Most of that memory would sit unused.

Creating each PatientRecord dynamically, only when NEW is actually called for a patient who has just arrived, means memory is allocated only as needed, avoiding memory reserved in advance for patients who may never arrive.

[2 marks]: [1] for identifying that memory is only allocated when actually needed (when a patient arrives), [1] for contrasting this with a fixed array wasting memory reserved for patients who do not arrive.

Final answers

  • (a) A pointer stores a reference to (the memory address of) another variable, giving indirect access to it.
  • (b) DECLARE PatientPtr : POINTER TO PatientRecord
  • (c) PatientPtr ← NEW PatientRecord then PatientPtr^.Name ← "Grace Oduya"
  • (d) Memory is only allocated when a patient actually arrives, unlike a fixed array that reserves memory in advance for patients who may never need a record.