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. Release memory using 'delete' operator for 2 dimensional array

Release memory using 'delete' operator for 2 dimensional array

Scheduled Pinned Locked Moved C / C++ / MFC
data-structuresperformancequestionannouncement
5 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.
  • N Offline
    N Offline
    Nikesh Jagtap
    wrote on last edited by
    #1

    I want to release memory for int a[4][6] which is allocated dynamically. I m allocating this memory as for(int i = 0;i<4; i++) { a[i] = new int [6]; } And deallocating it as for(int j = 0; j<4; j++) { delete []a[i]; } Is the process of allocating & deallocating memory is right or not? Thanks in advance. Regards Nikesh

    C CPalliniC D 3 Replies Last reply
    0
    • N Nikesh Jagtap

      I want to release memory for int a[4][6] which is allocated dynamically. I m allocating this memory as for(int i = 0;i<4; i++) { a[i] = new int [6]; } And deallocating it as for(int j = 0; j<4; j++) { delete []a[i]; } Is the process of allocating & deallocating memory is right or not? Thanks in advance. Regards Nikesh

      C Offline
      C Offline
      Code o mat
      wrote on last edited by
      #2

      Seems to be but in your second loop you are using i and not j as you probably should, that!s just a copy-paste problem in the post here i supose...

      for(int j = 0; j<4; j++)
      {
      delete []a[i]; // here you wrote i, not j, that might cause problems if this is actually in your code
      }

      > The problem with computers is that they do what you tell them to do and not what you want them to do. <

      1 Reply Last reply
      0
      • N Nikesh Jagtap

        I want to release memory for int a[4][6] which is allocated dynamically. I m allocating this memory as for(int i = 0;i<4; i++) { a[i] = new int [6]; } And deallocating it as for(int j = 0; j<4; j++) { delete []a[i]; } Is the process of allocating & deallocating memory is right or not? Thanks in advance. Regards Nikesh

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        as addendum to Code-o-mat, you may use (instead of hard-wired limits)

        for (int j=0; j< sizeof(a)/sizeof(a[0]); j++)
        {
        delete [] a[j];
        }

        :)

        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]

        In testa che avete, signor di Ceprano?

        C 1 Reply Last reply
        0
        • CPalliniC CPallini

          as addendum to Code-o-mat, you may use (instead of hard-wired limits)

          for (int j=0; j< sizeof(a)/sizeof(a[0]); j++)
          {
          delete [] a[j];
          }

          :)

          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]

          C Offline
          C Offline
          Code o mat
          wrote on last edited by
          #4

          Yeah, i wonder why C++ doesn't have something like an itemcountof or such operator for that by default. Many times it would be useful, i keep making macros for sizeof(array)/sizeof(array[0]) and i keep forgetting where i put them. :)

          > The problem with computers is that they do what you tell them to do and not what you want them to do. <

          1 Reply Last reply
          0
          • N Nikesh Jagtap

            I want to release memory for int a[4][6] which is allocated dynamically. I m allocating this memory as for(int i = 0;i<4; i++) { a[i] = new int [6]; } And deallocating it as for(int j = 0; j<4; j++) { delete []a[i]; } Is the process of allocating & deallocating memory is right or not? Thanks in advance. Regards Nikesh

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

            Nikesh Jagtap wrote:

            int a[4][6] which is allocated dynamically.

            Not unless you've declared it as:

            int **a = new int*[4];

            "Love people and use things, not love things and use people." - Unknown

            "The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch

            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