comp.lang.ada
 help / color / mirror / Atom feed
From: "John B. Matthews" <nospam@nospam.invalid>
Subject: Re: segfault with large-ish array with GNAT
Date: Thu, 18 Mar 2010 10:44:21 -0400
Date: 2010-03-18T10:44:21-04:00	[thread overview]
Message-ID: <nospam-F11D0B.10442118032010@news.aioe.org> (raw)
In-Reply-To: ac4bed10-f655-4fa5-8891-2967ba4388a0@k6g2000prg.googlegroups.com

In article 
<ac4bed10-f655-4fa5-8891-2967ba4388a0@k6g2000prg.googlegroups.com>,
 Jerry <lanceboyle@qwest.net> wrote:

> Thanks for the helpful comments.
> 
> First,
>   ulimit -s unlimited
> does not work on OS X:
>   -bash: ulimit: stack size: cannot modify limit: Operation not
> permitted but I understand that it works on Linux. And possibly the 
> reason is the difference in the way that Linux and OS X treat stack 
> and heap memory. (Don't be confused and think I know what I'm talking 
> about but I read that somewhere.)
> 
> ulimit allows querying the hard limit of stack space
>   ulimit -Hs
> which on OS X reports 65532 = 2^16 -4 kilobytes, about 67 MB. The user
> via ulimit can set the stack up to that size but not higher:
>   ulimit -s 65532
> The default soft limit on OS X is 8192 kB, found by
>   ulimit -s
> 
> So here's me being naive: I would have thought that Ada (or GNAT 
> specifically) would be smart enough to allocate memory for large 
> objects such as my long array in a transparent way so that I don't 
> have to worry about it, thus (in the Ada spirit) making it harder to 
> screw up. (Like not having to worry about whether arguments to 
> subprograms are passed by value or by reference--it just happens.)
> 
> But it seems that I will have to allocate memory for large objects 
> using pointers (and thus take the memory from the heap). Is that 
> right?

I think so. When I ran into this some years ago, I was pleasantly 
surprised at how easy it was to change over to heap allocation for my 
largest data structure. Under Mac OS 9, such allocations fragmented the 
heap, but Mac OS X behaves more reasonably.

The menace listed below allocates megabyte-sized blocks right up to the 
limit of wired memory, as shown by top:

-----
with Ada.Text_IO;

procedure Heap_Test is

Megabyte : constant Positive := 1024 * 1024;
type Block is array (0 .. Megabyte - 1) of Character;
type Block_Ptr is access all Block;

BPtr : Block_Ptr;
N : Natural := 1;

begin
  Ada.Text_IO.Put_Line("*** Heap test...");
  while True loop
    BPtr := new Block;
    Ada.Text_IO.Put (N'Img);
    N := N + 1;
  end loop;
  Ada.Text_IO.New_Line;
end Heap_Test;
-----

This horror raises STORAGE_ERROR at the `ulimit -s` you showed, but only 
when compiled with -fstack-check:

-----
with Ada.Text_IO;

procedure Stack_Test is

Megabyte : constant Positive := 1024 * 1024;
type Block is array (0 .. Megabyte - 1) of Character;

procedure Allocate_Stack (N : Positive) is
  Local : Block := (others => Character'Val(0));
begin
  Ada.Text_IO.Put (N'Img);
  Allocate_Stack(N + 1);
end;

begin
  Ada.Text_IO.Put_Line("*** Stack test...");
  Allocate_Stack(1);
  Ada.Text_IO.New_Line;
end Stack_Test;
-----

For reference, ulimit is a bash built-in, so `man bash` for details:

<http://linux.die.net/man/1/bash>

-- 
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>



  parent reply	other threads:[~2010-03-18 14:44 UTC|newest]

Thread overview: 34+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-03-17 19:21 segfault with large-ish array with GNAT Jerry
2010-03-17 19:36 ` Gautier write-only
2010-03-17 19:58   ` Georg Bauhaus
2010-03-18  6:45     ` Jerry
2010-03-18  7:52       ` Ludovic Brenta
2010-03-18 23:57         ` Robert A Duff
2010-03-18 10:13       ` Jeffrey Creem
2010-03-18 10:23         ` Ludovic Brenta
2010-03-19  0:44           ` Jerry
2010-03-18 19:51         ` Adam Beneschan
2010-03-18 14:44       ` John B. Matthews [this message]
2010-03-19  4:44         ` Jeffrey R. Carter
2010-03-19  8:14           ` John B. Matthews
2010-03-18 15:36       ` Gautier write-only
2010-03-18 16:46       ` tmoran
2010-03-18 19:11         ` Warren
2010-03-18 17:03       ` Warren
2010-03-18 20:38         ` Maciej Sobczak
2010-03-19 13:26           ` Charmed Snark
2010-03-19 17:27             ` tmoran
2010-03-19 18:02               ` Simon Wright
2010-03-19 20:10                 ` Warren
2010-03-19 21:50                 ` Adam Beneschan
2010-03-19 20:24               ` Warren
2010-03-19 20:38           ` Warren
2010-03-19  8:31         ` Ludovic Brenta
2010-03-19 13:20           ` Warren
2010-03-19 12:04       ` Brian Drummond
2010-03-19 19:22         ` Jerry
2010-03-19 20:22         ` Jeffrey R. Carter
2010-03-19 23:24           ` Jerry
2010-03-20  0:25             ` Jeffrey R. Carter
2010-05-07 21:58       ` Raising the stack limit (was: segfault with large-ish array with GNAT) Björn Persson
2010-03-17 19:57 ` segfault with large-ish array with GNAT jonathan
replies disabled

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