Assignment to Pointer-referenced Variable

General discussions about working with the Astrobe IDE and programming ARM Cortex-M0, M3, M4 and M7 microcontrollers.
Post Reply
gray
Posts: 143
Joined: Tue Feb 12, 2019 2:59 am
Location: Mauritius

Assignment to Pointer-referenced Variable

Post by gray » Sat Apr 27, 2019 11:06 am

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?

cfbsoftware
Site Admin
Posts: 525
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Assignment to Pointer-referenced Variable

Post by cfbsoftware » Sat Apr 27, 2019 11:49 am

No. You need to call NEW(p) explicitly before the assignment.

gray
Posts: 143
Joined: Tue Feb 12, 2019 2:59 am
Location: Mauritius

Re: Assignment to Pointer-referenced Variable

Post by gray » Sat Apr 27, 2019 1:25 pm

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).

cfbsoftware
Site Admin
Posts: 525
Joined: Fri Dec 31, 2010 12:30 pm
Contact:

Re: Assignment to Pointer-referenced Variable

Post by cfbsoftware » Sat Apr 27, 2019 11:28 pm

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.

Post Reply