Menu
Recherche




Articles & Documents

Fermer Tutoriaux

Lettre d'information
Pour avoir des nouvelles de ce site, inscrivez-vous à notre Newsletter.
S'abonner
Se désabonner
22 Abonnés
CPDB.Net

Nouvelles des Amis
Visites

   visiteurs

   visiteurs en ligne

Webmaster - Infos
forum.gifForum - ppCompiler - Sujet n°22

Forum - Forum
ppCompiler - ppCompiler


actif  Sujet n° 22  Why I crashed (pointers to functions)

le 08/04/2010 @ 09:04
par vasalvit

Anonyme



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.

Ecrire à vasalvit   Poster une réponse  Haut
Réponse n° 1
--------
le 11/04/2010 @ 00:03
par Philippe

Anonyme

visiteur
"@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.
Ecrire à Philippe   Poster une réponse  Haut
actif sujet actif   clos sujet clos   Important! Important!   Nouveau Nouveau message   -   Rectifier Rectifier message   Clôturer Clôturer sujet   Remonter Remonter
[]
Catégories de discussion  Forum 




^ Haut ^