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

  • Context: PHP 
  • Thread starter Thread starter Silicon Waffle
  • Start date Start date
  • Tags Tags
    deleted Php
Click For Summary

Discussion Overview

The discussion revolves around a PHP code snippet that raises questions about memory management and variable scope, particularly focusing on the behavior of a deleted pointer and its impact on a class member variable. The scope includes programming practices and potential pitfalls in C++ coding.

Discussion Character

  • Debate/contested
  • Technical explanation

Main Points Raised

  • One participant questions the expectation that the value in the class member variable should be deleted after the pointer is deleted, noting that the function Set(int&) passes by reference.
  • Another participant expresses confusion over the use of the variable name 'i' for different purposes, highlighting that it is a pointer in main but a member variable in class A.
  • A participant discusses garbage collection principles, explaining how multiple pointers can reference the same object and how deletion affects accessibility based on pointer references.
  • A later reply corrects the earlier misunderstanding about assigning a value to the pointer, clarifying that it should be *i = 10 instead of i = 10.

Areas of Agreement / Disagreement

Participants express differing views on the programming practices involved, particularly regarding variable naming and memory management. There is no consensus on the implications of deleting the pointer or the best practices for variable usage.

Contextual Notes

There are unresolved questions about the implications of passing by reference and the effects of deleting pointers on class member variables. The discussion does not clarify the correct approach to memory management in this context.

Who May Find This Useful

Readers interested in programming practices, memory management in C++, and the implications of variable scope may find this discussion relevant.

Silicon Waffle
Messages
160
Reaction score
202
C:
using namespace std;
class A
{
   int i;
public:
   A();
   void Set(int& i){this->i=i;}
   void Print(){count<<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   Reactions: 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   Reactions: 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
 

Similar threads

  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 6 ·
Replies
6
Views
1K
Replies
3
Views
2K
  • · Replies 25 ·
Replies
25
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 23 ·
Replies
23
Views
2K
Replies
7
Views
4K
  • · Replies 3 ·
Replies
3
Views
3K