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 2 dimensional array

dynamic 2 dimensional array

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformance
11 Posts 6 Posters 1 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.
  • T Offline
    T Offline
    Tyrus182
    wrote on last edited by
    #1

    Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks

    C B A R 4 Replies Last reply
    0
    • T Tyrus182

      Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks

      C Offline
      C Offline
      Curi0us_George
      wrote on last edited by
      #2

      The only way I know to do it is to create an array of int* and then assign each element to point to an array. e.g. int ** array = new int*[2]; for (int i = 0; i < 2; i++) { array[i] = new int[2]; }

      A 1 Reply Last reply
      0
      • T Tyrus182

        Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks

        B Offline
        B Offline
        Bob Stanneveld
        wrote on last edited by
        #3

        Why don't you use a vector instead? They are much safer and easier to handle...

        A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

        C 1 Reply Last reply
        0
        • B Bob Stanneveld

          Why don't you use a vector instead? They are much safer and easier to handle...

          A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

          C Offline
          C Offline
          Curi0us_George
          wrote on last edited by
          #4

          ... and also one dimensional.

          B 1 Reply Last reply
          0
          • C Curi0us_George

            ... and also one dimensional.

            B Offline
            B Offline
            Bob Stanneveld
            wrote on last edited by
            #5

            vector < vector < int > > Matrix;

            and now you have a 2d array... ;P

            A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

            C 1 Reply Last reply
            0
            • B Bob Stanneveld

              vector < vector < int > > Matrix;

              and now you have a 2d array... ;P

              A student knows little about a lot. A professor knows a lot about little. I know everything about nothing.

              C Offline
              C Offline
              Curi0us_George
              wrote on last edited by
              #6

              I suppose that works, but ick. I guess if it does the job for you, though . . .

              1 Reply Last reply
              0
              • C Curi0us_George

                The only way I know to do it is to create an array of int* and then assign each element to point to an array. e.g. int ** array = new int*[2]; for (int i = 0; i < 2; i++) { array[i] = new int[2]; }

                A Offline
                A Offline
                Alexander M
                wrote on last edited by
                #7

                this is definitly the most silly way. for every new an entry in the heap will be created which will decrese the speed of your app dramatically if there are many fields for the array. Don't try it, just do it! ;-)

                C 1 Reply Last reply
                0
                • T Tyrus182

                  Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks

                  A Offline
                  A Offline
                  Alexander M
                  wrote on last edited by
                  #8

                  there is a much better and faster way!

                  int **array;
                  int i, j;

                  array = new int[4]; 2*2=4
                  for( i = 0; i < 2; i++ )
                  {
                  for( j = 0; j < 2; j++ )
                  {
                  array[i*2+j] = i;
                  // ....
                  }
                  }

                  this is the fastest way Don't try it, just do it! ;-)

                  D 1 Reply Last reply
                  0
                  • A Alexander M

                    there is a much better and faster way!

                    int **array;
                    int i, j;

                    array = new int[4]; 2*2=4
                    for( i = 0; i < 2; i++ )
                    {
                    for( j = 0; j < 2; j++ )
                    {
                    array[i*2+j] = i;
                    // ....
                    }
                    }

                    this is the fastest way Don't try it, just do it! ;-)

                    D Offline
                    D Offline
                    David Crow
                    wrote on last edited by
                    #9

                    Alexander M. wrote: this is the fastest way Unless you have some metrics to support this, speed is probably going to be irrelevant. Your approach has a major flaw; namely that it is not intuitive. Read my response here as to why array[i][j] is much preferred over array[i*2+j].


                    "The pointy end goes in the other man." - Antonio Banderas (Zorro, 1998)

                    1 Reply Last reply
                    0
                    • A Alexander M

                      this is definitly the most silly way. for every new an entry in the heap will be created which will decrese the speed of your app dramatically if there are many fields for the array. Don't try it, just do it! ;-)

                      C Offline
                      C Offline
                      Curi0us_George
                      wrote on last edited by
                      #10

                      Pft. Allocation of the array is not typically the source of speed overhead. Accessing array positions repeatedly is, and your way is no faster than mine. Mine is at least intuitive.

                      1 Reply Last reply
                      0
                      • T Tyrus182

                        Hi i know this is probably a stupid question but whats the proper syntax for for allocating memory for a 2d array? i tried int **array; array = new int[2][2] even tried the second line with *array this wont even compile so wahts the correct way to do this? thanks

                        R Offline
                        R Offline
                        Rick York
                        wrote on last edited by
                        #11

                        Here's what I do :

                        = calloc( ydim, yitmsize ); if( data[x] == NULL ) { for( i = 0; i < x; i++ ) // free all we have so far free( data[i] ); free( data ); return NULL; } } return data; } // a general 2d array release function void **Free2DArray( void **array, int xdim ) { int x; if( array == NULL ) return NULL; // sanity check for( x = xdim - 1; x >= 0; x-- ) { free( array[x] ); array[x] = NULL; } free( array ); return NULL; } // a specialized two-dimensional matrix allocator // this function handles all casting for us. float **AllocFloatMatrix( int xdim, int ydim ) { float **data = (float **)Alloc2DArray( xdim, sizeof( float * ), ydim, sizeof( float ) ); return data; } // a specialized two-dimensional matrix deallocator // this function handles all casting for us. float **FreeFloatMatrix( float **array, int xdim ) { Free2DArray( (void **)array, xdim ); return NULL; }
                        to access data which I find that to be reasonably clean. __________________________________________ a two cent stamp short of going postal.

                        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