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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. delete or delete[]

delete or delete[]

Scheduled Pinned Locked Moved C / C++ / MFC
questiontutoriallearning
11 Posts 5 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.
  • H Hamed Musavi

    I've seen some codes using delete[] blah; and someone else delete blah;. I didn't read in any book how to use them and what's difference. It's a couple of years that I'm programming and using just delete. Does any one know what is the difference and when we need to use those brackets?

    // "Life is very short and is very fragile also." Yanni
    while (I'm_alive)
    {
    cout<<"I love programming.";
    }

    J Offline
    J Offline
    jhwurmbach
    wrote on last edited by
    #2

    Hamed Mosavi wrote:

    delete[] blah; delete blah;. what's difference

    Simple: If the memory was newed with [], use delete[], otherwise normal delete. delete[] calls the destructors of the elements of the array before freeig the memory, whereas delete does not. You might get away with using the wrong one, until you are really under pressure. Then the whole shit will explode in your face.


    Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
    George Orwell, "Keep the Aspidistra Flying", Opening words

    H J 2 Replies Last reply
    0
    • H Hamed Musavi

      I've seen some codes using delete[] blah; and someone else delete blah;. I didn't read in any book how to use them and what's difference. It's a couple of years that I'm programming and using just delete. Does any one know what is the difference and when we need to use those brackets?

      // "Life is very short and is very fragile also." Yanni
      while (I'm_alive)
      {
      cout<<"I love programming.";
      }

      L Offline
      L Offline
      led mike
      wrote on last edited by
      #3

      Hamed Mosavi wrote:

      I didn't read in any book how to use them and what's difference.

      Why not?[^]

      H 1 Reply Last reply
      0
      • J jhwurmbach

        Hamed Mosavi wrote:

        delete[] blah; delete blah;. what's difference

        Simple: If the memory was newed with [], use delete[], otherwise normal delete. delete[] calls the destructors of the elements of the array before freeig the memory, whereas delete does not. You might get away with using the wrong one, until you are really under pressure. Then the whole shit will explode in your face.


        Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
        George Orwell, "Keep the Aspidistra Flying", Opening words

        H Offline
        H Offline
        Hamed Musavi
        wrote on last edited by
        #4

        What a big memory leak in some of my applications.:sigh: Thank you.:)

        // "Life is very short and is very fragile also." Yanni
        while (I'm_alive)
        {
        cout<<"I love programming.";
        }

        N 1 Reply Last reply
        0
        • L led mike

          Hamed Mosavi wrote:

          I didn't read in any book how to use them and what's difference.

          Why not?[^]

          H Offline
          H Offline
          Hamed Musavi
          wrote on last edited by
          #5

          Thanks.

          // "Life is very short and is very fragile also." Yanni
          while (I'm_alive)
          {
          cout<<"I love programming.";
          }

          1 Reply Last reply
          0
          • J jhwurmbach

            Hamed Mosavi wrote:

            delete[] blah; delete blah;. what's difference

            Simple: If the memory was newed with [], use delete[], otherwise normal delete. delete[] calls the destructors of the elements of the array before freeig the memory, whereas delete does not. You might get away with using the wrong one, until you are really under pressure. Then the whole shit will explode in your face.


            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
            George Orwell, "Keep the Aspidistra Flying", Opening words

            J Offline
            J Offline
            jhwurmbach
            wrote on last edited by
            #6

            Wow - four letter words get automagically beeped here. I did write a 'i' instead of a '*" in the post above. -- modified at 11:46 Wednesday 15th August, 2007 Being too soapboxy


            Though I speak with the tongues of men and of angels, and have not money, I am become as a sounding brass, or a tinkling cymbal.
            George Orwell, "Keep the Aspidistra Flying", Opening words

            1 Reply Last reply
            0
            • H Hamed Musavi

              What a big memory leak in some of my applications.:sigh: Thank you.:)

              // "Life is very short and is very fragile also." Yanni
              while (I'm_alive)
              {
              cout<<"I love programming.";
              }

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #7

              Hamed Mosavi wrote:

              What a big memory leak in some of my applications.

              Not necessarily[^] if you were using VC++, at least according to Stan Lippman. But in general, this leads to undefined behavior and should be avoided.


              Programming Blog utf8-cpp

              H 1 Reply Last reply
              0
              • N Nemanja Trifunovic

                Hamed Mosavi wrote:

                What a big memory leak in some of my applications.

                Not necessarily[^] if you were using VC++, at least according to Stan Lippman. But in general, this leads to undefined behavior and should be avoided.


                Programming Blog utf8-cpp

                H Offline
                H Offline
                Hamed Musavi
                wrote on last edited by
                #8

                I was wondering why I didn't notice this type of memory leak reports already. I'm mostly very careful about it. I used vld dll for a while. So it is possible that this does not result in a memory leak in VC++, but worse than that, it might lead to an unknown behavior. Why VC++ compiler doesn't generate a warning? or perhaps it does in level4?(I often use the default level3)

                // "Life is very short and is very fragile also." Yanni
                while (I'm_alive)
                {
                cout<<"I love programming.";
                }

                N 1 Reply Last reply
                0
                • H Hamed Musavi

                  I was wondering why I didn't notice this type of memory leak reports already. I'm mostly very careful about it. I used vld dll for a while. So it is possible that this does not result in a memory leak in VC++, but worse than that, it might lead to an unknown behavior. Why VC++ compiler doesn't generate a warning? or perhaps it does in level4?(I often use the default level3)

                  // "Life is very short and is very fragile also." Yanni
                  while (I'm_alive)
                  {
                  cout<<"I love programming.";
                  }

                  N Offline
                  N Offline
                  Nemanja Trifunovic
                  wrote on last edited by
                  #9

                  Hamed Mosavi wrote:

                  but worse than that, it might lead to an unknown behavior

                  No, my understanding is that with VC++ it works fine either way (haven't tried it though :) ). However, the C++ Standard says that such a case leads to undefined behavior which means that with other compilers (or a new version of VC++) you may see just about anything at all: leaks, crashes, destructors not being called - you name it.


                  Programming Blog utf8-cpp

                  H 1 Reply Last reply
                  0
                  • N Nemanja Trifunovic

                    Hamed Mosavi wrote:

                    but worse than that, it might lead to an unknown behavior

                    No, my understanding is that with VC++ it works fine either way (haven't tried it though :) ). However, the C++ Standard says that such a case leads to undefined behavior which means that with other compilers (or a new version of VC++) you may see just about anything at all: leaks, crashes, destructors not being called - you name it.


                    Programming Blog utf8-cpp

                    H Offline
                    H Offline
                    Hamed Musavi
                    wrote on last edited by
                    #10

                    Thanks.

                    // "Life is very short and is very fragile also." Yanni
                    while (I'm_alive)
                    {
                    cout<<"I love programming.";
                    }

                    1 Reply Last reply
                    0
                    • H Hamed Musavi

                      I've seen some codes using delete[] blah; and someone else delete blah;. I didn't read in any book how to use them and what's difference. It's a couple of years that I'm programming and using just delete. Does any one know what is the difference and when we need to use those brackets?

                      // "Life is very short and is very fragile also." Yanni
                      while (I'm_alive)
                      {
                      cout<<"I love programming.";
                      }

                      K Offline
                      K Offline
                      karle
                      wrote on last edited by
                      #11

                      you must call delete [] if you have an array of objects. if you leave out the [] the destructor only is called for the first object of the list. So, to be sure not to get resource/memory leak you have to use [] for arrays.

                      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