public inbox for gcc-help@gcc.gnu.org
 help / color / mirror / Atom feed
* maximum number of dimensions in C/C++ array
@ 2006-05-14 21:13 andre maute
  2006-05-14 21:27 ` Brian Dessent
  0 siblings, 1 reply; 6+ messages in thread
From: andre maute @ 2006-05-14 21:13 UTC (permalink / raw)
  To: gcc-help

Has somebody an idea where to find the maximum number
of dimensions for declaring an array?
How many are supported within GCC and how many are
mandated by the standards?

e.g.

double v[n1][n2][n3];
declares obviously a  3-dimensional array of doubles.

Regards Andre

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

* Re: maximum number of dimensions in C/C++ array
  2006-05-14 21:13 maximum number of dimensions in C/C++ array andre maute
@ 2006-05-14 21:27 ` Brian Dessent
  2006-05-14 21:39   ` andre maute
  0 siblings, 1 reply; 6+ messages in thread
From: Brian Dessent @ 2006-05-14 21:27 UTC (permalink / raw)
  To: gcc-help

andre maute wrote:

> Has somebody an idea where to find the maximum number
> of dimensions for declaring an array?
> How many are supported within GCC and how many are
> mandated by the standards?
> 
> e.g.
> 
> double v[n1][n2][n3];
> declares obviously a  3-dimensional array of doubles.

The problem is that declaring an array that way causes it to be
allocated on the stack, and the stack is not a good place to allocate
large amounts of memory.  You will almost certainly overflow the stack
trying to do this.  If instead you use malloc() and pointers, you can
allocate arrays to any arbitrary depth, bounded only by total available
memory.

Brian

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

* Re: maximum number of dimensions in C/C++ array
  2006-05-14 21:27 ` Brian Dessent
@ 2006-05-14 21:39   ` andre maute
  2006-05-15 11:33     ` John Love-Jensen
  0 siblings, 1 reply; 6+ messages in thread
From: andre maute @ 2006-05-14 21:39 UTC (permalink / raw)
  To: gcc-help

On Sunday 14 May 2006 23:27, Brian Dessent wrote:
> andre maute wrote:
> > Has somebody an idea where to find the maximum number
> > of dimensions for declaring an array?
> > How many are supported within GCC and how many are
> > mandated by the standards?
> >
> > e.g.
> >
> > double v[n1][n2][n3];
> > declares obviously a  3-dimensional array of doubles.
>
> The problem is that declaring an array that way causes it to be
> allocated on the stack, and the stack is not a good place to allocate
> large amounts of memory.  You will almost certainly overflow the stack
> trying to do this.  If instead you use malloc() and pointers, you can
> allocate arrays to any arbitrary depth, bounded only by total available
> memory.

What i really want to do, is to generate header files with const array's,
with at least 8 dimensions. This happens within my numerical integration code.

So there should be no problem with the stack.

I.e. i want to know how many dimensions the compiler is supposed to cope with, 
when parsing the const array.

Andre

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

* Re: maximum number of dimensions in C/C++ array
  2006-05-14 21:39   ` andre maute
@ 2006-05-15 11:33     ` John Love-Jensen
  2006-05-15 14:50       ` andre maute
  0 siblings, 1 reply; 6+ messages in thread
From: John Love-Jensen @ 2006-05-15 11:33 UTC (permalink / raw)
  To: andre maute, MSX to GCC

Hi Andre,

I have not run into the number-of-dimension limit of the compiler.

But I've never tried 8 (or more) dimensions!

I don't think the ISO 9899:1999 (C99) or ISO 9899:1989 (C89) have limits on
the number of dimensions.  I presume the only constraint is available memory
for your architecture.

Calculate out how many bytes of storage your technique will utilize:
sizeof(double) * n1 * n2 * n3 * n4 * n5 * n6 * n7 * n8;
Is that within the constraint of your platform?  For instance, PC-DOS that
limit would be 65536 bytes.

Also note:  putting arrays (or any sort of data) in header files is strongly
discouraged.  If two different source files include that same header file,
each translation unit will get its own copy of that data.  Header files are
for declarations of things (no storage reserved), not definitions of things
(bytes of code or data allocated).

Also, as mentioned already, putting large arrays on the stack is
discouraged, because the stack is usually significantly smaller than the
heap (or in C++ lingo, the global store).

Sincerely,
--Eljay

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

* Re: maximum number of dimensions in C/C++ array
  2006-05-15 11:33     ` John Love-Jensen
@ 2006-05-15 14:50       ` andre maute
  2006-05-15 15:14         ` Brian Budge
  0 siblings, 1 reply; 6+ messages in thread
From: andre maute @ 2006-05-15 14:50 UTC (permalink / raw)
  To: gcc-help

On Monday 15 May 2006 13:32, John Love-Jensen wrote:
> Hi Andre,
>
> I have not run into the number-of-dimension limit of the compiler.
>
> But I've never tried 8 (or more) dimensions!
I investigated it further and i can limit it to 9 dimensions
>
> I don't think the ISO 9899:1999 (C99) or ISO 9899:1989 (C89) have limits on
> the number of dimensions.  I presume the only constraint is available
> memory for your architecture.
that would be nice
>
> Calculate out how many bytes of storage your technique will utilize:
> sizeof(double) * n1 * n2 * n3 * n4 * n5 * n6 * n7 * n8;
> Is that within the constraint of your platform?  For instance, PC-DOS that
> limit would be 65536 bytes.
My system is a i686-pc-linux-gnu with enough memory.
I thought that perhaps the compiler needed a maximum dimension constraint.
>
> Also note:  putting arrays (or any sort of data) in header files is
> strongly discouraged.  If two different source files include that same
> header file, each translation unit will get its own copy of that data. 
> Header files are for declarations of things (no storage reserved), not
> definitions of things (bytes of code or data allocated).
I know. Perhaps the best way for such a big array is to create a file e.g 
called bigarray.cc and one bigarray.h where the array is declared extern.

>
> Also, as mentioned already, putting large arrays on the stack is
> discouraged, because the stack is usually significantly smaller than the
> heap (or in C++ lingo, the global store).
I don't put them on the stack, so ...
>
> Sincerely,
> --Eljay
Thank you for your kind response, regards
Andre

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

* Re: maximum number of dimensions in C/C++ array
  2006-05-15 14:50       ` andre maute
@ 2006-05-15 15:14         ` Brian Budge
  0 siblings, 0 replies; 6+ messages in thread
From: Brian Budge @ 2006-05-15 15:14 UTC (permalink / raw)
  To: andre maute; +Cc: gcc-help

If you are using C++ you might look into using the boost multiarray
class.  It will take care of many of these issues, and you can use it
the same way ie.
with multiple bracket operators [][][][][][][][]...

  Brian

On 5/15/06, andre maute <andre.maute@gmx.de> wrote:
> On Monday 15 May 2006 13:32, John Love-Jensen wrote:
> > Hi Andre,
> >
> > I have not run into the number-of-dimension limit of the compiler.
> >
> > But I've never tried 8 (or more) dimensions!
> I investigated it further and i can limit it to 9 dimensions
> >
> > I don't think the ISO 9899:1999 (C99) or ISO 9899:1989 (C89) have limits on
> > the number of dimensions.  I presume the only constraint is available
> > memory for your architecture.
> that would be nice
> >
> > Calculate out how many bytes of storage your technique will utilize:
> > sizeof(double) * n1 * n2 * n3 * n4 * n5 * n6 * n7 * n8;
> > Is that within the constraint of your platform?  For instance, PC-DOS that
> > limit would be 65536 bytes.
> My system is a i686-pc-linux-gnu with enough memory.
> I thought that perhaps the compiler needed a maximum dimension constraint.
> >
> > Also note:  putting arrays (or any sort of data) in header files is
> > strongly discouraged.  If two different source files include that same
> > header file, each translation unit will get its own copy of that data.
> > Header files are for declarations of things (no storage reserved), not
> > definitions of things (bytes of code or data allocated).
> I know. Perhaps the best way for such a big array is to create a file e.g
> called bigarray.cc and one bigarray.h where the array is declared extern.
>
> >
> > Also, as mentioned already, putting large arrays on the stack is
> > discouraged, because the stack is usually significantly smaller than the
> > heap (or in C++ lingo, the global store).
> I don't put them on the stack, so ...
> >
> > Sincerely,
> > --Eljay
> Thank you for your kind response, regards
> Andre
>

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

end of thread, other threads:[~2006-05-15 15:14 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-05-14 21:13 maximum number of dimensions in C/C++ array andre maute
2006-05-14 21:27 ` Brian Dessent
2006-05-14 21:39   ` andre maute
2006-05-15 11:33     ` John Love-Jensen
2006-05-15 14:50       ` andre maute
2006-05-15 15:14         ` Brian Budge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).