From: anon@att.net
Subject: Re: GNAT - return by anonymous access
Date: Tue, 20 Dec 2011 23:53:58 +0000 (UTC)
Date: 2011-12-20T23:53:58+00:00 [thread overview]
Message-ID: <jcr76k$2ogd$1@news.ett.com.ua> (raw)
In-Reply-To: 784c67eb-a542-41b0-b23d-fca1234e56b2@n10g2000vbg.googlegroups.com
A corrected version, that works!
Just made a few changes in the code.
First GNAT does not like the "access Integer", define a
Integer_Access type.
Second, "limited" was remove because of the error message
"cannot initialize entities of limited type".
Third, I use an "Use" Statement in "test_driver.adb" instead of
using of "test_package.".
-- Unit 1
package test_package is
type Integer_Access is access all Integer ;
type test_type (p_obj : Integer_Access) is private;
function get return test_type;
private
type test_type (p_obj : Integer_Access) is null record ;
end test_package;
-- Unit 2
package body test_package is
function get return test_type is
begin
return test_type ' ( p_obj => new Integer'(42) );
end get;
end test_package;
-- Unit 3
with test_package;
with Text_IO;
use test_package;
procedure test_driver is
foo : test_type := test_package.get;
-- Removed : '42' (expected)
-- Present : '4409264'
bar : Integer_Access := new Integer'(69);
begin
Text_IO.Put_Line(foo.p_obj.all'img);
end test_driver;
In <784c67eb-a542-41b0-b23d-fca1234e56b2@n10g2000vbg.googlegroups.com>, Simon Belmont <sbelmont700@gmail.com> writes:
>
>This ostensibly simple program prints strange results, and i'm rapidly
>losing faith in the GNAT compiler (GNAT GPL 20110428). If anyone with
>an alternative compiler can confirm or deny this output as spurious, I
>would be much obliged. In short, when the declaration of 'bar' is
>commented out, the program behaves as i would expect (output is '42'),
>but when bar is present, the output is much different (output is
>'4409264'). Or, if my line of reasoning is mixed up and this is
>actually the appropriate behavior, I would be grateful for any
>explanation.
>
>Thank you again
>
>-sb
>
>-- Unit 1
>package test_package is
> type test_type (p_obj : access Integer) is limited private;
> function get return test_type;
>
>private
> type test_type (p_obj : access Integer) is limited null record;
>
>end test_package;
>
>-- Unit 2
>package body test_package is
>
> function get return test_type is
> begin
> return test_type'(p_obj => new Integer'(42));
> end get;
>
>end test_package;
>
>-- Unit 3
>with test_package;
>with Text_IO;
>
>procedure test_driver is
>
> foo : test_package.test_type := test_package.get;
>
> -- Removed : '42' (expected)
> -- Present : '4409264'
> bar : access Integer := new Integer'(69);
>
>begin
>
> Text_IO.Put_Line(foo.p_obj.all'img);
>
>end test_driver;
prev parent reply other threads:[~2011-12-20 23:53 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-12-17 19:17 GNAT - return by anonymous access Simon Belmont
2011-12-17 19:51 ` Simon Wright
2011-12-18 9:08 ` Georg Bauhaus
2011-12-19 23:46 ` Randy Brukardt
2011-12-20 0:14 ` Shark8
2011-12-20 23:18 ` Randy Brukardt
2011-12-21 0:44 ` Simon Belmont
2011-12-21 7:29 ` AdaMagica
2011-12-21 11:05 ` Georg Bauhaus
2011-12-21 11:43 ` AdaMagica
2011-12-21 12:05 ` Georg Bauhaus
2011-12-22 0:33 ` Randy Brukardt
2011-12-22 0:33 ` Simon Belmont
2011-12-22 7:11 ` Adam Beneschan
2011-12-22 22:11 ` Simon Belmont
2011-12-23 0:56 ` Randy Brukardt
2011-12-23 13:20 ` Simon Belmont
2011-12-27 23:51 ` Randy Brukardt
2011-12-20 23:53 ` anon [this message]
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox