Recherche
Articles & Documents
Tutoriaux
Lettre d'information
CPDB.Net
Nouvelles des Amis
Visites
Webmaster - Infos
|
- Forum - ppCompiler
Sujet n° 22 |
Why I crashed (pointers to functions) |
le 08/04/2010 @ 09:04 par vasalvit
|
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. |
|
|
Réponse n° 1 -------- le 11/04/2010 @ 00:03 par Philippe
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. |
|
|
sujet actif
sujet clos
Important!
Nouveau message -
Rectifier message
Clôturer sujet
Remonter
|