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 or delete [ ]??

delete or delete [ ]??

Scheduled Pinned Locked Moved C / C++ / MFC
question
8 Posts 6 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.
  • F Offline
    F Offline
    funwithdolphin
    wrote on last edited by
    #1

    In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;

    _ C S T 4 Replies Last reply
    0
    • F funwithdolphin

      In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;

      _ Offline
      _ Offline
      __yb
      wrote on last edited by
      #2

      delete[] but they get to the same place anyway..

      C T _ 3 Replies Last reply
      0
      • F funwithdolphin

        In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        delete[] because the memory has been allocated with new[]. The rule is simple: if memory is allocated with new[], then use delete[]. If memory is allocated with new, use delete.

        1 Reply Last reply
        0
        • _ __yb

          delete[] but they get to the same place anyway..

          C Offline
          C Offline
          Cedric Moonen
          wrote on last edited by
          #4

          __yb wrote:

          but they get to the same place anyway..

          What do you mean by going to the same place ? If you mean that you can use one instead of the other, it's not true. Check this link[^]

          1 Reply Last reply
          0
          • F funwithdolphin

            In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;

            S Offline
            S Offline
            sunit5
            wrote on last edited by
            #5

            if u r allocating an array of data say *ptr=new [100] then go for delete[] complier will know an array of data has to be deleted.Otherwise if u dont do this only the first block of data (depending upon data type say char only first byte in case of char will be deleted this memory leak!) remember this combination new-->delete new[]-->delete[]

            1 Reply Last reply
            0
            • F funwithdolphin

              In the following code snippet which delete should I use?? char *ptr1=new char[100]; char arr[10]="abcdefghi"; strcpy(ptr1,arr); //delete ptr1; delete []ptr1; Should I use delete ptr1; or delete []ptr1;

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

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


              TOXCCT >>> GEII power
              [toxcct][VisualCalc]

              1 Reply Last reply
              0
              • _ __yb

                delete[] but they get to the same place anyway..

                T Offline
                T Offline
                Tim Smith
                wrote on last edited by
                #7

                They in fact do not. However, it is common for POD arrays to both compile down to a call to free. But you should never rely on that since it is implementation specific. Tim Smith I'm going to patent thought. I have yet to see any prior art.

                1 Reply Last reply
                0
                • _ __yb

                  delete[] but they get to the same place anyway..

                  _ Offline
                  _ Offline
                  __yb
                  wrote on last edited by
                  #8

                  u're right, its per implementation, I was refering to mine - VC6

                  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