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. about "delete"

about "delete"

Scheduled Pinned Locked Moved C / C++ / MFC
7 Posts 5 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.
  • S Offline
    S Offline
    simon wan
    wrote on last edited by
    #1

    ...... char* p= new char[100]; ... //should I use // delete p; //or // delete[] p;

    P J 2 Replies Last reply
    0
    • S simon wan

      ...... char* p= new char[100]; ... //should I use // delete p; //or // delete[] p;

      P Offline
      P Offline
      PJ Arends
      wrote on last edited by
      #2

      delete[] new -> delete new[] -> delete[]


      "You're obviously a superstar." - Christian Graus about me - 12 Feb '03 "Obviously ???  You're definitely a superstar!!!" mYkel - 21 Jun '04 Within you lies the power for good - Use it!


      Honoured as one of The Most Helpful Members of 2004

      1 Reply Last reply
      0
      • S simon wan

        ...... char* p= new char[100]; ... //should I use // delete p; //or // delete[] p;

        J Offline
        J Offline
        Jorgen Sigvardsson
        wrote on last edited by
        #3

        I'd like to stress PJ's point that you should *always* use delete [] with new []. Otherwise strange things will happen. Trust me. :) -- Schni Schna Schnappi! Schnappi Schnappi Schnapp!

        N 1 Reply Last reply
        0
        • J Jorgen Sigvardsson

          I'd like to stress PJ's point that you should *always* use delete [] with new []. Otherwise strange things will happen. Trust me. :) -- Schni Schna Schnappi! Schnappi Schnappi Schnapp!

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

          I'd suggest he (almost) never uses new[] and delete[] and make use of container classes instead. Here is why[^]


          My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

          H 1 Reply Last reply
          0
          • N Nemanja Trifunovic

            I'd suggest he (almost) never uses new[] and delete[] and make use of container classes instead. Here is why[^]


            My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

            H Offline
            H Offline
            hfrmobile
            wrote on last edited by
            #5

            I allways used delete regardless if used new or new[] ...it worked fine (and I see no reason why it should not...) byte buffer[4] = new byte[4]; // 4 bytes DWORD dw4Bytes = new DWORD[1]; // 4 bytes ... delete buffer; // releases 4 bytes delete dw4Bytes; // releases 4 bytes

            N 1 Reply Last reply
            0
            • H hfrmobile

              I allways used delete regardless if used new or new[] ...it worked fine (and I see no reason why it should not...) byte buffer[4] = new byte[4]; // 4 bytes DWORD dw4Bytes = new DWORD[1]; // 4 bytes ... delete buffer; // releases 4 bytes delete dw4Bytes; // releases 4 bytes

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

              rubicon_hfr wrote: ...it worked fine (and I see no reason why it should not...) Because for one thing, your example is not exception-safe. If in the "..." code an exception is thrown, you'll end up with a memory leak.


              My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

              H 1 Reply Last reply
              0
              • N Nemanja Trifunovic

                rubicon_hfr wrote: ...it worked fine (and I see no reason why it should not...) Because for one thing, your example is not exception-safe. If in the "..." code an exception is thrown, you'll end up with a memory leak.


                My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

                H Offline
                H Offline
                hfrmobile
                wrote on last edited by
                #7

                Because for one thing, your example is not exception-safe. If in the "..." code an exception is thrown, you'll end up with a memory leak. Since the topic has the subject "about delete" I only mention that there is no difference between delete and delete[]. You may assume that "..." stands for the following: try { // do something meaningful } catch (...) { } Of course you can use finally to free memory... :rose: Or you just use a wrapper class which ctor does this for you (so you can't forget)... Or use managed C++ :laugh:

                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