comp.lang.ada
 help / color / mirror / Atom feed
From: "Vincent Smeets" <No.Spam@localhost>
Subject: Re: Allocating Memory with "new"
Date: Fri, 25 Apr 2003 08:05:53 +0200
Date: 2003-04-25T08:05:53+02:00	[thread overview]
Message-ID: <3ea8d041$0$4083$4d4ebb8e@read.news.de.uu.net> (raw)
In-Reply-To: 1b585154.0304241130.35fa0a97@posting.google.com

Hello,

I would say that the Sun version is correct! You defined the package
Mem_Block_Ptr_Pkg inside the procedure alloc. Every time the procedure is
left, the package becomes out of scope and so does the access type defined
in that package. The next time you call the procedure, a new package will be
instantiated and with that a new access type will be defined.

Every time an access type comes out of scope, the memory allocated by that
access type can be freed (garbage collection).

Conclusion:
Your program is erroneous.
Move the package Mem_Block_Ptr_Pkg to an outer scoop.



Vincent Smeets

"Peter Richtmyer" <prichtmyer@yahoo.com> wrote in message
news:1b585154.0304241130.35fa0a97@posting.google.com...
> I am stumped ...
>
> My memory allocation scheme worked OK on Gnat / Win2K,
> but on Sun Blade with Rational Ada it does not give me
> what I expected. See after program for output.
>
> I built a test program to illustrate my problem:
> (forgive me, it is quick and ugly)
>
> thanks
> Peter
> --------------------------------------------------------------
> with system;
> with system.address_to_access_conversions;
> with Text_Io;
> use  text_io;
> with unchecked_conversion;
>
> procedure Hi is
>
>     first_time : boolean := true;
>
>     procedure alloc (nbytes : integer) is
>
>         type Mem_Block_T is array (1 .. Nbytes) of Character;
>         package Mem_Block_Ptr_Pkg is new
>              system.address_to_access_conversions (Mem_Block_T);
> Mem_Block_Ptr :  Mem_Block_Ptr_Pkg.Object_Pointer := null;
>
> addr : system.address;
>
> In_Work_Area : String (1 .. 12) := "            ";
>
>         function Addr_to_int is new Unchecked_Conversion
>             (system.address, integer);
>
>         package data_io is new integer_io(integer);
>
>      begin
>         if first_time then
>            first_time := false;
>            put_line ("Mem_Block_Ptr_Pkg.Object_Pointer'size = " &
>                 Integer'image(Mem_Block_Ptr_Pkg.Object_Pointer'size));
>         end if;
>
> Mem_Block_Ptr := new Mem_Block_T;
> Addr := Mem_Block_Ptr_Pkg.to_address (Mem_Block_Ptr);
>
> Data_Io.Put (In_Work_Area, addr_to_int (addr), 16);
> put_line ("allocated " & In_Work_Area);
>
>      end alloc;
>
>
> begin
>
>     put_line ("system.address'size = " &
Integer'image(system.address'size));
>     put_line ("integer'size = " & Integer'image(integer'size));
>
>     for i in 1 .. 10 loop
>       Put ("len 1: ");
>       alloc (1);
>     end loop;
>
>     for i in 1 .. 10 loop
>       Put ("len 8: ");
>       alloc (8);
>     end loop;
>
>      for i in 1 .. 50 loop
>       Put ("len" & integer'image(i * 8) & " : ");
>       alloc (i * 8);
>     end loop;
>
> end Hi;
>
> -- Rational Apex Ada on SUN Blade:
>
> -- system.address'size =  32
> -- integer'size =  32
> -- len 1: Mem_Block_Ptr_Pkg.Object_Pointer'size =  32
> -- allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 1: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8: allocated    16#F8A88#
> -- len 8 : allocated    16#F8A88#
> -- len 16 : allocated    16#F8A80#
> -- len 24 : allocated    16#F8A78#
> -- len 32 : allocated    16#F8A70#
> -- len 40 : allocated    16#F8A68#
> -- len 48 : allocated    16#F8A60#
> -- len 56 : allocated    16#F8A58#
> -- len 64 : allocated    16#F8A50#
> -- len 72 : allocated    16#F8A48#
> -- len 80 : allocated    16#F8A40#
> -- len 88 : allocated    16#F8A38#
> -- len 96 : allocated    16#F8A30#
>
> -- continues decreasing by 8
>
> -------------------------------------------------------
> -- Gnat 3.15P on Win2000       (OK)
>
> --system.address'size =  32
> --integer'size =  32
> --len 1: Mem_Block_Ptr_Pkg.Object_Pointer'size =  32
> --allocated  16#2602450#
> --len 1: allocated  16#2602520#
> --len 1: allocated  16#2602538#
> --len 1: allocated  16#2602550#
> --len 1: allocated  16#2602568#
> --len 1: allocated  16#2602580#
> --len 1: allocated  16#2602598#
> --len 1: allocated  16#26025B0#
> --len 1: allocated  16#26025C8#
> --len 1: allocated  16#26025E0#
> --len 8: allocated  16#26025F8#
> --len 8: allocated  16#2602610#
> --len 8: allocated  16#2602628#
> --len 8: allocated  16#2602640#
> --len 8: allocated  16#2602658#
> --len 8: allocated  16#2602670#
> --len 8: allocated  16#2602688#
> --len 8: allocated  16#26026A0#
> --len 8: allocated  16#26026B8#
> --len 8: allocated  16#26026D0#
> --len 8 : allocated  16#26026E8#
> --len 16 : allocated  16#2602700#
> --len 24 : allocated  16#2602718#
> --len 32 : allocated  16#2602740#
> --len 40 : allocated  16#2602768#
> --len 48 : allocated  16#26027A0#
> --len 56 : allocated  16#26027D8#
> --len 64 : allocated  16#2602820#
> --len 72 : allocated  16#26039C8#
> --len 80 : allocated  16#2603A20#
> --len 88 : allocated  16#2603A78#
> --len 96 : allocated  16#2603AE0#
> --len 104 : allocated  16#2603B48#
> --len 112 : allocated  16#2603BC0#
> --len 120 : allocated  16#2603C38#
> --len 128 : allocated  16#2603CC0#
> --len 136 : allocated  16#2603D48#




  reply	other threads:[~2003-04-25  6:05 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-04-24 19:30 Allocating Memory with "new" Peter Richtmyer
2003-04-25  6:05 ` Vincent Smeets [this message]
2003-04-25 10:33   ` Peter Richtmyer
2003-04-25 15:09 ` Stephen Leake
  -- strict thread matches above, loose matches on Subject: below --
2003-04-25 12:46 David C. Hoos
2003-04-25 22:19 ` Peter Richtmyer
replies disabled

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