Re: About Lists of Pointers
From: Dimitrij Klingbeil (dklingb_at_cs.tu-berlin.de)
Date: 10/30/03
- Next message: Jerry: "Memory Leak"
- Previous message: Mike Hubbard: "Modal Form"
- In reply to: Steve B: "About Lists of Pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: Jerry: "Memory Leak"
- Previous message: Mike Hubbard: "Modal Form"
- In reply to: Steve B: "About Lists of Pointers"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|