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
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...

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