You must submit this lab at the end of next tutorial.
For someone, who has difficulties to encode the program handed in the labs. At the end of this tutorial you have to understand how dynamic memory works and what the dynamic memory allocation do.
Encode the following programs in C, execute it and define what do they perform.
NOTES:
1. dispose() is analog of delete operator in Pascal.
2. nil in Pascal is NULL in C.
PROGRAM 1:
program pointers3( output );
type int_pointer = ^integer;
var iptr1, iptr2 : int_pointer;
begin
new( iptr1 );
new( iptr2 );
iptr1^ := 10;
iptr2^ := 25;
writeln('the value of iptr1 is ', iptr1^);
writeln('the value of iptr2 is ', iptr2^);
dispose( iptr1 );
iptr1 := iptr2;
iptr1^ := 3;
writeln('the value of iptr1 is ', iptr1^);
writeln('the value of iptr2 is ', iptr2^);
dispose( iptr2 );
end.
PROGRAM 2:
program PointerRecordExample( output );
type rptr = ^recdata;
recdata = record
number : integer;
code : string;
nextrecord : rptr
end;
var startrecord : rptr;
begin
new( startrecord );
if startrecord = nil then
begin
writeln('1: unable to allocate storage space');
exit
end;
startrecord^.number := 10;
startrecord^.code := 'This is the first record';
new( startrecord^.nextrecord );
if startrecord^.nextrecord = nil then
begin
writeln('2: unable to allocate storage space');
exit
end;
startrecord^.nextrecord^.number := 20;
startrecord^.nextrecord^.code := 'This is the second record';
new( startrecord^.nextrecord^.nextrecord );
if startrecord^.nextrecord^.nextrecord = nil then
begin
writeln('3: unable to allocate storage space');
exit
end;
startrecord^.nextrecord^.nextrecord^.number := 30;
startrecord^.nextrecord^.nextrecord^.code := 'This is the third record';
startrecord^.nextrecord^.nextrecord^.nextrecord := nil;
writeln( startrecord^.number );
writeln( startrecord^.code );
writeln( startrecord^.nextrecord^.number );
writeln( startrecord^.nextrecord^.code );
writeln( startrecord^.nextrecord^.nextrecord^.number );
writeln( startrecord^.nextrecord^.nextrecord^.code );
dispose( startrecord^.nextrecord^.nextrecord );
dispose( startrecord^.nextrecord );
dispose( startrecord )
end.
PROGRAM 3:
program PointerRecordExample2( output );
type rptr = ^recdata;
recdata = record
number : integer;
code : string;
nextrecord : rptr
end;
var startrecord, listrecord : rptr;
begin
new( listrecord );
if listrecord = nil then
begin
writeln('1: unable to allocate storage space');
exit
end;
startrecord := listrecord;
listrecord^.number := 10;
listrecord^.code := 'This is the first record';
new( listrecord^.nextrecord );
if listrecord^.nextrecord = nil then
begin
writeln('2: unable to allocate storage space');
exit
end;
listrecord := listrecord^.nextrecord;
listrecord^.number := 20;
listrecord^.code := 'This is the second record';
new( listrecord^.nextrecord );
if listrecord^.nextrecord = nil then
begin
writeln('3: unable to allocate storage space');
exit
end;
listrecord := listrecord^.nextrecord;
listrecord^.number := 30;
listrecord^.code := 'This is the third record';
listrecord^.nextrecord := nil;
while startrecord <> nil do
begin
listrecord := startrecord;
writeln( startrecord^.number );
writeln( startrecord^.code );
startrecord := startrecord^.nextrecord;
dispose( listrecord )
end
end.