Delete doesn't ^delete^ a reference?
-
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.
-
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.
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 yourdelete
statement with some other statement that needed to allocate memory, then it is likely that the program would have failed, ascallFunc
would (probably) be pointing at something that was not atest
object. When deleting objects you should always reset any pointers toNULL
in order to protect yourself from any potential hazards.The best things in life are not things.
-
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.
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.
-
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.
-
Besides the above comments you might want to read up on what a 'heap' represents in computer science both conceptually and via implementations.
-
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.
-
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 yourdelete
statement with some other statement that needed to allocate memory, then it is likely that the program would have failed, ascallFunc
would (probably) be pointing at something that was not atest
object. When deleting objects you should always reset any pointers toNULL
in order to protect yourself from any potential hazards.The best things in life are not things.
-
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.
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 callingdelete
on it! Even if yourdelete
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 overwritedelete
, if you really want it that way... ;) -
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 callingdelete
on it! Even if yourdelete
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 overwritedelete
, if you really want it that way... ;)