Thread: What is the maximum number of dimensions allowed for an array in C++?

  1. #1
    Registered User
    Join Date
    Jun 2019
    Posts
    2

    Lightbulb What is the maximum number of dimensions allowed for an array in C++?

    I am curious to know that the maximum number of dimensions can be an array in C Programming Language, in 32bits and 64bits.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,485
    The language doesn't care, you can have as many as you like.

    The answer (for you, today) depends on your OS and Compiler.

    If you exclude the trivial char array[1][1][1][1][1][1][1][1][1]..., then perhaps char array[2][2][2][2][2][2][2]... is more to your liking.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Dec 2017
    Posts
    630
    It depends on the compiler. The C++ standard apparently recommends supporting at least 256, although it is not required. In a sense all arrays are one-dimensional. Anything else is an array of arrays (of arrays of arrays ...).

    However, I can't see any use for 256 dimensions. A char array with 256 dimensions where each dimension was of size 2 would consume over 10 to the power of 77 bytes. That's close to the number of atoms in the observable universe.

    On a 32-bit machine you can't directly access more than 4gb, the size of a char array of 32 dimensions with each dimension of size 2. On a 64-bit machine you could have, "theoretically", 64 dimensions, if the compiler allowed it. Of course that would leave no room for anything else to be addressed, like the rest of your program and the OS.

    Much more than three dimensions is practically unheard of, so the true answer is that every C or C++ implementation handles more array dimensions than you will ever need in real code.
    The world hangs on a thin thread, and that is the psyche of man. - Carl Jung

  4. #4
    Registered User
    Join Date
    Apr 2019
    Posts
    69
    I think a software renderer must handle more dimensions than 3,the camera changes for example makes use of matrices xyzw calculations,xyztime,UVW=like xyz for textures,ARGB color space
    How do you know when you are a Guru?
    When you can (code) C even blindfolded

  5. #5
    Registered User
    Join Date
    Feb 2019
    Posts
    533
    Quote Originally Posted by I C everything View Post
    I think a software renderer must handle more dimensions than 3,the camera changes for example makes use of matrices xyzw calculations,xyztime,UVW=like xyz for textures,ARGB color space
    It isn't usual for libraries like OpenGL, Direct3D or Vulkan to deal with multidimensional arrays. You can always define your vertices as:
    Code:
    struct vertice_T {
      float x, y, z, w;  // vertice
      float nx, ny, nz; // normal
      float r, g, b, a;  // color
      float u, v; // define s and q if you need them...
      ...
    };
    To define an object on model space you need just to declare a unidimensional array using this structure...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to find maximum number in array
    By vead in forum C Programming
    Replies: 8
    Last Post: 12-18-2017, 11:44 AM
  2. Replies: 1
    Last Post: 08-25-2016, 02:07 AM
  3. Replies: 2
    Last Post: 09-30-2015, 09:15 AM
  4. Finding maximum and minimum number in 2D array
    By wonderwall in forum C Programming
    Replies: 4
    Last Post: 10-23-2011, 10:21 PM
  5. Dynamic array of unknown number of dimensions
    By gamask in forum C Programming
    Replies: 5
    Last Post: 04-29-2011, 08:42 PM

Tags for this Thread