Assignment to Pointer-referenced Variable

General discussions about working with the Astrobe IDE and programming ARM Cortex-M3, M4 and M7 microcontrollers.

Assignment to Pointer-referenced Variable

Postby 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?
gray
 
Posts: 51
Joined: Tue Feb 12, 2019 2:59 am

Re: Assignment to Pointer-referenced Variable

Postby cfbsoftware » Sat Apr 27, 2019 11:49 am

No. You need to call NEW(p) explicitly before the assignment.
cfbsoftware
Site Admin
 
Posts: 395
Joined: Fri Dec 31, 2010 12:30 pm

Re: Assignment to Pointer-referenced Variable

Postby 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).
gray
 
Posts: 51
Joined: Tue Feb 12, 2019 2:59 am

Re: Assignment to Pointer-referenced Variable

Postby 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.
cfbsoftware
Site Admin
 
Posts: 395
Joined: Fri Dec 31, 2010 12:30 pm


Return to Astrobe for ARM Cortex-M3, M4 and M7

cron