Page 1 of 1

Assignment to Pointer-referenced Variable

Posted: Sat Apr 27, 2019 11:06 am
by gray

Code: Select all

MODULE M;
  TYPE
    P = POINTER TO T;
    T = RECORD END;

  VAR
    p: P;
    t: T;

BEGIN
  p^ := t
END M.
Is here NEW(p) executed before the assignment?

Re: Assignment to Pointer-referenced Variable

Posted: Sat Apr 27, 2019 11:49 am
by cfbsoftware
No. You need to call NEW(p) explicitly before the assignment.

Re: Assignment to Pointer-referenced Variable

Posted: Sat Apr 27, 2019 1:25 pm
by gray
Then again...

Code: Select all

MODULE M;
  IMPORT Main, Out;
    
  TYPE
    P = POINTER TO T;
    T = RECORD
      i: INTEGER 
    END;

  VAR
    p: P;
    t: T;

BEGIN
  t.i := 13;
  p^ := t;
  Out.Int(p.i, 0); Out.Ln;
  p.i := 4;
  Out.Int(p.i, 0); Out.Ln
END M.
... compiles and executes (prints 13 and 4).

Re: Assignment to Pointer-referenced Variable

Posted: Sat Apr 27, 2019 11:28 pm
by cfbsoftware
You just happened to be unlucky because your result may have led you to a false conclusion.

On my Cortex-M4 board your example compiles and executes and prints:

537066800
537066800

My conclusion is that the program is incorrect and its behaviour is unpredictable.