Latest versions
...
Menu  Language: fr
Search




Articles & Documents

Close Tutorials

Preferences

Connect again
---

Your name (or pseudo) :

Your secret code (associated with your pseudo)


 Count of members 409 members


Connected :

(nobody)
CPDB.Net

Friends News
Google Ads
Newsletter
To receive news about this website, consider subscribing to our Newsletter.
Subscribe
Unsubscribe
23 Subscribers
Webmaster - Infos
Visits

   visitors

   visitors online

Pagerank
forum.gifForum - ppCompiler [EN] - Topic #22

Forum - Forum
ppCompiler [EN] - ppCompiler [EN]


active  Topic # 22  Why I crashed (pointers to functions)

08/04/2010 @ 09:04
by vasalvit

Anonymous



Hi All.

I try use pointers to functions, but application crached:


FUNCTION Some(Int : Integer) : Boolean;

BEGIN

    Some := True;

END;


TYPE

    TFunc : FUNCTION (Int : Integer) : Boolean;

    PFunc : ^Func;

VAR

    Func : TFunc;

    FuncP : PFunc;

BEGIN

    Func := Some;

    Func(10); // worked

    FuncP := @Some;

    FuncP^(10); // crached

    FuncP := @Some;

    Func := FuncP ^;

    Func(10); // crached

END.

Write to vasalvit   Post an answer  Top
Answer n° 1
--------
11/04/2010 @ 00:03
by Philippe

Anonymous


"@Some" and "Some", when invoqued as right value in an assignement are both equal to a reference to the function "Some" which is represented by its address in memory.
Moreover, @Some is a pointer type, assignement compatible with all other pointer types. Thus FuncP:@Some is a legal assignement.
The call FuncP^(10); looks for a function at the memory location pointed by FuncP.
But after the assignement FuncP=@Some, the pointee is the adress of the function Some, not the expected memory location that contains a function reference. Thus Crash is normal.
Write instead :
FuncP:=@Func;
FuncP^(10);
In Pascal, pointer to procedural types are rarely necessary, as they lead to useless indirection. Direct procedural types are most of the time enough.
Note that procedural types are allowed as function parameters.
Write to Philippe   Post an answer  Top
active topic active   closed topic closed   Sticky Sticky   New New message   -   Correct Correct message   Close Close topic   Make sticky Make sticky
[]
Forum Topic  Forum 




^ Top ^