[fpc-pascal] Problem passing pointers to "hidden" procedure

andrew.bennett at ns.sympatico.ca andrew.bennett at ns.sympatico.ca
Sat Oct 15 01:01:47 CEST 2011


As part of an attempt at multi-threading, I have had to move
many things that used to be global into the Implementation
parts of units in order to enforce privacy. When I moved a
routine passed as a parameter and put it inside the routine
to be threaded, the program ceased to work. I append the 
essential code abstracted from my program:

Program Test ;
Uses TestSub ;
Begin
  Test_it ;
End.

Unit TestSub ;
Interface
Procedure Test_it ;
Implementation
Const
  Chunksize = 24 ;
  Arraysize = 100 ;
Type
  ProcT = Procedure(Const P1, P2 : Pointer) ;
  BArray  = Array[0..1999999999] Of Byte ;
  Chunk   = Array[0..Chunksize-1] Of Byte ;
Var
  Data : Array[0..Arraysize-1] Of Chunk ;
Procedure Do_it(Var A ; Const N, NB : Longint ; F : ProcT) ;
Begin
  Writeln('Do_it: expect'#9, Longint(@A), #9, Longint(@Barray(A)[NB])) ;
  F(@BArray(A)[0], @BArray(A)[NB]) ;
End ; { Do_it }
Procedure PrPublic(Const f1, f2 : Pointer) ; { Visible }
Begin
  Writeln('PrPublic'#9, Longint(f1), #9, Longint(f2)) ;
End ; { PrPublic }
Procedure Test_it ;
  Procedure PrPrivate(Const f1, f2 : Pointer) ; { Private }
  Begin
    Writeln('PrPrivate'#9, Longint(f1), #9, Longint(f2)) ;
  End ; { PrPrivate }
Begin { Test_it }
  Do_it(Data, Arraysize, Chunksize, @PrPublic) ;
  Do_it(Data, Arraysize, Chunksize, @PrPrivate) ;
End ; { Test_it }
End.

Output:
Running "c:\fpc\2.4.2\exe\test.exe "
Do_it: expect    4252848    4252872
PrPublic    4252848    4252872
Do_it: expect    4252848`4252872
PrPrivate    4252872    4234688

When the routine being passed as a parameter is in the top level
of the unit, "PrPublic", all is well; the expected pointers are 
passed. When an identical routine "PrPrivate" is hidden inside 
the top level routine "Test_it", the expected 2nd pointer appears 
in place of the first and an apparently random pointer appears 
second.

What am I doing wrong?

Andrew Bennett

 



More information about the fpc-pascal mailing list