[fpc-pascal] pointer magic

spir ☣ denis.spir at gmail.com
Tue Apr 27 10:16:37 CEST 2010


Hello,

Two basic questions about pointer targets (I call target what is pointed to).

-1- untyped pointer allocation
In case of an untyped pointer, I read the compiler considers the target'size is 1 byte. There certainly is an operation to allocate memory for the target according to an existing piece of data. Something like
    data := something
    new(p,data);
    // Either p points to data where it was
    // or a new memory area is silently reserved,
    // data copied there, and p holds its address.

-2- reflexive pointing
The following works, but I do not understand how.

======= code =======
program LinkedList;

uses
    Classes, SysUtils;  //, Variants;

// typed list version
type
    List = ^Node;
    Node = record
        next : List;
        data : ^Integer;
    end;

var
    endNode : List;

procedure defineEndNode();
begin
    new(endNode);
    with endNode^ do begin
        new(data);
        data^ := 0;
        next := endNode;
    end;
end;

//[...]

begin
    // end node flag
    defineEndNode();
    writeln(endNode^.data^);
    //[...]
end.
======= /code ======

The issue is: ^endNode holding a pointer to endNode, how is this kind of infinite self-referencing loop resolved by the compiler, at definition time?

PS: How else terminate a linked list in Pascal? (I must put something in the .next field of the last actual node, and this thing must be a List, ie a node pointer.)


Denis
________________________________

vit esse estrany ☣

spir.wikidot.com



More information about the fpc-pascal mailing list