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. C++ memory freeing

C++ memory freeing

Scheduled Pinned Locked Moved C / C++ / MFC
c++performancequestion
7 Posts 6 Posters 15 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.
  • M Offline
    M Offline
    mike7411
    wrote on last edited by
    #1

    I am a little confused about when to use delete and delete[]. Is this correct?

    int* numArray = new int[100];
    delete numArray;

    Or should it be this?

    int* numArray = new int[100];
    delete[] numArray;

    I saw the former in an MIT lecture, and it looked suspicious. Both of them compile. Thanks.

    L Mircea NeacsuM J 3 Replies Last reply
    0
    • M mike7411

      I am a little confused about when to use delete and delete[]. Is this correct?

      int* numArray = new int[100];
      delete numArray;

      Or should it be this?

      int* numArray = new int[100];
      delete[] numArray;

      I saw the former in an MIT lecture, and it looked suspicious. Both of them compile. Thanks.

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

      If you create an array with operator new as above, then you must tell delete that it is an array. So example 2 is the correct way to do it.

      1 Reply Last reply
      0
      • M mike7411

        I am a little confused about when to use delete and delete[]. Is this correct?

        int* numArray = new int[100];
        delete numArray;

        Or should it be this?

        int* numArray = new int[100];
        delete[] numArray;

        I saw the former in an MIT lecture, and it looked suspicious. Both of them compile. Thanks.

        Mircea NeacsuM Offline
        Mircea NeacsuM Offline
        Mircea Neacsu
        wrote on last edited by
        #3

        Array delete operator, delete[] invokes the destructor of each array element before freeing the array. For an array of integers, it doesn't make any difference. Meanwhile, if the objects in the array have a non-trivial destructor you should call the delete[] operator.

        Mircea

        D 1 Reply Last reply
        0
        • M mike7411

          I am a little confused about when to use delete and delete[]. Is this correct?

          int* numArray = new int[100];
          delete numArray;

          Or should it be this?

          int* numArray = new int[100];
          delete[] numArray;

          I saw the former in an MIT lecture, and it looked suspicious. Both of them compile. Thanks.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          As noted you should use the correct one. I know for a fact that long ago using the wrong one would cause memory stack corruption. I presume that now that isn't possible or is less likely. Although I would be curious if the spec deals with that specific issue.

          K 1 Reply Last reply
          0
          • J jschell

            As noted you should use the correct one. I know for a fact that long ago using the wrong one would cause memory stack corruption. I presume that now that isn't possible or is less likely. Although I would be curious if the spec deals with that specific issue.

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

            In any case, smart pointers are preferable in almost all cases to new/delete. I imagine that there are some cases where new and delete are preferred to smart pointers, but for most applications, a smart pointer is the better option.

            "A little song, a little dance, a little seltzer down your pants" Chuckles the clown

            1 Reply Last reply
            0
            • Mircea NeacsuM Mircea Neacsu

              Array delete operator, delete[] invokes the destructor of each array element before freeing the array. For an array of integers, it doesn't make any difference. Meanwhile, if the objects in the array have a non-trivial destructor you should call the delete[] operator.

              Mircea

              D Offline
              D Offline
              Daniel Pfeffer
              wrote on last edited by
              #6

              Mircea Neacsu wrote:

              For an array of integers, it doesn't make any difference.

              It is true that no destructor is called for each element of an int[] array, but that does not mean that the memory layout is compatible with delete. For example, the compiler might allocate additional space for a variable containing the size of the array and return a pointer to memory after this variable. When using delete, the heap will be corrupted. The heap layout is implementation-dependent, and therefore you should always match new/delete and new[]/delete[]!

              Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

              Mircea NeacsuM 1 Reply Last reply
              0
              • D Daniel Pfeffer

                Mircea Neacsu wrote:

                For an array of integers, it doesn't make any difference.

                It is true that no destructor is called for each element of an int[] array, but that does not mean that the memory layout is compatible with delete. For example, the compiler might allocate additional space for a variable containing the size of the array and return a pointer to memory after this variable. When using delete, the heap will be corrupted. The heap layout is implementation-dependent, and therefore you should always match new/delete and new[]/delete[]!

                Freedom is the freedom to say that two plus two make four. If that is granted, all else follows. -- 6079 Smith W.

                Mircea NeacsuM Offline
                Mircea NeacsuM Offline
                Mircea Neacsu
                wrote on last edited by
                #7

                While you are absolutely right, I don't know of any heap manager that behaves the way you describe. Let's say that calling delete on an int array is a smaller sin, a "peccadillo" that will place you on one of the first circles of hell. :laugh:

                Mircea

                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