re PR c++/16681 (array initialization in struct construct is a memory hog)

cp:
	PR c++/16681
	* init.c (build_zero_init): Build a RANGE_EXPR for an array
	initializer.
testsuite:
	PR c++/16681
	* g++.dg/init/array15.C: New.
	* g++.dg/init/array16.C: New.

From-SVN: r91928
This commit is contained in:
Nathan Sidwell 2004-12-09 09:37:37 +00:00 committed by Nathan Sidwell
parent 42fabf21b2
commit 9476364759
5 changed files with 174 additions and 9 deletions

View File

@ -1,3 +1,9 @@
2004-12-09 Nathan Sidwell <nathan@codesourcery.com>
PR c++/16681
* init.c (build_zero_init): Build a RANGE_EXPR for an array
initializer.
2004-12-08 Kelley Cook <kcook@gcc.gnu.org>
* typeck.c: Remove DOS line endings.

View File

@ -214,7 +214,6 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
}
else if (TREE_CODE (type) == ARRAY_TYPE)
{
tree index;
tree max_index;
tree inits;
@ -228,14 +227,16 @@ build_zero_init (tree type, tree nelts, bool static_storage_p)
/* A zero-sized array, which is accepted as an extension, will
have an upper bound of -1. */
if (!tree_int_cst_equal (max_index, integer_minus_one_node))
for (index = size_zero_node;
!tree_int_cst_lt (max_index, index);
index = size_binop (PLUS_EXPR, index, size_one_node))
inits = tree_cons (index,
build_zero_init (TREE_TYPE (type),
/*nelts=*/NULL_TREE,
static_storage_p),
inits);
{
tree elt_init = build_zero_init (TREE_TYPE (type),
/*nelts=*/NULL_TREE,
static_storage_p);
tree range = build2 (RANGE_EXPR,
sizetype, size_zero_node, max_index);
inits = tree_cons (range, elt_init, inits);
}
CONSTRUCTOR_ELTS (init) = nreverse (inits);
}
else

View File

@ -1,3 +1,9 @@
2004-12-09 Nathan Sidwell <nathan@codesourcery.com>
PR c++/16681
* g++.dg/init/array15.C: New.
* g++.dg/init/array16.C: New.
2004-12-08 Tobias Schlueter <tobias.schlueter@physik.uni-muenchen.de>
PR fortran/18826

View File

@ -0,0 +1,46 @@
// { dg-do run }
// Copyright (C) 2004 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 8 Dec 2004 <nathan@codesourcery.com>
// PR 16681 too much memory used
// Origin: Matt LaFary <lafary@activmedia.com>
struct foo {
unsigned char buffer[4111222];
foo() ;
bool check () const;
};
foo::foo ()
: buffer()
{}
bool foo::check () const
{
for (unsigned ix = sizeof (buffer); ix--;)
if (buffer[ix])
return false;
return true;
}
void *operator new (__SIZE_TYPE__ size, void *p)
{
return p;
}
char heap[5000000];
int main ()
{
for (unsigned ix = sizeof (heap); ix--;)
heap[ix] = ix;
foo *f = new (heap) foo ();
if (!f->check ())
return 1;
return 0;
}

View File

@ -0,0 +1,106 @@
// { dg-do run }
// Copyright (C) 2004 Free Software Foundation, Inc.
// Contributed by Nathan Sidwell 8 Dec 2004 <nathan@codesourcery.com>
// PR 16681 too much memory used
// Origin: Matt LaFary <lafary@activmedia.com>
struct elt
{
static int count;
static elt*ptr;
static int abort;
char c;
elt ();
~elt ();
};
int elt::count;
elt *elt::ptr;
int elt::abort;
elt::elt ()
:c ()
{
if (count >= 0)
{
if (!ptr)
ptr = this;
if (count == 100)
throw 2;
if (this != ptr)
abort = 1;
count++;
ptr++;
}
}
elt::~elt ()
{
if (count >= 0)
{
ptr--;
count--;
if (ptr != this)
abort = 2;
}
}
struct foo {
elt buffer[4111222];
foo() ;
bool check () const;
};
foo::foo ()
: buffer()
{}
bool foo::check () const
{
for (unsigned ix = sizeof (buffer)/ sizeof (buffer[0]); ix--;)
if (buffer[ix].c)
return false;
return true;
}
void *operator new (__SIZE_TYPE__ size, void *p)
{
return p;
}
char heap[5000000];
int main ()
{
for (unsigned ix = sizeof (heap); ix--;)
heap[ix] = ix;
try
{
foo *f = new (heap) foo ();
return 1;
}
catch (...)
{
if (elt::count)
return 2;
if (elt::abort)
return elt::abort + 3;
}
for (unsigned ix = sizeof (heap); ix--;)
heap[ix] = ix;
elt::count = -1;
foo *f = new (heap) foo ();
if (!f->check ())
return 3;
return 0;
}