Home > Software engineering >  C pointer to null and assignment after the release, other elements include the pointer containers ho
C pointer to null and assignment after the release, other elements include the pointer containers ho

Time:09-24

The code is as follows:
 
The class SNew
{
Public:
SNew ()
{
M_nValue=https://bbs.csdn.net/topics/1;
}
Virtual ~ SNew ()
{

}

Int m_nValue;
};

Int _tmain (int arg c, _TCHAR * argv [])
{
SNew * pValue=https://bbs.csdn.net/topics/new SNew ();

VectorVpData. Push_back (pValue);
Vectorfor (int i=0; i{
VpData2. Push_back (vpData. At (I));
}

For (vector{
SNew * pTmp=* it;
The delete pTmp;
PTmp=NULL;

It=vpData. Erase (it);
}
SNew * pTmp2=vpData2. Ats (0); //pointer to the pointer to 0 xfeeefeee wild, how to design the NULL?

return 0;
}
PS: not allowed in the for loop vpData2 processing situation, how to make vpData2 elements automatically become NULL?

CodePudding user response:

In the vpData delete vpData2 also deleted

CodePudding user response:

reference 1st floor zgl7903 response:
in the vpData delete vpData2 also delete
vpData delete and vpData2 values are not in a function, here is an example of how the vpData delete after vpData2 access pointer members automatically is empty? Because it refers to the address has been released and assignment to null

CodePudding user response:

This can't, need to manipulate other containers, modify the element value is NULL

CodePudding user response:

Need extra to operate vpData2 ~

CodePudding user response:

By using a container storage, all the other places in the container of pointer to access

CodePudding user response:

 for (vector{
SNew * pTmp=* it;
The delete pTmp;
PTmp=NULL;//this sentence, not the vpData container pointer is set to NULL, but the pTmp point to NULL, when you delete pTmp vpData container when internal pointer is 0 xfeeefeee

It=vpData. Erase (it);
}

CodePudding user response:

Ordinary Pointers can't do what you say.
Only their encapsulation, or use the ready-made smart Pointers.
When a pointer without reference counting, automatic release of memory.
  • Related