Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the maximum number of dimensions allowed for an array, and why?

Tags:

c++

c

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?

like image 758
Dipali Y. Avatar asked Aug 29 '17 04:08

Dipali Y.


People also ask

What is maximum dimension of array allowed in C?

There is no fixed limit to the size of an array in C. The size of any single object, including of any array object, is limited by SIZE_MAX , the maximum value of type size_t , which is the result of the sizeof operator.

What is the maximum number of dimension?

In string theory, the multiverse is made of different dimensions but the highest is 11th dimension. Beyond 11 dimensions, the universe would become unstable and dimensions higher than 11 would collapse to an 11-dimensional universe.

What is the maximum number of dimensions an array in a May Have Mcq?

So, what is the maximum number of dimensions that you can use when declaring an array? It's one, obviously. Actually, your second example has a buffer overrun in it.


2 Answers

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.)

like image 77
Jonathan Leffler Avatar answered Nov 15 '22 04:11

Jonathan Leffler


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.

like image 24
user2736738 Avatar answered Nov 15 '22 04:11

user2736738