comp.lang.ada
 help / color / mirror / Atom feed
From: Ludovic Brenta <ludovic@ludovic-brenta.org>
Subject: Re: Allocation question
Date: Mon, 11 May 2009 03:40:54 -0700 (PDT)
Date: 2009-05-11T03:40:54-07:00	[thread overview]
Message-ID: <54897f26-0d3f-421e-9426-1822a531674e@p4g2000vba.googlegroups.com> (raw)
In-Reply-To: 4a07fc7a$0$2855$ba620e4c@news.skynet.be

On May 11, 12:23 pm, Olivier Scalbert <olivier.scalb...@algosyn.com>
wrote:
> procedure Test_Image is
>      Image : Image_T(1..100, 1..100);
>      FillColor : constant Color_T := (0, 0, 0);
> begin
>      Fill(Image, FillColor);
> end Test_Image;
>
> This program seems to work.
> However when I increase the size of Image, I get a Segmentation fault.
>
> Should I need to play with access type and allocation stuff ?

Yes. Your image is on the stack. You can increase the size of the
stack up to an operating-system-dependent maximum (see ulimit(1) on
Unix-like systems) but there is always a limit. For potentially very
large objects, I think the better approach is to allocate them on the
heap. I would suggest you hide the allocation and deallocation inside
a controlled object which you declare on the stack, e.g.

private with Ada.Finalization;
package Image is
   type Image_T (Width, Height : Natural := 0) is private;
private
   type Data_T is array(Positive range <>, Positive range <>) of
Color_T;
   type Data_Access_T is access Data_T;
   type Image_T (Width, Height : Natural) is
     new Ada.Finalization.Controlled with record
      Data : Data_Access_T;
   end record;
   overriding procedure Initialize (Object : in out Image_T); --
allocates
   overriding procedure Adjust(Object : in out Image_T); -- duplicates
Data.all?
   overriding procedure Finalize(Object : in out Image_T); --
deallocates
end Image;

with Image;
declare
   Im1 : Image.Image_T (1 .. 1024, 1 .. 1024); -- calls Initialize
which allocates
   Im2 : Image.Image_T (0, 0); -- calls Initialize which does nothing
begin
   Im2 := Im1; -- calls Finalize (Im2) then Adjust (Im2)
end; -- calls Finalize for both

You may also share image data between objects.

For even larger objects you can make Initialize, Adjust and Finalize
much more sophisticated and store parts of the object on the heap and
part on disk. You would do that only if you think you can beat the
performance of your kernel's paging subsystem.

HTH

--
Ludovic Brenta.



  parent reply	other threads:[~2009-05-11 10:40 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-05-11 10:23 Allocation question Olivier Scalbert
2009-05-11 10:38 ` Georg Bauhaus
2009-05-11 10:40 ` Ludovic Brenta [this message]
2009-05-11 12:14   ` Olivier Scalbert
2009-05-11 12:18     ` Ludovic Brenta
2009-05-11 13:01       ` Olivier Scalbert
2009-05-11 12:26   ` Jacob Sparre Andersen
2009-05-11 13:27     ` Ludovic Brenta
2009-05-11 10:51 ` Use aggregates (Was: Allocation question) Jacob Sparre Andersen
2009-05-11 11:46   ` Olivier Scalbert
2009-05-11 12:16   ` Ludovic Brenta
2009-05-11 21:26     ` sjw
2009-05-12  5:58       ` GNAT, aggregates and efficiency (Was: Use aggregates) Jacob Sparre Andersen
2009-05-12 18:57         ` sjw
2009-05-12  7:47     ` Use aggregates (Was: Allocation question) Martin
2009-05-12 10:24       ` Brian Drummond
2009-05-12 11:07         ` Georg Bauhaus
2009-05-12 11:14           ` Georg Bauhaus
2009-05-12 21:18       ` Randy Brukardt
2009-05-13 16:38         ` Martin
2009-05-13 20:38           ` Randy Brukardt
2009-05-14 10:48             ` Martin
2009-05-12  9:13   ` Emacs Stephen Leake
2009-05-12  9:38     ` Emacs Ludovic Brenta
2009-05-12  9:46     ` Emacs Olivier Scalbert
2009-05-11 10:57 ` Allocation question Philipp Riegger
2009-05-11 12:18   ` Georg Bauhaus
2009-05-11 12:36     ` Philipp Riegger
2009-05-11 23:32 ` Brian Drummond
2009-05-12  1:09 ` tmoran
replies disabled

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