comp.lang.ada
 help / color / mirror / Atom feed
* Re: Allocating Memory with "new"
@ 2003-04-25 12:46 David C. Hoos
  2003-04-25 22:19 ` Peter Richtmyer
  0 siblings, 1 reply; 6+ messages in thread
From: David C. Hoos @ 2003-04-25 12:46 UTC (permalink / raw)
  To: comp.lang.ada mail to news gateway


----- Original Message -----
From: "Peter Richtmyer" <prichtmyer@yahoo.com>
Newsgroups: comp.lang.ada
To: <comp.lang.ada@ada.eu.org>
Sent: Friday, April 25, 2003 5:33 AM
Subject: Re: Allocating Memory with "new"


> "Vincent Smeets" <No.Spam@localhost> wrote in message
news:<3ea8d041$0$4083$4d4ebb8e@read.news.de.uu.net>...
>
> > Every time an access type comes out of scope, the memory allocated by
that
> > access type can be freed (garbage collection).
>
> Thanks - that certainly explains it. I thought the programmer had
> complete control of the memory allocated and that it would not be
> freed until an Unchecked Deallocate (or the program ended).
>
>From the addresses allocated, it looks like the allocation was done on the
stack, instead of the heap.  The compiler is free to do this, given that
Mem_Block_Ptr is declared on the stack.

Thus, this is not "garbage collection," but simply the fact that the
allocated memory goes out of scope when the procedure returns.

In this sense, the Rational compiler is "smarter" than GNAT, since
allocation on the heap is more expensive than allocation on the stack.

Incidentally, setting Mem_Block_Ptr to null is unnecessary, since
the rules of Ada require access objects to be initialized to null.

David




^ permalink raw reply	[flat|nested] 6+ messages in thread
* Allocating Memory with "new"
@ 2003-04-24 19:30 Peter Richtmyer
  2003-04-25  6:05 ` Vincent Smeets
  2003-04-25 15:09 ` Stephen Leake
  0 siblings, 2 replies; 6+ messages in thread
From: Peter Richtmyer @ 2003-04-24 19:30 UTC (permalink / raw)


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#



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

end of thread, other threads:[~2003-04-25 22:19 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-04-25 12:46 Allocating Memory with "new" David C. Hoos
2003-04-25 22:19 ` Peter Richtmyer
  -- strict thread matches above, loose matches on Subject: below --
2003-04-24 19:30 Peter Richtmyer
2003-04-25  6:05 ` Vincent Smeets
2003-04-25 10:33   ` Peter Richtmyer
2003-04-25 15:09 ` Stephen Leake

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