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 doesn't ^delete^ a reference?

Delete doesn't ^delete^ a reference?

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialquestion
9 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.
  • D Offline
    D Offline
    Dean Seo
    wrote on last edited by
    #1

    Hi, I just found that the keyword Delete doesn't mean ^delete^ the way we(or maybe just I) normally think it does. I thought if you delete a reference, the reference is deleted right after the delete and to have NULL value, which is not so true. Let me show you an example. -------------------------------------------- #include class test { public: char ch; ~test() { printf("End!\n"); } }; class test2 { public: char ch2; ~test2() { printf("End!\n"); } }; int main() { test* callFunc = new test(); callFunc ->ch = 'a'; delete callFunc; test2* callFunc2 = new test2(); printf("%p %p\n",(void*)callFunc,(void*)callFunc2); callFunc2 -> ch2 = 'b'; printf("%c %c \n",callFunc ->ch ,callFunc2 -> ch2); } End! 0395BA8 0395BA8 b b -------------------------------------------- From this result, I want to theorize that the keyword Delete is just to let the references that you delete be available to be pasted later. I'd like to know if my idea is correct or if there are some other reasons for this? Thanks in advance.

    L C J S 4 Replies Last reply
    0
    • D Dean Seo

      Hi, I just found that the keyword Delete doesn't mean ^delete^ the way we(or maybe just I) normally think it does. I thought if you delete a reference, the reference is deleted right after the delete and to have NULL value, which is not so true. Let me show you an example. -------------------------------------------- #include class test { public: char ch; ~test() { printf("End!\n"); } }; class test2 { public: char ch2; ~test2() { printf("End!\n"); } }; int main() { test* callFunc = new test(); callFunc ->ch = 'a'; delete callFunc; test2* callFunc2 = new test2(); printf("%p %p\n",(void*)callFunc,(void*)callFunc2); callFunc2 -> ch2 = 'b'; printf("%c %c \n",callFunc ->ch ,callFunc2 -> ch2); } End! 0395BA8 0395BA8 b b -------------------------------------------- From this result, I want to theorize that the keyword Delete is just to let the references that you delete be available to be pasted later. I'd like to know if my idea is correct or if there are some other reasons for this? Thanks in advance.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      This is one of the anomalies of delete in C++. When an object (or block of memory) is deleted then it is marked as free and may be re-used at some later time. However, the actual memory content, and any pointers to it, will not be physically cleared. If you had followed your delete statement with some other statement that needed to allocate memory, then it is likely that the program would have failed, as callFunc would (probably) be pointing at something that was not a test object. When deleting objects you should always reset any pointers to NULL in order to protect yourself from any potential hazards.

      The best things in life are not things.

      D 1 Reply Last reply
      0
      • D Dean Seo

        Hi, I just found that the keyword Delete doesn't mean ^delete^ the way we(or maybe just I) normally think it does. I thought if you delete a reference, the reference is deleted right after the delete and to have NULL value, which is not so true. Let me show you an example. -------------------------------------------- #include class test { public: char ch; ~test() { printf("End!\n"); } }; class test2 { public: char ch2; ~test2() { printf("End!\n"); } }; int main() { test* callFunc = new test(); callFunc ->ch = 'a'; delete callFunc; test2* callFunc2 = new test2(); printf("%p %p\n",(void*)callFunc,(void*)callFunc2); callFunc2 -> ch2 = 'b'; printf("%c %c \n",callFunc ->ch ,callFunc2 -> ch2); } End! 0395BA8 0395BA8 b b -------------------------------------------- From this result, I want to theorize that the keyword Delete is just to let the references that you delete be available to be pasted later. I'd like to know if my idea is correct or if there are some other reasons for this? Thanks in advance.

        C Offline
        C Offline
        Chris Losinger
        wrote on last edited by
        #3

        Dean Seo wrote:

        if there are some other reasons for this?

        a pointer to a block of allocated memory is not set to NULL when you delete that block of memory. you need to pay attention to that fact when deleting memory. also, in C++ a pointer is not a reference. int foo(int &a, int *b) { ... } a is a reference. b is a pointer.

        image processing toolkits | batch image processing

        D 1 Reply Last reply
        0
        • D Dean Seo

          Hi, I just found that the keyword Delete doesn't mean ^delete^ the way we(or maybe just I) normally think it does. I thought if you delete a reference, the reference is deleted right after the delete and to have NULL value, which is not so true. Let me show you an example. -------------------------------------------- #include class test { public: char ch; ~test() { printf("End!\n"); } }; class test2 { public: char ch2; ~test2() { printf("End!\n"); } }; int main() { test* callFunc = new test(); callFunc ->ch = 'a'; delete callFunc; test2* callFunc2 = new test2(); printf("%p %p\n",(void*)callFunc,(void*)callFunc2); callFunc2 -> ch2 = 'b'; printf("%c %c \n",callFunc ->ch ,callFunc2 -> ch2); } End! 0395BA8 0395BA8 b b -------------------------------------------- From this result, I want to theorize that the keyword Delete is just to let the references that you delete be available to be pasted later. I'd like to know if my idea is correct or if there are some other reasons for this? Thanks in advance.

          J Offline
          J Offline
          jschell
          wrote on last edited by
          #4

          Besides the above comments you might want to read up on what a 'heap' represents in computer science both conceptually and via implementations.

          D 1 Reply Last reply
          0
          • J jschell

            Besides the above comments you might want to read up on what a 'heap' represents in computer science both conceptually and via implementations.

            D Offline
            D Offline
            Dean Seo
            wrote on last edited by
            #5

            Thanks! I will.

            1 Reply Last reply
            0
            • C Chris Losinger

              Dean Seo wrote:

              if there are some other reasons for this?

              a pointer to a block of allocated memory is not set to NULL when you delete that block of memory. you need to pay attention to that fact when deleting memory. also, in C++ a pointer is not a reference. int foo(int &a, int *b) { ... } a is a reference. b is a pointer.

              image processing toolkits | batch image processing

              D Offline
              D Offline
              Dean Seo
              wrote on last edited by
              #6

              Thanks!

              1 Reply Last reply
              0
              • L Lost User

                This is one of the anomalies of delete in C++. When an object (or block of memory) is deleted then it is marked as free and may be re-used at some later time. However, the actual memory content, and any pointers to it, will not be physically cleared. If you had followed your delete statement with some other statement that needed to allocate memory, then it is likely that the program would have failed, as callFunc would (probably) be pointing at something that was not a test object. When deleting objects you should always reset any pointers to NULL in order to protect yourself from any potential hazards.

                The best things in life are not things.

                D Offline
                D Offline
                Dean Seo
                wrote on last edited by
                #7

                Thanks. This helps me a lot.

                1 Reply Last reply
                0
                • D Dean Seo

                  Hi, I just found that the keyword Delete doesn't mean ^delete^ the way we(or maybe just I) normally think it does. I thought if you delete a reference, the reference is deleted right after the delete and to have NULL value, which is not so true. Let me show you an example. -------------------------------------------- #include class test { public: char ch; ~test() { printf("End!\n"); } }; class test2 { public: char ch2; ~test2() { printf("End!\n"); } }; int main() { test* callFunc = new test(); callFunc ->ch = 'a'; delete callFunc; test2* callFunc2 = new test2(); printf("%p %p\n",(void*)callFunc,(void*)callFunc2); callFunc2 -> ch2 = 'b'; printf("%c %c \n",callFunc ->ch ,callFunc2 -> ch2); } End! 0395BA8 0395BA8 b b -------------------------------------------- From this result, I want to theorize that the keyword Delete is just to let the references that you delete be available to be pasted later. I'd like to know if my idea is correct or if there are some other reasons for this? Thanks in advance.

                  S Offline
                  S Offline
                  Stefan_Lang
                  wrote on last edited by
                  #8

                  new does three things: 1. allocate memory from the heap 2. initialize the object at the resulting memory location 3. return a pointer to this new object. What it does not do is set the value of your pointer variable - this is done by the assignment operator '='. delete does the reverse, in reverse order: 1. take a pointer to an existing object 2. destruct the object 3. deallocate the memory it occupied What it does not is reset the value of your pointer variable. Technically it wouldn't be so hard to make delete also reset your pointer variable to 0, but since 'cleaning up' is often done at the end of a function or the end of the scope a pointer is defined in, it is usually not neccessary. Nevertheless it is a good idea to always explicitely set a pointer to 0 after calling delete on it! Even if your delete is the very last line in your code, someone might later add code to that function, past that line. That said, it might have been better if the C++ standard required the delete operator to also set the pointer to 0. But it doesn't. Of course, it is possible to overwrite delete, if you really want it that way... ;)

                  J 1 Reply Last reply
                  0
                  • S Stefan_Lang

                    new does three things: 1. allocate memory from the heap 2. initialize the object at the resulting memory location 3. return a pointer to this new object. What it does not do is set the value of your pointer variable - this is done by the assignment operator '='. delete does the reverse, in reverse order: 1. take a pointer to an existing object 2. destruct the object 3. deallocate the memory it occupied What it does not is reset the value of your pointer variable. Technically it wouldn't be so hard to make delete also reset your pointer variable to 0, but since 'cleaning up' is often done at the end of a function or the end of the scope a pointer is defined in, it is usually not neccessary. Nevertheless it is a good idea to always explicitely set a pointer to 0 after calling delete on it! Even if your delete is the very last line in your code, someone might later add code to that function, past that line. That said, it might have been better if the C++ standard required the delete operator to also set the pointer to 0. But it doesn't. Of course, it is possible to overwrite delete, if you really want it that way... ;)

                    J Offline
                    J Offline
                    jschell
                    wrote on last edited by
                    #9

                    Technically you are describing the "new expression" and "delete expression". Not be confused for example with the new operator and delete operator.

                    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