comp.lang.ada
 help / color / mirror / Atom feed
* Newbie question about pointers in GNAT 3.15p on Windows 2000
@ 2003-02-21 21:20 David Gressett
  2003-02-21 23:03 ` David C. Hoos, Sr.
  2003-02-22 15:31 ` Steve
  0 siblings, 2 replies; 8+ messages in thread
From: David Gressett @ 2003-02-21 21:20 UTC (permalink / raw)



I have the following package which I am trying to compile with GNAT
3.15p on Windows 2000:

-----------------------------------------------------------------------
package Miscellaneous3 is

  
  function X
    return String;
    
end Miscellaneous3;


package body Miscellaneous3 is

  -----------------------
  -- X --
  -----------------------
  
  type P_Integer is access all Integer;

  function X
    return String
  is
    Bufsize: Aliased Integer;
    P_Size: P_Integer;
    
  begin
    Bufsize := 1;
    P_Size := BufSize'Access;
    return "";
  end X;

end Miscellaneous3;

---------------------------------------------------------------------
Compilation produces this error message:

miscellaneous3.adb:17:15 non-local pointer cannot point to local
object

I have no idea what "local" means here -  both the pointer P_Size and
the pointed-to variable Bufsize are defined in the same place.

What am I doing wrong?






^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Newbie question about pointers in GNAT 3.15p on Windows 2000
  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-22 15:31 ` Steve
  1 sibling, 1 reply; 8+ messages in thread
From: David C. Hoos, Sr. @ 2003-02-21 23:03 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway

"Local" refers to the declarative region of the function X.

That the pointer is "non-local" is a consequence of the fact that
the pointer _type_ is not in the local region.

It's hard to see what is the point of this exercise, since the
value of P_Size is not used.  But, in general taking the access of
a variable on the stack is not very useful (e.g., to return it as
the result of a function), since the local stack variable is subject
to corruption upon return from the function.


----- Original Message ----- 
From: "David Gressett" <gressett@airmail.net>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: February 21, 2003 3:20 PM
Subject: Newbie question about pointers in GNAT 3.15p on Windows 2000


> 
> I have the following package which I am trying to compile with GNAT
> 3.15p on Windows 2000:
> 
> -----------------------------------------------------------------------
> package Miscellaneous3 is
> 
>   
>   function X
>     return String;
>     
> end Miscellaneous3;
> 
> 
> package body Miscellaneous3 is
> 
>   -----------------------
>   -- X --
>   -----------------------
>   
>   type P_Integer is access all Integer;
> 
>   function X
>     return String
>   is
>     Bufsize: Aliased Integer;
>     P_Size: P_Integer;
>     
>   begin
>     Bufsize := 1;
>     P_Size := BufSize'Access;
>     return "";
>   end X;
> 
> end Miscellaneous3;
> 
> ---------------------------------------------------------------------
> Compilation produces this error message:
> 
> miscellaneous3.adb:17:15 non-local pointer cannot point to local
> object
> 
> I have no idea what "local" means here -  both the pointer P_Size and
> the pointed-to variable Bufsize are defined in the same place.
> 
> What am I doing wrong?
> 
> 
> 
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 
> 




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Newbie question about pointers in GNAT 3.15p on Windows 2000
  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-22 15:31 ` Steve
  2003-02-24  6:40   ` Newbie question about pointers in GNAT 3.15p on Windows 2000 : to Steve prashna
  1 sibling, 1 reply; 8+ messages in thread
From: Steve @ 2003-02-22 15:31 UTC (permalink / raw)


Using an acceess variable to reference a local variable is inherently risky
since the memory being accessed is only valid for the lifetime of the
procedure or function in which the variable is declared.  For this reason
using the 'Access attribute is restricted for these variables.

If you are absolutely certain the variable will not be referenced after the
lifetime of the procedure or function in which it is declared, you may use
'Unchecked_Access to specifically get around this restriction.  Using
'Unchecked_Access is a way of saying "I know what I'm doing is risky, but I
want to do it anyway".

I hope this helps,
Steve
(The Duck)





^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Newbie question about pointers in GNAT 3.15p on Windows 2000 : to Steve
  2003-02-22 15:31 ` Steve
@ 2003-02-24  6:40   ` prashna
  2003-02-25  2:57     ` Steve
  0 siblings, 1 reply; 8+ messages in thread
From: prashna @ 2003-02-24  6:40 UTC (permalink / raw)


> 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.

Thanks in Advance
Ashwath



^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Newbie question about pointers in GNAT 3.15p on Windows 2000 : to Steve
@ 2003-02-24 11:48 David C. Hoos, Sr.
  0 siblings, 0 replies; 8+ messages in thread
From: David C. Hoos, Sr. @ 2003-02-24 11:48 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway


----- Original Message ----- 
From: "prashna" <vashwath@rediffmail.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: February 24, 2003 12:40 AM
Subject: Re: Newbie question about pointers in GNAT 3.15p on Windows 2000 : to Steve


> > 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.
He is referring to the variable of which you are taking the 'access --
i.e., BUFSIZE.  It is risky to use 'Unchecked_Access if the result of
the 'Unchecked_Access attribute is passed outside the function, because
when BUFSIZE goes out of scope (i.e., when X returns), the result of the
'Unchecked_Access which has been passed outside of X will no longer be
valid.
> 
> Thanks in Advance
> Ashwath
> _______________________________________________
> comp.lang.ada mailing list
> comp.lang.ada@ada.eu.org
> http://ada.eu.org/mailman/listinfo/comp.lang.ada
> 
> 




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Newbie question about pointers in GNAT 3.15p on Windows 2000
  2003-02-21 23:03 ` David C. Hoos, Sr.
@ 2003-02-24 19:05   ` David Gressett
  2003-02-24 19:25     ` David Gressett
  0 siblings, 1 reply; 8+ messages in thread
From: David Gressett @ 2003-02-24 19:05 UTC (permalink / raw)


On Fri, 21 Feb 2003 17:03:14 -0600, "David C. Hoos, Sr."
<david.c.hoos.sr@ada95.com> wrote:

>"Local" refers to the declarative region of the function X.
>
>That the pointer is "non-local" is a consequence of the fact that
>the pointer _type_ is not in the local region.
>
>It's hard to see what is the point of this exercise, since the
>value of P_Size is not used.  But, in general taking the access of
>a variable on the stack is not very useful (e.g., to return it as
>the result of a function), since the local stack variable is subject
>to corruption upon return from the function.

Thanks.  It's been 2 years since I have done any Ada, and I had
learned very much the previous time.

I ran across this while trying to do Windows system calls with the
Windows binding that comes with the GNAT Windows compiler.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Newbie question about pointers in GNAT 3.15p on Windows 2000
  2003-02-24 19:05   ` David Gressett
@ 2003-02-24 19:25     ` David Gressett
  0 siblings, 0 replies; 8+ messages in thread
From: David Gressett @ 2003-02-24 19:25 UTC (permalink / raw)


On Mon, 24 Feb 2003 13:05:12 -0600, David Gressett
<gressett@airmail.net> wrote:

>On Fri, 21 Feb 2003 17:03:14 -0600, "David C. Hoos, Sr."
><david.c.hoos.sr@ada95.com> wrote:
>
>>"Local" refers to the declarative region of the function X.
>>
>>That the pointer is "non-local" is a consequence of the fact that
>>the pointer _type_ is not in the local region.
>>
>>It's hard to see what is the point of this exercise, since the
>>value of P_Size is not used.  But, in general taking the access of
>>a variable on the stack is not very useful (e.g., to return it as
>>the result of a function), since the local stack variable is subject
>>to corruption upon return from the function.
>
>Thanks.  It's been 2 years since I have done any Ada, and I had
>learned very much the previous time.

Oops -  that should have been " had not learned".
>
>I ran across this while trying to do Windows system calls with the
>Windows binding that comes with the GNAT Windows compiler.




^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: Newbie question about pointers in GNAT 3.15p on Windows 2000 : to 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
  0 siblings, 0 replies; 8+ messages in thread
From: Steve @ 2003-02-25  2:57 UTC (permalink / raw)



"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





^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2003-02-25  2:57 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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
  -- strict thread matches above, loose matches on Subject: below --
2003-02-24 11:48 David C. Hoos, Sr.

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