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. Question about delete a char* pointer

Question about delete a char* pointer

Scheduled Pinned Locked Moved C / C++ / MFC
performancequestionannouncement
7 Posts 4 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.
  • G Offline
    G Offline
    George2
    wrote on last edited by
    #1

    Hi, everyone! Look at the following codes, -------- const char* p = "12345"; char* q = "54321"; -------- If I want to release the memory, should I use -------- delete[] p; delete[] q; -------- or should I use -------- delete p; delete q; -------- Thanks in advance, George

    K J 2 Replies Last reply
    0
    • G George2

      Hi, everyone! Look at the following codes, -------- const char* p = "12345"; char* q = "54321"; -------- If I want to release the memory, should I use -------- delete[] p; delete[] q; -------- or should I use -------- delete p; delete q; -------- Thanks in advance, George

      K Offline
      K Offline
      karl_w
      wrote on last edited by
      #2

      You don't need to release the memory of these variables because they aren't allocated on heap but on stack. Defining char* q ="54321"; is the same as defining char q[6] = "54321";. You only need to use delete/delete [] for variables created with new/new []. -- karl

      G 1 Reply Last reply
      0
      • G George2

        Hi, everyone! Look at the following codes, -------- const char* p = "12345"; char* q = "54321"; -------- If I want to release the memory, should I use -------- delete[] p; delete[] q; -------- or should I use -------- delete p; delete q; -------- Thanks in advance, George

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

        Rule of thumb: You did not call new, you do not need to call delete. For char* r = new char[MAX_PATH+1]; you would call delete[] r; since you did a new[]. But this is one of the cases where it does not matter, since char is an integral type and as such has no constructor or destructor. The difference between delete and delete[] is that delete[] calls the destructor of every item to be deleted, whereas delete calls only the destructor of the first item.


        My opinions may have changed, but not the fact that I am right.

        G P 2 Replies Last reply
        0
        • K karl_w

          You don't need to release the memory of these variables because they aren't allocated on heap but on stack. Defining char* q ="54321"; is the same as defining char q[6] = "54321";. You only need to use delete/delete [] for variables created with new/new []. -- karl

          G Offline
          G Offline
          George2
          wrote on last edited by
          #4

          Thanks, karl buddies! George

          1 Reply Last reply
          0
          • J jhwurmbach

            Rule of thumb: You did not call new, you do not need to call delete. For char* r = new char[MAX_PATH+1]; you would call delete[] r; since you did a new[]. But this is one of the cases where it does not matter, since char is an integral type and as such has no constructor or destructor. The difference between delete and delete[] is that delete[] calls the destructor of every item to be deleted, whereas delete calls only the destructor of the first item.


            My opinions may have changed, but not the fact that I am right.

            G Offline
            G Offline
            George2
            wrote on last edited by
            #5

            Thanks, jhwurmbach buddies! George

            1 Reply Last reply
            0
            • J jhwurmbach

              Rule of thumb: You did not call new, you do not need to call delete. For char* r = new char[MAX_PATH+1]; you would call delete[] r; since you did a new[]. But this is one of the cases where it does not matter, since char is an integral type and as such has no constructor or destructor. The difference between delete and delete[] is that delete[] calls the destructor of every item to be deleted, whereas delete calls only the destructor of the first item.


              My opinions may have changed, but not the fact that I am right.

              P Offline
              P Offline
              peterchen
              wrote on last edited by
              #6

              jhwurmbach wrote: But this is one of the cases where it does not matter, since char is an integral type and as such has no constructor or destructor. This is true on most, but not all compilers. According to the standard, new must be deleted, and new[] delete[]'d. There is no guarantee that the compiler implements new[] "on top of" new. It could even use different heaps for that!


              Nur wer feige ist tötet Liebe durch das Wort allein
              [sighist] | [Agile Programming] [doxygen]
              If you look for evil in me you will find it whether it's there or not.

              G 1 Reply Last reply
              0
              • P peterchen

                jhwurmbach wrote: But this is one of the cases where it does not matter, since char is an integral type and as such has no constructor or destructor. This is true on most, but not all compilers. According to the standard, new must be deleted, and new[] delete[]'d. There is no guarantee that the compiler implements new[] "on top of" new. It could even use different heaps for that!


                Nur wer feige ist tötet Liebe durch das Wort allein
                [sighist] | [Agile Programming] [doxygen]
                If you look for evil in me you will find it whether it's there or not.

                G Offline
                G Offline
                George2
                wrote on last edited by
                #7

                Thanks, peterchen buddie! George

                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