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. How to allocate memory for 2-D integer array in one line?

How to allocate memory for 2-D integer array in one line?

Scheduled Pinned Locked Moved C / C++ / MFC
questioncomdata-structuresperformancetutorial
7 Posts 6 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.
  • N Offline
    N Offline
    Nilesh Hamane
    wrote on last edited by
    #1

    I have this code for allocating memory for 2-d int array.

    int **p = (int **)malloc(MAXROW*sizeof(int *));

    for(int i = 0; i < MAXROW; i++)
    {

    p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
    

    }

    How can i optimize this code in one line?

    http://nnhamane.googlepages.com/

    A C E L 4 Replies Last reply
    0
    • N Nilesh Hamane

      I have this code for allocating memory for 2-d int array.

      int **p = (int **)malloc(MAXROW*sizeof(int *));

      for(int i = 0; i < MAXROW; i++)
      {

      p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
      

      }

      How can i optimize this code in one line?

      http://nnhamane.googlepages.com/

      A Offline
      A Offline
      Avi Berger
      wrote on last edited by
      #2

      :confused:

      Nilesh Hamane wrote:

      How can i optimize this code in one line?

      It is unclear to me what your are asking here. Do you mean by using just one dynamic memory allocation? What aspect of the situation are you trying to optimize - code size or speed; initial setup or use? I am presuming that you are using C and not C++. There are a variety of options that you can use. If this is really critical for your application, you will probably have to run some tests to see what is best in your situation. A second possible approach is to do one allocation of col * row ints and explicitly calculate the offset into this array for each access. A third approach is to keep your first allocation of row pointers to int, supplement it by a second allocation of col * row ints and set each row pointer to a subsequent group of col ints in the second block.

      Please do not read this signature.

      C 1 Reply Last reply
      0
      • A Avi Berger

        :confused:

        Nilesh Hamane wrote:

        How can i optimize this code in one line?

        It is unclear to me what your are asking here. Do you mean by using just one dynamic memory allocation? What aspect of the situation are you trying to optimize - code size or speed; initial setup or use? I am presuming that you are using C and not C++. There are a variety of options that you can use. If this is really critical for your application, you will probably have to run some tests to see what is best in your situation. A second possible approach is to do one allocation of col * row ints and explicitly calculate the offset into this array for each access. A third approach is to keep your first allocation of row pointers to int, supplement it by a second allocation of col * row ints and set each row pointer to a subsequent group of col ints in the second block.

        Please do not read this signature.

        C Offline
        C Offline
        Cool_Dev
        wrote on last edited by
        #3

        as Avi told.. here is how it goes.. //FIRST approacc

        int* p[MAXROW];

        //allocate col*row ints to base ptr
        p[0] = (int*)malloc(MAXROW*MAXCOL*sizeof(int));

        //explicitly calculate the offset into this array
        for(int i=1; i<MAXROW; ++i)
        p[i] = p[i-1] + MAXCOL;

        //SECOND approach

        //first allocation of row pointers to int
        int **p = (int **)malloc(MAXROW*sizeof(int *));

        //second allocation of col*row ints
        p[0] = (int *)malloc(MAXCOL*MAXROW*sizeof(int));

        //set each row pointer to a subsequent group of col ints in the second //block.
        for(int i = 1; i<MAXROW; i++)
        p[i] = p[i-1] + MAXCOL;

        both aproach seems better since all memory allocation is done at once..

        1 Reply Last reply
        0
        • N Nilesh Hamane

          I have this code for allocating memory for 2-d int array.

          int **p = (int **)malloc(MAXROW*sizeof(int *));

          for(int i = 0; i < MAXROW; i++)
          {

          p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
          

          }

          How can i optimize this code in one line?

          http://nnhamane.googlepages.com/

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

          what's wrong with

          int * p = (int *) malloc(COLS * ROWS * sizeof(int));

          ?

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          1 Reply Last reply
          0
          • N Nilesh Hamane

            I have this code for allocating memory for 2-d int array.

            int **p = (int **)malloc(MAXROW*sizeof(int *));

            for(int i = 0; i < MAXROW; i++)
            {

            p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
            

            }

            How can i optimize this code in one line?

            http://nnhamane.googlepages.com/

            E Offline
            E Offline
            Eugen Podsypalnikov
            wrote on last edited by
            #5

            Just make a function void Make2DInt(int**& ppToCreate, int iWidth, int iHeight) and then call it at one line... :)

            Check your definition of Irrationality[^] :) 1 - Avicenna 5 - Hubbard 3 - Own definition

            1 Reply Last reply
            0
            • N Nilesh Hamane

              I have this code for allocating memory for 2-d int array.

              int **p = (int **)malloc(MAXROW*sizeof(int *));

              for(int i = 0; i < MAXROW; i++)
              {

              p\[i\] = (int \*)malloc(MAXCOL\*sizeof(int));
              

              }

              How can i optimize this code in one line?

              http://nnhamane.googlepages.com/

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #6

              Remove all the white space and new lines? ;)

              L u n a t i c F r i n g e

              N 1 Reply Last reply
              0
              • L Lost User

                Remove all the white space and new lines? ;)

                L u n a t i c F r i n g e

                N Offline
                N Offline
                Nilesh Hamane
                wrote on last edited by
                #7

                :laugh:

                Don't die, until you do.

                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