Why Does the Value Persist After Deletion in This PHP Code?

  • PHP
  • Thread starter Silicon Waffle
  • Start date
  • Tags
    deleted Php
In summary, the conversation relates to the use of pointers and objects in C++. The purpose of the line "i = 10" in main is not clear, as it seems to be bad programming practice to use the same variable for two different purposes. The conversation also touches on garbage collection and how it works with deleting objects in memory.
  • #1
Silicon Waffle
160
203
C:
using namespace std;
class A
{
   int i;
public:
   A();
   void Set(int& i){this->i=i;}
   void Print(){cout<<i<<endl;}
};

int main()
{
   int *i=new int;
   A ra;
   i=10;
   ra.Set(*i);
   delete i;
   ra.Print();
}
I think after delete i, the value in Set will also be deleted. Isn't Set(int &) means to pass in it by reference ?
 
Last edited by a moderator:
Technology news on Phys.org
  • #2
Soo sorry I post in the wrong forum. It should be the Programming forums up there. :nb)
Thank you any way:bow:
 
  • #3
It's hard to tell what you're trying to do. It seems to me to be very bad programming practice to use i for two completely different purposes. In class A, i is a private member of type int, but in main, i is a pointer to an int. Programmers almost always use i as a loop control variable.

What is the purpose of this line in main?
i = 10;
After all, i is a pointer in main.
 
Last edited:
  • Like
Likes Silicon Waffle
  • #4
Its good to know how garbage collection works with deleting some things. If I make three variables a,b,c and set a=myobject(), b=a and c=a, then all three variables will be pointers to the same instance of myobject in memory. Garbage collection removes objects in memory nothing is pointing to. That means deleting variable a deletes the object if its the only thing pointing to it, but in the case above its not, b and c also point to it, so by deleting a you can still access the object through b and c.
 
  • Like
Likes Silicon Waffle
  • #5
Mark44 said:
It's hard to tell what you're trying to do. It seems to me to be very bad programming practice to use i for two completely different purposes. In class A, i is a private member of type int, but in main, i is a pointer to an int. Programmers almost always use i as a loop control variable.

What is the purpose of this line in main?
i = 10;
After all, i is a pointer in main.
Yes thanks Mark44, it's *i=10; :-p
 

Related to Why Does the Value Persist After Deletion in This PHP Code?

1. What is "PHP: Deleted but still remains"?

"PHP: Deleted but still remains" is a phrase that refers to the lingering effects of deleting a file or code written in PHP, a popular programming language used for creating dynamic web pages. Essentially, even after deleting the code, remnants of it may still exist and potentially cause issues in the codebase.

2. How does PHP code get deleted but still remain?

There are several ways that PHP code can be deleted but still remain. One common cause is accidentally deleting a file or piece of code that is still being referenced by other parts of the codebase. Additionally, backups or cached versions of the code may still contain the deleted code.

3. What are the potential consequences of PHP code being deleted but still remaining?

If the deleted PHP code is still being referenced by other parts of the codebase, it can cause errors or unexpected behavior. This can be especially problematic if the code was deleted intentionally and the remaining code is no longer relevant or needed. It can also make it more difficult to troubleshoot and debug issues in the code.

4. How can I prevent PHP code from being deleted but still remaining?

To prevent the lingering effects of deleted PHP code, it is important to carefully review any changes made to the codebase and ensure that all references to the deleted code are also removed. It may also be helpful to regularly clean up any backups or cached versions of the code to avoid any conflicts or confusion.

5. Can deleted PHP code be recovered?

In some cases, deleted PHP code can be recovered if it was not permanently deleted or overwritten. This is why it is important to have regular backups of the codebase. However, if the code was intentionally deleted and the remaining code is causing issues, it may be best to rewrite the code rather than attempting to recover it.

Similar threads

  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
3
Views
856
  • Programming and Computer Science
Replies
13
Views
2K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
7
Views
3K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
6
Views
981
  • Programming and Computer Science
Replies
2
Views
807
Back
Top