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

  • Thread starter Thread starter Silicon Waffle
  • Start date Start date
  • Tags Tags
    deleted Php
AI Thread Summary
The discussion centers around a C++ code snippet that demonstrates issues with variable usage and memory management. The code defines a class `A` with a private integer member and methods for setting and printing its value. In the `main` function, an integer pointer `i` is incorrectly assigned a value directly, which leads to confusion about its intended use. The primary concern raised is the misuse of the variable name `i` for both a pointer and an integer, which is considered poor programming practice. Additionally, there is a misunderstanding about memory management, particularly regarding the deletion of the pointer and its impact on the value passed to the class method. The conversation highlights the importance of clear variable naming and understanding how garbage collection works in C++. The correction of the assignment to `*i = 10;` is noted as necessary for proper functionality.
Silicon Waffle
Messages
160
Reaction score
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
Soo sorry I post in the wrong forum. It should be the Programming forums up there. :nb)
Thank you any way:bow:
 
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
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
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
 
Thread 'Star maps using Blender'
Blender just recently dropped a new version, 4.5(with 5.0 on the horizon), and within it was a new feature for which I immediately thought of a use for. The new feature was a .csv importer for Geometry nodes. Geometry nodes are a method of modelling that uses a node tree to create 3D models which offers more flexibility than straight modeling does. The .csv importer node allows you to bring in a .csv file and use the data in it to control aspects of your model. So for example, if you...
I tried a web search "the loss of programming ", and found an article saying that all aspects of writing, developing, and testing software programs will one day all be handled through artificial intelligence. One must wonder then, who is responsible. WHO is responsible for any problems, bugs, deficiencies, or whatever malfunctions which the programs make their users endure? Things may work wrong however the "wrong" happens. AI needs to fix the problems for the users. Any way to...

Similar threads

Replies
5
Views
1K
Replies
1
Views
1K
Replies
22
Views
3K
Replies
13
Views
2K
Replies
6
Views
1K
Replies
25
Views
2K
Replies
3
Views
1K
Back
Top