5

What is the maximum number of dimensions that you can use when declaring an array?

For Example.

#include <iostream.h>
#include <conio.h>
{
       int a[3][3][3][4][3];
       a[2][2][2][2][2] = 9;
}

So, how many dimensions can we declare on an array. What is limitation of it? And what is reason behind it?

Jarod42
  • 203,559
  • 14
  • 181
  • 302
Dipali Y.
  • 113
  • 1
  • 3
  • 8
  • 7
    Your code has undefined behavior, due to out-of-bounds array access.... – ad absurdum Aug 29 '17 at 04:22
  • 2
    Curious combination of tags — [tag:c] and [tag:c++11]. Why not a dated C tag such as [tag:c11], or an undated C++ tag such as [tag:c++], or the latest C++ version ([tag:c++14], or even possibly [tag:c++17])? – Jonathan Leffler Aug 29 '17 at 04:58
  • 1
    Human would be more limited to understand those arrays than computer as declaring/using them. – Jarod42 Aug 29 '17 at 07:49

3 Answers3

11

ISO/IEC 9899:2011 — C

In C, the C11 standard requires:

5.2.4.1 Translation limits

The implementation shall be able to translate and execute at least one program that contains at least one instance of every one of the following limits:18)

  • 12 pointer, array, and function declarators (in any combinations) modifying an arithmetic, structure, union, or void type in a declaration.

18) Implementations should avoid imposing fixed translation limits whenever possible.

That means that to be a standard-compliant compiler, it must allow at least 12 array dimensions on a simple type like int, but should avoid imposing any limit if at all possible. The C90 and C99 standards also required the same limit.

ISO/IEC 14882:2011 — C++

For C++11, the equivalent information is:

Annex B (informative) Implementation quantities [implimits]

Because computers are finite, C++ implementations are inevitably limited in the size of the programs they can successfully process. Every implementation shall document those limitations where known. This documentation may cite fixed limits where they exist, say how to compute variable limits as a function of available resources, or say that fixed limits do not exist or are unknown.

2 The limits may constrain quantities that include those described below or others. The bracketed number following each quantity is recommended as the minimum for that quantity. However, these quantities are only guidelines and do not determine compliance.

  • Pointer, array, and function declarators (in any combination) modifying a class, arithmetic, or incomplete type in a declaration [256].

Thus, in C++, the recommendation is that you should be able to use at least 256 dimensions in an array declaration.


Note that even after you've got the compiler to accept your code, there will ultimately be limits imposed by the memory on the machine where the code is run. The standards specify the minimum number of dimensions that the compiler must allow (over-specify in the C++ standard; the mind boggles at the thought of a 256-dimensional array). The intention is that you shouldn't run into a problem — use as many dimensions as you need. (Can you imagine working with the source code for a 64-dimensional array, let alone anything more — the individual expressions in the source would be horrid to behold, let alone write, read, modify.)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • Can you please tell me where to find the standard? C and C++ – user2736738 Aug 29 '17 at 05:09
  • Presumably (for C), this means that at least 12 dimensions are possible so long as there is enough memory. Doesn't the stipulation that an instance of an object of at least 65535 bytes be possible mean that even if there is plenty of memory (e.g., stack limit of 4Mb), that an array of 12 dimensions might not be possible if it occupies more than 65535 bytes? – ad absurdum Aug 29 '17 at 05:10
  • @DavidBowling.: That's why I mentioned about the memory constraint because ultimately it is constrained by memory..even `a[10000][10000]` will fail in case of C. – user2736738 Aug 29 '17 at 05:11
  • @coderredoc-- here is a nice html version of the [C11 Draft Standard](http://port70.net/~nsz/c/c11/n1570.html) – ad absurdum Aug 29 '17 at 05:13
  • You can buy them from ANSI or ISO, or find drafts at the committee web pages ([C — WG14](http://www.open-std.org/jtc1/sc22/wg14/) or [C++ — WG22](http://www.open-std.org/jtc1/sc22/wg22/)). See also [Where do I find the current C or C++ standard documents?](https://stackoverflow.com/questions/81656/where-do-i-find-the-current-c-or-c-standard-documents) – Jonathan Leffler Aug 29 '17 at 05:15
  • @JonathanLeffler.: Thanks..I will do that later.. regarding the question.Don't you think ultimately it boils down to the memory available..? – user2736738 Aug 29 '17 at 05:25
  • @coderredoc: There are two sets of memory limits to worry about — the compiler and the run-time. The compiler is required to be able to process N-dimensional arrays where the limit on N should not be less than 12. It doesn't have to create that array in its own memory; it has to be able to create object code that will use it. The machine where the code runs may be wholly different: it may be larger, or smaller, or the same machine. The memory limits there will matter — but only if the compiler has accepted the code in the first place. Yes, memory is also a factor — but so are the standards. – Jonathan Leffler Aug 29 '17 at 05:30
  • Ok I get it...standard specifies a minimum limit of 12 in any case. yes standard is clear about that. – user2736738 Aug 29 '17 at 05:36
  • 2
    There's some interest in the C++ requirement. Supposing you had an array of `char` with 256 dimensions, each of size 2. Then that would require 2^256 bytes of memory to implement it. That's an awful lot of memory; I don't see anyone ever testing that limit successfully! Even with Moore's Law and assuming we could get an exbibyte of main memory now, and the amount doubles every 12 months, it would take about 200 years before we reach that limit. And I'm not sure that isn't ignoring little issues like 'the size of the universe'. (You could test it with some dimensions of `[1]`, but…) – Jonathan Leffler Aug 29 '17 at 05:39
  • @JonathanLeffler.: Yes i thought all that without any knowledge of what is specified in standard and said that in answer. I guess it is not adding value. Good answer. – user2736738 Aug 29 '17 at 05:40
  • @JonathanLeffler: Note however that an array of `char` with 256 dimensions each of size 1 is valid and no larger than a single byte ;-). C Compilers may reject is as the number of dimensions exceeds 12. – chqrlie Aug 29 '17 at 05:49
  • @chqrlie: Yes — I noted that `[1]` allows you to shrink the array size, at the cost of 'why do you need that dimension in the array since you can only ever specify `0` as the index value'. I think we should go and find somewhere to discuss how many angels can dance on the head of a pin. :D – Jonathan Leffler Aug 29 '17 at 05:51
  • @JonathanLeffler: OK, I read your comment too quickly. Regarding the angels, male or female? – chqrlie Aug 29 '17 at 06:01
  • 1
    @chqrlie: Female angels are more agile than male angels, so let's go with female...Or, since we should aim to achieve equality, perhaps we should check how many male and how many female angels exist, and try to balance the numbers to ensure appropriate representation. Do we have to worry about ages, and races, and so on? – Jonathan Leffler Aug 29 '17 at 06:10
1

It is not hard to understand that it is only limited by the amount of memory your machine has. You can take 100 (n)dimensional array also.1

Note: your code is accessing a memory out of the bound which is undefined behavior.

1.standard specifies a minimum limit of 12 in case of C and 256 in case of c++11.(This information is added after discussion with Jonathan leffler.My earlier answer only points out the maximum limits which is constrained my machine memory.

user2736738
  • 30,591
  • 5
  • 42
  • 56
0

maximum number depend on stack size. ex, if stack size = 1Mb --> size of int a[xx][xx][xx][xx][xx] must < 1Mb

  • 1
    The question is about the number of dimensions, not the size – Karsten Koop Aug 29 '17 at 06:46
  • Not all arrays are allocated on the stack. Some are allocated as global variables, and some are allocated dynamically. Their size is not limited by the stack size. You're right that the stack can be quite small. Even on UNIX machines, it tends to be 8 MiB, though Windows defaults to just 1 MiB. – Jonathan Leffler Aug 29 '17 at 12:27