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. delete vs. delete []

delete vs. delete []

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++visual-studiodata-structuresdebugging
4 Posts 3 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.
  • E Offline
    E Offline
    Eikthrynir
    wrote on last edited by
    #1

    Hi! I have a question concerning the use of delete keyword in C++ programs. First, I do something like this: int *pInt = new int [16]; delete pInt; All the documentation I have read about delete sais that you must use delete [] when the allocated memory is an array. But if I run the small program containing these two lines of code in Debug mode, the _CrtDumpMemoryLeaks function tells me that the is no memory leak. In conclusion, why is there any need to deallocate the memory pointed by pInt using delete [] pInt; if it works using only delete pInt? If anyone can answer my question, I would greatly appreciate it! Thanks in advance!

    G T 2 Replies Last reply
    0
    • E Eikthrynir

      Hi! I have a question concerning the use of delete keyword in C++ programs. First, I do something like this: int *pInt = new int [16]; delete pInt; All the documentation I have read about delete sais that you must use delete [] when the allocated memory is an array. But if I run the small program containing these two lines of code in Debug mode, the _CrtDumpMemoryLeaks function tells me that the is no memory leak. In conclusion, why is there any need to deallocate the memory pointed by pInt using delete [] pInt; if it works using only delete pInt? If anyone can answer my question, I would greatly appreciate it! Thanks in advance!

      G Offline
      G Offline
      George L Jackson
      wrote on last edited by
      #2

      Please read: http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.13[^]

      E 1 Reply Last reply
      0
      • G George L Jackson

        Please read: http://www.parashift.com/c++-faq-lite/freestore-mgmt.html#faq-16.13[^]

        E Offline
        E Offline
        Eikthrynir
        wrote on last edited by
        #3

        Thank you! It helps a lot!

        1 Reply Last reply
        0
        • E Eikthrynir

          Hi! I have a question concerning the use of delete keyword in C++ programs. First, I do something like this: int *pInt = new int [16]; delete pInt; All the documentation I have read about delete sais that you must use delete [] when the allocated memory is an array. But if I run the small program containing these two lines of code in Debug mode, the _CrtDumpMemoryLeaks function tells me that the is no memory leak. In conclusion, why is there any need to deallocate the memory pointed by pInt using delete [] pInt; if it works using only delete pInt? If anyone can answer my question, I would greatly appreciate it! Thanks in advance!

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          Eikthrynir wrote:

          lete [] pInt

          actually [] instruct compiler to delete the array of Memory block here.. if you use simple delete then only one memory block will be freed

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and you

          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