Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Dynamic Multidimensional Array or Alternatives

Dynamic Multidimensional Array or Alternatives

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
9 Posts 7 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi

    R C R T S 6 Replies Last reply
    0
    • L Lost User

      Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      OK, I'll tell you how to find what you're looking for. :|

      1. Fire up a browser and visit www.google.com.
      2. In the search box, type "multidimensional arrays". (Omit the quotes.)
      3. You'll see a page of links. Click on the second link (www.eskimo.com/~scs/cclass/notes/sx4ba.html).
      4. Read the page content.

      /ravi "There is always one more bug..." http://www.ravib.com ravib@ravib.com

      1 Reply Last reply
      0
      • L Lost User

        Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        i'd use a vector of vectors. -c


        #define O 0.05 #define I(c) putchar(c) main(){int I(int);double l[6];char lO[5];for(*(lO+1)=0;*(lO+1)<'2';I(0x0A),(*(l+5))=-25*O+((*(lO+1) )++)*O)for((*(lO+2))=0;(*(lO+2))<'2';(*(l+4))=-40.*O+((*(lO+2))++)*O){for((*(l))=(*(l+1))=0, *(lO)=1;++*(lO)&&(((*(l+2))=((*(l))*(*(l))))+((*(l+3))=((*(l+1))*(*(l+1))))<4.);(*(l+1))=(*( l))*(*(l+1))+(*(l+5))+(*(l+1))*(*(l)),(*(l))=((*(l+2))-(*(l+3)))+(*(l+4)));I((*(lO)?42:0x20));}}

        1 Reply Last reply
        0
        • L Lost User

          Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi

          R Offline
          R Offline
          Remi Morin
          wrote on last edited by
          #4

          oups Dynamic Multidimensional Array are very easy to create... but harder to use. look at the creation of a 2d Array int x = 100; int y = 200; int *MyArray; MyArray = new int[x][y]; and to acces (here initialisation) you must do: int i,j; for(i=0;i

          1 Reply Last reply
          0
          • L Lost User

            Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi

            T Offline
            T Offline
            Todd Smith
            wrote on last edited by
            #5

            What kind of data do you want to store?

            Todd Smith

            1 Reply Last reply
            0
            • L Lost User

              Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi

              S Offline
              S Offline
              Sprudling
              wrote on last edited by
              #6

              C++ doesn't directly support dynamic multidimensional arrays, however there are ways although a bit more complicated than necessary. Here is another example on how to create one. Unlike Remi Morin's "version" this one is a little harder to create but alot easier to use. And it's alot faster too I guess, since there is no multiplication necessary to access the data. This version does take up a bit more memory though...:

              #include "memory.h"
              #include "new.h"
              //
              void main()
              {
              int x = 4, y = 7, z = 9; // 3D array
              int i, j, k;
              //
              // Allocate memory
              int nSize = x * y * z * sizeof(int);
              int* p = (int*)::operator new(x * y * z * sizeof(int));
              int* q = p; // Used while creating the array
              ::memset(p, 0, nSize); // Initialize all array data to zero
              //
              // Create the array
              int*** a = new int**[x];
              for (i = 0; i < x; i++)
              {
              a[i] = new int*[y];
              for (j = 0; j < y; j++)
              {
              a[i][j] = new (q) int[z];
              q += z;
              }
              }
              //
              // Set values to all values in the array
              for (i = 0; i < x; i++)
              {
              for (j = 0; j < y; j++)
              {
              for (k = 0; k < z; k++)
              {
              a[i][j][k] = i + (j * x) + (k * x * y);
              }
              }
              }
              //
              // Deallocate the array - NB! No destruction
              for (i = 0; i < x; i++)
              {
              delete[] a[i];
              }
              delete[] a;
              ::operator delete(p);
              }

              Remember that the array objects will not be destructed using the destructor (if any) when deleted. It's possible to make that happen too of course by implicitly calling each and every destructor before deleting the array data. This must be done if the data stored in the array is not of a primitive type, or if for some reason the objects do not need to be destructed. The included "new.h" file is necessary for using placement new used in the last for-loop in the process of creating the array. This places the allocated object(s) (int's in this example) at the specified pointer (q). Sprudling :)

              R 1 Reply Last reply
              0
              • L Lost User

                Could someone tell be how or where to find how to make a dinamic multidimensional array or some easier alternatives? Raffi

                M Offline
                M Offline
                Malcolm McMahon
                wrote on last edited by
                #7

                There are all sorts of structures, depending on what you're storing and how the "dynamic" bit occurs. Often you'll want to create a class for each dimension. Along the lines of class Cell { .. whatever each cell stores }; class Row { .. .. .. Cell &operator[](const int idx); } class Matrix { .. .. Row &operator[](const int idx); }; Then you can do: Matrix mat(...); Cell v1,v2; mat[row][column] = v1; v2 = mat[row][col]; How the data are to be organised within the Matrix and Row classes depends on how it's used. Is it sparse? Does it grow and shrink? Are new elements added in the middle or only appended? You can use one of the standard collection classes within your classes, or you can brew your own list or array structure. You may also want to add a exception for index out of range.

                1 Reply Last reply
                0
                • S Sprudling

                  C++ doesn't directly support dynamic multidimensional arrays, however there are ways although a bit more complicated than necessary. Here is another example on how to create one. Unlike Remi Morin's "version" this one is a little harder to create but alot easier to use. And it's alot faster too I guess, since there is no multiplication necessary to access the data. This version does take up a bit more memory though...:

                  #include "memory.h"
                  #include "new.h"
                  //
                  void main()
                  {
                  int x = 4, y = 7, z = 9; // 3D array
                  int i, j, k;
                  //
                  // Allocate memory
                  int nSize = x * y * z * sizeof(int);
                  int* p = (int*)::operator new(x * y * z * sizeof(int));
                  int* q = p; // Used while creating the array
                  ::memset(p, 0, nSize); // Initialize all array data to zero
                  //
                  // Create the array
                  int*** a = new int**[x];
                  for (i = 0; i < x; i++)
                  {
                  a[i] = new int*[y];
                  for (j = 0; j < y; j++)
                  {
                  a[i][j] = new (q) int[z];
                  q += z;
                  }
                  }
                  //
                  // Set values to all values in the array
                  for (i = 0; i < x; i++)
                  {
                  for (j = 0; j < y; j++)
                  {
                  for (k = 0; k < z; k++)
                  {
                  a[i][j][k] = i + (j * x) + (k * x * y);
                  }
                  }
                  }
                  //
                  // Deallocate the array - NB! No destruction
                  for (i = 0; i < x; i++)
                  {
                  delete[] a[i];
                  }
                  delete[] a;
                  ::operator delete(p);
                  }

                  Remember that the array objects will not be destructed using the destructor (if any) when deleted. It's possible to make that happen too of course by implicitly calling each and every destructor before deleting the array data. This must be done if the data stored in the array is not of a primitive type, or if for some reason the objects do not need to be destructed. The included "new.h" file is necessary for using placement new used in the last for-loop in the process of creating the array. This places the allocated object(s) (int's in this example) at the specified pointer (q). Sprudling :)

                  R Offline
                  R Offline
                  Remi Morin
                  wrote on last edited by
                  #8

                  I've try it but I have a problem with this line in debug mode int* p = (int*)::operator new(x * y * z * sizeof(int)); I'm unable to compile. this error raised E:\woop\woopDoc.cpp(827) : error C2665: 'new' : none of the 4 overloads can convert parameter 1 from type 'char [20]' In release mode it's ok it work very fine Why it dos'nt work in Debug? Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

                  S 1 Reply Last reply
                  0
                  • R Remi Morin

                    I've try it but I have a problem with this line in debug mode int* p = (int*)::operator new(x * y * z * sizeof(int)); I'm unable to compile. this error raised E:\woop\woopDoc.cpp(827) : error C2665: 'new' : none of the 4 overloads can convert parameter 1 from type 'char [20]' In release mode it's ok it work very fine Why it dos'nt work in Debug? Remi Morin Rmorin@Operamail.com Remi.Morin@Lyrtech.com

                    S Offline
                    S Offline
                    Sprudling
                    wrote on last edited by
                    #9

                    What?! I have no idea why it want to convert anythingat all from char[20] :eek: Since it's just in debug mode, my best guess is that someone or something somehow has edited your "new.h" file. Try replacing it... Sprudling

                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups