comp.lang.ada
 help / color / mirror / Atom feed
From: "Steve" <nospam_steved94@attbi.com>
Subject: Re: Newbie question about pointers in GNAT 3.15p on Windows 2000 : to Steve
Date: Tue, 25 Feb 2003 02:57:21 GMT
Date: 2003-02-25T02:57:21+00:00	[thread overview]
Message-ID: <lYA6a.225562$iG3.27006@sccrnsc02> (raw)
In-Reply-To: d40d7104.0302232240.2681e8e5@posting.google.com


"prashna" <vashwath@rediffmail.com> wrote in message
news:d40d7104.0302232240.2681e8e5@posting.google.com...
> > If you are absolutely certain the variable will not be referenced after
the
>                                     ^^^^^
> > lifetime of the procedure or function in which it is declared>
>
> Hi Steve,
> This sentence is bit confisung for me.I am not sure you are talking
> about which variable. Are talking about BUFSIZE (or P_SIZE). Both
> these variables are local to function X and it is absolutely
> impossible to refer these variables outside X.Please clarify this.
> And also please let me know how it is risky if I use
> 'Unchecked_Access.
>

The simplest answer is an example of what to NOT do:

with Ada.Integer_Text_IO;
with Ada.Text_Io;
procedure Counter_Example is

  type my_record is
    record
      value : Integer;
    end record;

  type my_record_acc is access all my_record;

  procedure Nested( acc : out my_record_acc ) is
    local_value : aliased my_record;
  begin
    local_value.value := 42;
    acc := local_value'unchecked_access;
  end Nested;

  rec_acc : my_record_acc;
begin
  Nested( rec_acc );
  Ada.Text_Io.Put( "Value is: " );
  Ada.Integer_Text_Io.Put( rec_acc.value );
  Ada.Text_Io.New_Line;
end Counter_Example;

In this example the value returned from "Nested" is an access to
"local_value".  But on return from "Nested", "local_value" no longer exists.
In this example the value contained in the no-longer valid local_value
access is displayed.  On my system the resulting output is:

  Value is: 11

The key I am trying to illustrate is that access to local variables are no
longer valid after the procedure or function in which they are declared have
returned.  Saving one of these "pointers" in a data structure that has a
lifetime longer than the procedure or function means the pointer will not be
valid when the procedure or function returns.

If you are certain that this condition will never happen, everything will be
ok.

I hope this helps,
Steve
(The Duck)

> Thanks in Advance
> Ashwath





  reply	other threads:[~2003-02-25  2:57 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-21 21:20 Newbie question about pointers in GNAT 3.15p on Windows 2000 David Gressett
2003-02-21 23:03 ` David C. Hoos, Sr.
2003-02-24 19:05   ` David Gressett
2003-02-24 19:25     ` David Gressett
2003-02-22 15:31 ` Steve
2003-02-24  6:40   ` Newbie question about pointers in GNAT 3.15p on Windows 2000 : to Steve prashna
2003-02-25  2:57     ` Steve [this message]
  -- strict thread matches above, loose matches on Subject: below --
2003-02-24 11:48 David C. Hoos, Sr.
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox