Search
Articles & Documents
Tutorials
Newsletter
CPDB.Net
Friends News
Visits
Webmaster - Infos
|
- Forum - ppCompiler
Topic # 22 |
Why I crashed (pointers to functions) |
08/04/2010 @ 09:04 by 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. |
|
|
Answer n° 1 -------- 11/04/2010 @ 00:03 by Philippe
visitor |
"@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. |
|
|
topic active
topic closed
Sticky
New message -
Correct message
Close topic
Make sticky
|