Discussion:
[fpc-pascal] Passing nested procs out?
Ryan Joseph
2018-11-15 08:46:19 UTC
Permalink
Is passing nested procs outside of the calling scope relying on undefined behavior? It doesn’t seem like this should be allowed.

===========================

{$modeswitch nestedprocvars}

program test;
type
TCallback = procedure (i: integer) is nested;


function DoTest: TCallback;
var
context: integer = 0;

procedure Process(x: integer);
begin
context += x;
writeln(x,' ',context);
end;
begin
result := @Process;
end;

var
c: TCallback;
begin
c := DoTest(20);
c(1);
end.


Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://l
Sven Barth via fpc-pascal
2018-11-15 10:14:41 UTC
Permalink
Post by Ryan Joseph
Is passing nested procs outside of the calling scope relying on undefined
behavior? It doesn’t seem like this should be allowed.
See the notes mentioned here:
http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables

Don't know if it is documented as such, but if not, it should be.

Regards,
Sven
Marcos Douglas B. Santos
2018-11-15 11:25:34 UTC
Permalink
On Thu, Nov 15, 2018 at 8:15 AM Sven Barth via fpc-pascal
Post by Ryan Joseph
Is passing nested procs outside of the calling scope relying on undefined behavior? It doesn’t seem like this should be allowed.
See the notes mentioned here: http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables
Don't know if it is documented as such, but if not, it should be.
If I understood correctly, this can be used in all cases of anonymous
functions, with the advantage to make the code more readable and
"Pascalish".
Am I right?

regards,
Marcos Douglas
_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman
Sven Barth via fpc-pascal
2018-11-15 13:19:42 UTC
Permalink
Post by Marcos Douglas B. Santos
On Thu, Nov 15, 2018 at 8:15 AM Sven Barth via fpc-pascal
Post by Sven Barth via fpc-pascal
Post by Ryan Joseph
Is passing nested procs outside of the calling scope relying on
undefined behavior? It doesn’t seem like this should be allowed.
http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables
Post by Sven Barth via fpc-pascal
Don't know if it is documented as such, but if not, it should be.
If I understood correctly, this can be used in all cases of anonymous
functions, with the advantage to make the code more readable and
"Pascalish".
Am I right?
No, because function references (which is the name of the variables for
anonymous functions) store the captured variables in an automatically
created and managed object. Nested functions simply work on the stack. So
passing them outside of the context of the function they're declared in
will result in garbage.

Regards,
Sven
Ryan Joseph
2018-11-15 11:40:41 UTC
Permalink
See the notes mentioned here: http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables
Don't know if it is documented as such, but if not, it should be.
Sorry I’m not understanding this. I read we can make the assignment but the part I’m curious about is passing the function pointer out of the nested functions scope. I did some tests and it work in some cases and gave me garbled memory in others. Just wanted to make absolutely certain what’s happening here.


Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/f
Sven Barth via fpc-pascal
2018-11-15 13:24:22 UTC
Permalink
Post by Sven Barth via fpc-pascal
On Nov 15, 2018, at 5:14 PM, Sven Barth via fpc-pascal <
http://wiki.freepascal.org/FPC_New_Features_2.6.0#Support_for_nested_procedure_variables
Don't know if it is documented as such, but if not, it should be.
Sorry I’m not understanding this. I read we can make the assignment but
the part I’m curious about is passing the function pointer out of the
nested functions scope. I did some tests and it work in some cases and gave
me garbled memory in others. Just wanted to make absolutely certain what’s
happening here.
The linked entry answers your question directly:

"if you assign a nested routine to nested procedure variable and then exit
the nested routine's parent stack frame, calling the nested procedure
variable will result in undefined bahaviour [sic]."

Regards,
Sven
Ryan Joseph
2018-11-15 13:32:06 UTC
Permalink
"if you assign a nested routine to nested procedure variable and then exit the nested routine's parent stack frame, calling the nested procedure variable will result in undefined bahaviour [sic]."
My fault for not reading more carefully. Sorry Sven!

Regards,
Ryan Joseph

_______________________________________________
fpc-pascal maillist - fpc-***@lists.freepascal.org
http://lists.freepascal.org/

Continue reading on narkive:
Search results for '[fpc-pascal] Passing nested procs out?' (Questions and Answers)
4
replies
acessing cached pages?
started 2006-07-07 05:56:14 UTC
computers & internet
Loading...