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 allocation of multidimensional arrays

Dynamic allocation of multidimensional arrays

Scheduled Pinned Locked Moved C / C++ / MFC
6 Posts 4 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.
  • S Offline
    S Offline
    Sprudling
    wrote on last edited by
    #1

    How can I create a multidimensional array of int's (or whatever) when I don't know the dimensions at compile time? I've tried alot of stuff, but the compiler still hasn't accepted any of my desperate tries. Please help me... :confused: In my case, I want the user to decide at runtime the lenght and width of a labyrint, that should be generated afterwards. I'm aware that there are other alternatives to multidimensional arrays, and I'm open to other good suggestions too. Sprudling

    S M 2 Replies Last reply
    0
    • S Sprudling

      How can I create a multidimensional array of int's (or whatever) when I don't know the dimensions at compile time? I've tried alot of stuff, but the compiler still hasn't accepted any of my desperate tries. Please help me... :confused: In my case, I want the user to decide at runtime the lenght and width of a labyrint, that should be generated afterwards. I'm aware that there are other alternatives to multidimensional arrays, and I'm open to other good suggestions too. Sprudling

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

      This is the best I've managed to do so far...

      #include "iostream.h"

      int main(int argc, char* argv[])
      {
      // Set the dimensions of the array
      int nA = 5;
      int nB = 4;

      // "Create" the 1. dimension of the array
      int** a = new int*[nA];

      // "Create" the 2. dimension of the array
      for (int i1 = 0; i1 < nA; i1++)
      {
      a[i1] = new int[nB];
      }

      // Initialize all the array elements
      for (i1 = 0; i1 < nA; i1++)
      {
      for (int i2 = 0; i2 < nB; i2++)
      {
      a[i1][i2] = (i1 * nB) + i2;
      }
      }

      // Print all the array elements to the screen
      for (i1 = 0; i1 < nA; i1++)
      {
      for (int i2 = 0; i2 < nB; i2++)
      {
      cout << "[" << i1 << "][" << i2 << "] : " << a[i1][i2] << endl;
      }
      }

      // Deallocate the array
      for (i1 = 0; i1 < nA; i1++)
      {
      delete[] a[i1];
      }
      delete[] a;

      return 0;
      }

      It seems to work ok, but there is alot of code just for creating an array, and I'll have to keep track of the size of the 1. dimension of the array, and that's not always simple. Isn't there a better way? :confused: Sprudling

      M 1 Reply Last reply
      0
      • S Sprudling

        This is the best I've managed to do so far...

        #include "iostream.h"

        int main(int argc, char* argv[])
        {
        // Set the dimensions of the array
        int nA = 5;
        int nB = 4;

        // "Create" the 1. dimension of the array
        int** a = new int*[nA];

        // "Create" the 2. dimension of the array
        for (int i1 = 0; i1 < nA; i1++)
        {
        a[i1] = new int[nB];
        }

        // Initialize all the array elements
        for (i1 = 0; i1 < nA; i1++)
        {
        for (int i2 = 0; i2 < nB; i2++)
        {
        a[i1][i2] = (i1 * nB) + i2;
        }
        }

        // Print all the array elements to the screen
        for (i1 = 0; i1 < nA; i1++)
        {
        for (int i2 = 0; i2 < nB; i2++)
        {
        cout << "[" << i1 << "][" << i2 << "] : " << a[i1][i2] << endl;
        }
        }

        // Deallocate the array
        for (i1 = 0; i1 < nA; i1++)
        {
        delete[] a[i1];
        }
        delete[] a;

        return 0;
        }

        It seems to work ok, but there is alot of code just for creating an array, and I'll have to keep track of the size of the 1. dimension of the array, and that's not always simple. Isn't there a better way? :confused: Sprudling

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        The way you're doing it is the correct way. :) --Mike-- http://home.inreach.com/mdunn/ Ford: How would you react if I said that I'm not from Guildford after all, but from a small planet somewhere in the vicinity of Betelguese? Arthur: I don't know. Why, do you think it's the sort of thing you're likely to say?

        N S 2 Replies Last reply
        0
        • M Michael Dunn

          The way you're doing it is the correct way. :) --Mike-- http://home.inreach.com/mdunn/ Ford: How would you react if I said that I'm not from Guildford after all, but from a small planet somewhere in the vicinity of Betelguese? Arthur: I don't know. Why, do you think it's the sort of thing you're likely to say?

          N Offline
          N Offline
          Nick Blumhardt
          wrote on last edited by
          #4

          this is the perfect place for some cool template code :) any takers?

          1 Reply Last reply
          0
          • M Michael Dunn

            The way you're doing it is the correct way. :) --Mike-- http://home.inreach.com/mdunn/ Ford: How would you react if I said that I'm not from Guildford after all, but from a small planet somewhere in the vicinity of Betelguese? Arthur: I don't know. Why, do you think it's the sort of thing you're likely to say?

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

            What if I want like 10 different arrays with 10 or more dimensions? That would be alot of code. And are you telling me that C++ hasn't support for dynamic multidimensional arrays?! If so, I'm sure this has been a "problem" for a long time, and someone must have come up with something... A quick and easy general way to create arrays of N dimensions. If there exist one, I really want to know :) Sprudling

            1 Reply Last reply
            0
            • S Sprudling

              How can I create a multidimensional array of int's (or whatever) when I don't know the dimensions at compile time? I've tried alot of stuff, but the compiler still hasn't accepted any of my desperate tries. Please help me... :confused: In my case, I want the user to decide at runtime the lenght and width of a labyrint, that should be generated afterwards. I'm aware that there are other alternatives to multidimensional arrays, and I'm open to other good suggestions too. Sprudling

              M Offline
              M Offline
              MehranNZ
              wrote on last edited by
              #6

              here you go man i think it is the best way to do that int main() { double (* pX)[4]; pX = new double [3][4]; delete [] pX; return 0; }

              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