Re: About Lists of Pointers

From: Dimitrij Klingbeil (dklingb_at_cs.tu-berlin.de)
Date: 10/30/03


Date: Thu, 30 Oct 2003 00:44:33 +0100


"Steve B" <steve@www.www> wrote in message
news:3f9e92e2@newsgroups.borland.com...
> procedure TMainForm.Proc1;
> var
> C: Char;
> begin
> C := 'A';
> FStack.Push(Pointer(C));
> end;
>
> procedure TMainForm.Proc2;
> begin
> ShowMessage(Char(FStack.Pop));
> end;
>
> In Proc1, the address of a local variable is pushed. So why is the
> address still valid in Proc2?

Because you do not actually push the address, but the value that is made
look like an address (converted to be 4 bytes wide). After popping it you
convert it back without having used its address at all. Note that Pointer(C)
is NOT the same as Addr(C), although both return pointers. If you used Addr
instead of Pointer and PChar(X)^ instead of Char(X), you would actually use
real pointers, but this could be exceptionally risky. When Proc2 gets called
immediately after Proc1 returns with no calls or stack operations inbetween,
the stack frame of Proc1 will be invalid, but not yet overwritten. This
would allow to access its data the way your example was meant. Never use it
this way, consequences may become unpredictable (especially when your
functions involve strings as this could force calls implicitly generated by
the compiler).

Dimitrij



Relevant Pages

  • finding process and waiting for it to end
    ... I am working on a jscript that launches a process (proc1). ... Proc1 launches a secondary process (proc2) and then exits. ... I want my script to launch proc1 and then find the process associated ...
    (microsoft.public.scripting.jscript)
  • waiting for input of widget and return input value
    ... run proc1, then proc1 will invoke proc2 and popup some checkbutton for ... to invoke proc3, proc3 will check which button was checked and return a ... checked variable list, then proc2 return the list to proc1, and proc1 ... proc proc1 { ...
    (comp.lang.tcl)
  • Re: Is this a legitimate use of "reutrn -level"?
    ... puts "This is proc0" ... proc proc1 { ... proc proc2 { ...
    (comp.lang.tcl)
  • Re: About Lists of Pointers
    ... "Steve B" wrote ... > In Proc1, the address of a local variable is pushed. ... > So why is the address still valid in Proc2? ... "by chance". ...
    (borland.public.delphi.language.objectpascal)