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 double dimension

Dynamic double dimension

Scheduled Pinned Locked Moved C / C++ / MFC
helpdata-structures
11 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.
  • A Offline
    A Offline
    Arrun
    wrote on last edited by
    #1

    I am using the following code to allocate dynamic double dimensional array double **ImagePro; try { ImagePro = new double*[width]; for(int i = 0; i < height; i++) { ImagePro[i] = new double[width]; } } catch(CMemoryException* ex) { ImagePro = NULL; ex->ReportError(); return FALSE; } and for deletion for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro; return TRUE; My problem is whenever the height is greater than width I get assertion error message Pls Help

    K C D 3 Replies Last reply
    0
    • A Arrun

      I am using the following code to allocate dynamic double dimensional array double **ImagePro; try { ImagePro = new double*[width]; for(int i = 0; i < height; i++) { ImagePro[i] = new double[width]; } } catch(CMemoryException* ex) { ImagePro = NULL; ex->ReportError(); return FALSE; } and for deletion for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro; return TRUE; My problem is whenever the height is greater than width I get assertion error message Pls Help

      K Offline
      K Offline
      kakan
      wrote on last edited by
      #2

      Hello. Try changing: ImagePro = new double*[width]; to ImagePro = new double*[height];

      1 Reply Last reply
      0
      • A Arrun

        I am using the following code to allocate dynamic double dimensional array double **ImagePro; try { ImagePro = new double*[width]; for(int i = 0; i < height; i++) { ImagePro[i] = new double[width]; } } catch(CMemoryException* ex) { ImagePro = NULL; ex->ReportError(); return FALSE; } and for deletion for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro; return TRUE; My problem is whenever the height is greater than width I get assertion error message Pls Help

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Did you try this ? for(int i = 0; i < height; i++) { delete**[]** ImagePro[i]; } delete**[]** ImagePro; And wich part of the code is asserting ?

        A 1 Reply Last reply
        0
        • C Cedric Moonen

          Did you try this ? for(int i = 0; i < height; i++) { delete**[]** ImagePro[i]; } delete**[]** ImagePro; And wich part of the code is asserting ?

          A Offline
          A Offline
          Arrun
          wrote on last edited by
          #4

          delete[] ImagePro is not working I get assertion in "delete ImagePro;"

          K 1 Reply Last reply
          0
          • A Arrun

            delete[] ImagePro is not working I get assertion in "delete ImagePro;"

            K Offline
            K Offline
            kakan
            wrote on last edited by
            #5

            You should, since delete ImagePro[0]; Is the same as delete ImagePro; So when you the delete ImagePro, its already deleted.

            A 1 Reply Last reply
            0
            • K kakan

              You should, since delete ImagePro[0]; Is the same as delete ImagePro; So when you the delete ImagePro, its already deleted.

              A Offline
              A Offline
              Arrun
              wrote on last edited by
              #6

              It works fine when the height and width are equal or if the height is less than width The error occurs only when the height is greater than the width. BoundsChecker displays an error message "Dynamic memory overrun"

              K 1 Reply Last reply
              0
              • A Arrun

                It works fine when the height and width are equal or if the height is less than width The error occurs only when the height is greater than the width. BoundsChecker displays an error message "Dynamic memory overrun"

                K Offline
                K Offline
                kakan
                wrote on last edited by
                #7

                Hello. See my earlier post. You allocate no of items twice. You don't use , except in the allocating loop. Since you allocate room for items in your table. So it's natural that you go out of bounds if height is bigger than what you allocated (which is items). Kakan

                A 1 Reply Last reply
                0
                • K kakan

                  Hello. See my earlier post. You allocate no of items twice. You don't use , except in the allocating loop. Since you allocate room for items in your table. So it's natural that you go out of bounds if height is bigger than what you allocated (which is items). Kakan

                  A Offline
                  A Offline
                  Arrun
                  wrote on last edited by
                  #8

                  so how should I allocate and delete dynamic memory when the width and height are different. help me with some code.

                  K 2 Replies Last reply
                  0
                  • A Arrun

                    so how should I allocate and delete dynamic memory when the width and height are different. help me with some code.

                    K Offline
                    K Offline
                    kakan
                    wrote on last edited by
                    #9

                    Sorry, I'm at work and have work to do. But there is HEAPS of source code for dynamic 2-dimensional arrays out there. Serach at CodeProject and/or Google. I think you need to find a working example to start with. Good luck. Kakan.

                    1 Reply Last reply
                    0
                    • A Arrun

                      so how should I allocate and delete dynamic memory when the width and height are different. help me with some code.

                      K Offline
                      K Offline
                      kakan
                      wrote on last edited by
                      #10

                      Hello again. I couldn't let go of your problem. :) I found a lot of suggestions at experts exhange, at: http://www.experts-exchange.com/Programming/Programming_Languages/Cplusplus/Q_21442835.html[^] This example looks interesting, and complete: template < typename T > T **Allocate2DArray( int nRows, int nCols) { T **ppi; T *pool; T *curPtr; //(step 1) allocate memory for array of elements of column ppi = new T*[nRows]; //(step 2) allocate memory for array of elements of each row pool = new T [nRows * nCols]; // Now point the pointers in the right place curPtr = pool; for( int i = 0; i < nRows; i++) { *(ppi + i) = curPtr; curPtr += nCols; } return ppi; } template < typename T > void Free2DArray(T** Array) { delete [] *Array; delete [] Array; } int main() { double **d = Allocate2DArray(10000, 10000); d[0][0] = 10.0; d[1][1] = 20.0; d[9999][9999] = 2345.09; Free2DArray(d); } In the main function, you can see how to use the array. I think you should try it out. Regards Kakan

                      1 Reply Last reply
                      0
                      • A Arrun

                        I am using the following code to allocate dynamic double dimensional array double **ImagePro; try { ImagePro = new double*[width]; for(int i = 0; i < height; i++) { ImagePro[i] = new double[width]; } } catch(CMemoryException* ex) { ImagePro = NULL; ex->ReportError(); return FALSE; } and for deletion for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro; return TRUE; My problem is whenever the height is greater than width I get assertion error message Pls Help

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

                        Arrun wrote:

                        ImagePro = new double*[width]; for(int i = 0; i < height; i++)

                        If you allocate room for width double pointers, it only makes sense that the for loop execute only that many times. If height happens to be larger than width, an error should be expected. See here for an example.

                        Arrun wrote:

                        for(int i = 0; i < height; i++) { delete ImagePro[i]; } delete ImagePro;

                        Change to:

                        for(int i = 0; i < height; i++)
                        delete [] ImagePro[i];

                        delete [] ImagePro;


                        "Take only what you need and leave the land as you found it." - Native American Proverb

                        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