From: visaoni@gmail.com
Subject: Interesting Array Initialization Code Gen [FSF GNAT 4.9.2]
Date: Mon, 11 May 2015 11:02:51 -0700 (PDT)
Date: 2015-05-11T11:02:51-07:00 [thread overview]
Message-ID: <9ee472db-e51d-4e04-a5df-7bee793794a1@googlegroups.com> (raw)
Recently I was messing around with some large arrays and noticed something strange. Large arrays (allocated with new) cause a stack overflow when initialized with a function call. Another pair of eyes (thanks, Lucretia) was able to spot the issue.
-----
procedure Main is
function X( I : Integer ) return Integer is (I);
type Arr is array (1..2**30) of Integer;
type Arr_Ptr is access Arr;
Q : Arr_Ptr := new Arr'(others => X(1));
begin
null;
end Main;
------
Interestingly this creates an array on the stack, initializes it, and then copies it into a newly allocated array.
-----
procedure main is
function main__x (i : integer) return integer;
type main__arr is array (1 .. 16#4000_0000#) of integer;
type main__arr_ptr is access main__arr;
[type main__TarrB is array (1 .. 16#4000_0000# range <>) of integer]
freeze main__TarrB []
[subtype main__T5b is main__TarrB (1 .. 16#4000_0000#)]
A6b : main__T5b;
for J7b in 1 .. 16#4000_0000# loop
A6b (J7b) := main__x (1);
end loop;
q : main__arr_ptr := new main__arr'(A6b);
function main__x (i : integer) return integer is
begin
return (i);
end main__x;
begin
null;
return;
end main;
-----
Furthermore, if you don't use a function, like so...
-----
procedure Main is
function X( I : Integer ) return Integer is (I);
type Arr is array (1..2**30) of Integer;
type Arr_Ptr is access Arr;
Q : Arr_Ptr := new Arr'(others => 1);
begin
null;
end Main;
-----
You get something that seems more sensible, with no array on the stack and the allocated array initialized via pointer.
-----
procedure main is
function main__x (i : integer) return integer;
type main__arr is array (1 .. 16#4000_0000#) of integer;
type main__arr_ptr is access main__arr;
[type main__TarrB is array (1 .. 16#4000_0000# range <>) of integer]
freeze main__TarrB []
[subtype main__T5b is main__TarrB (1 .. 16#4000_0000#)]
P6b : main__arr_ptr := new main__T5b;
for J8b in 1 .. 16#4000_0000# loop
main__T5b!(P6b.all) (J8b) := 1;
end loop;
q : main__arr_ptr := P6b;
function main__x (i : integer) return integer is
begin
return (i);
end main__x;
begin
null;
return;
end main;
-----
Particularly in light of the second bit, the code gen for initialization by function seems incorrect. Or is this allowed somehow, even though it effectively means potentially large arrays can't be initialized by function?
Also, GNAT GPL 2014 generates similar code but in both cases raises a storage error saying the object is too large.
next reply other threads:[~2015-05-11 18:02 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-05-11 18:02 visaoni [this message]
2015-05-11 22:45 ` Interesting Array Initialization Code Gen [FSF GNAT 4.9.2] visaoni
2015-05-12 1:23 ` Randy Brukardt
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox