C++ multidimensional array allocation using new []

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • LeDen
    New Member
    • Sep 2007
    • 1

    #1

    C++ multidimensional array allocation using new []

    I have a problem. I need to dynamically allocate variable a, not in initialization but later in the program (i.e. based upon input data). Actually, I have 2-dimensional array the first dimension unknown, and I want to change just the first dimension (other dimensions are known in declaration). I heard this is possible but I don't know how.
    So in the beginning I have the following:
    int a[][5];
    And somehow I need to get a[10][5];

    ..Or the problem in 3 dimensions:
    I need to get a[15][10][5] from previous declaration of a[][10][5] (or ***a for that matter).

    How to do that?
  • Ganon11
    Recognized Expert Specialist
    • Oct 2006
    • 3651

    #2
    You'll need to use a for...loop for each dimension. What I do when faced with this type of problem is this:

    1) Declare your array as an n-pointer (where n is the number of dimensions you need), but don't allocate memory just yet.
    2) Get the size variable from the user.
    3) Initialize the array as an array (with the size given by the user) of (n-1)-pointers.
    4) For each member in this array,
    4) a. Initialize this (n-1)-pointer as an array of (n-2)-pointers.
    4) b. For each member in this array,
    4) b. I...(repeat until you have an array of ints, or doubles, or whatevers).

    So, for a 2-d array, this becomes

    [CODE=cpp]int **array, size;
    //Get user input into size;
    array = new int*[size];
    for (int i = 0; i < size; i++)
    array[i] = new int[10];

    // array is now a 2D array with dimensions size and 10.[/CODE]

    Comment

    • RRick
      Recognized Expert Contributor
      • Feb 2007
      • 463

      #3
      Or you can use C++ vectors to define multiple arrays. It can get messy and typedefs are handy for the definitions. For example,
      [code=cpp]
      typedef vector<int> Ind1d;
      typedef vector< Vec1D> Int2d;
      ... until the cows come home ....
      [/code]
      Since these definitions create empty vectors, you'll still have to initialize the various vector dimensions as you would in creating arrays with new [].
      [code=cpp]
      // Initialize 2D vector to [10][50]
      Int2d iarray;
      iarr.reserve( 10);
      for ( int ind=0; ind<10; ind++)
      iarray[ ind].capacity( 50):
      [/code]

      Now you can access entries like you expect.
      [code=cpp] iarray[5][35] = 99;[/code]

      The real advantage of this is in the allocation and deleting of the vector. Once you delete iarray, all of the entries for all dimensions are gone.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Or perhaps, you could just use an array properly.

        First, there are only one-dimensional arrays in C or C++. The number of elements in out between brackets:
        [code=c]
        int array[5];
        [/code]

        That is an array of 5 elements each of which is an int.

        [code=c]
        int array[];
        [/code]

        won't compile. You need to declare the number of elements.

        Second, this array:
        [code=c]
        int array[5][10];
        [/code]

        is still an array of 5 elements. Each element is an array of 10 int.

        [code=c]
        int array[5][10][15];
        [/code]

        is still an array of 5 elements. Each element is an array of 10 elements where each element is an array of 15 int.


        [code=c]
        int array[][10];
        [/code]

        won't compile. You need to declare the number of elements.

        Third, the name of an array is the address of element 0
        [code=c]
        int array[5];
        [/code]

        Here array is the address of array[0]. Since array[0] is an int, array is the address of an int. You can assign the name array to an int*.

        [code=c]
        int array[5][10];
        [/code]

        Here array is the address of array[0]. Since array[0] is an array of 10 int, array is the address of an array of 10 int. You can assign the name array to a pointer to an array of 10 int:
        [code=c]
        int array[5][10];

        int (*ptr)[10] = array;
        [/code]

        Fourth, when the number of elements is not known at compile time, you create the array dynamically:

        [code=c]
        int* array = new int[value][5];
        int (*ptr)[10] = new int[value][10];
        int (*ptr)[10][15] = new int[value][10][15];
        [/code]

        In each case value is the number of elements. Any other brackets only describe the elements.

        Using an int** for an array of arrays is incorrect and produces wrong answers using pointer arithmetic. The compiler knows this so it won't compile this code:

        [code=c]
        int** ptr = new int[value][10]; //ERROR
        [/code]

        new returns the address of an array of 10 int and that isn't the same as an int**.

        Likewise:
        [code=c]
        int*** ptr = new int[value][10][15]; //ERROR
        [/code]

        new returns the address of an array of 10 elements where each element is an array of 15 int and that isn't the same as an int***.

        Comment

        • Suko
          New Member
          • Sep 2007
          • 6

          #5
          Hi, not sure which compiler your using but Borland C++ and Delphi use a class called DynamicArray with member func's to do all u need.

          Comment

          • Suko
            New Member
            • Sep 2007
            • 6

            #6
            Hello,

            sorry but im not sure about this :-

            int (*ptr)[10] = new int[value][10];
            int (*ptr)[10][15] = new int[value][10][15];

            Can you explain what 'value' is exactly and how it affects the array?
            In the second declaration its an array of 10 ints containing 15 int elements each, but what does 'value' do?

            Comment

            Working...