Click here to Skip to main content
15,898,134 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a doubt about verifying a dangling pointer in pointer aliasing

C++
CMyClass* pObj = new CMyClass();

pObj will store address (say K) of a variable in it. i.e. pObj pointing to variable stored at address K.
While pObj itself will have some address (say X).

C++
CMyClass* pMirrorObj = pObj;     //Aliased Pointer

pMirrorObj will also store address of same variable in it. i.e. pMirrorObj also pointing to variable stored at address K.
If I am not wrong, pMirrorObj may have some different address (say Y).

As per the above,
X -> K
Y -> K

I am using both pointers (pObj and pMirrorObj) in different code location.

C++
if( pObj )
{
delete(pObj);
pObj = NULL;
}


Result of above operation will,
X -> NULL
Y -> K

How to deal with second pointer (dangling pointer)?
Shall I use as double pointer (CMyClass** ppMirrorObj = &pObj) instead of single pointer (pMirrorObj) to avoid deletion dangling pointer as follows,
C++
if( pMirrorObj )     //assigning NULL to pObj, will not make pMirrorObj as NULL
{
delete(pMirrorObj);
pMirrorObj = NULL;
}


Thanks & Regards,
Aniket A. Salunkhe
Posted

Well, you could create a pointer to the new object

CMyClass *pObj = new CMyClass();


Then instead of copying the value stored in pObj, you can create 2 pointers to pObj.

CMyClass **ptrToOrigPtrA = &pObj;
CMyClass **ptrToOrigPtrB = &pObj;


If you then delete(pObj) and set pObj = NULL, then both *ptrToOrigPtrA & *ptrToOrigPtrB will also be NULL.
So, since you can see that they point to a memory location holding NULL you then know that trying to reference the original object via **ptrToOrigPtrA or **ptrToOrigPtrB will be bad news.


So, yes - you should use 'double-pointers'. :)
 
Share this answer
 
You may have a look at smart pointers (see, for instance Wikipedia[^]).
 
Share this answer
 
You nearly had it.

C++
CMyClass* pObj = new CMyClass();  // Pointer to object
CMyClass** ppMirrorObj = &pObj;   // Pointer to pointer to object

Now you can:
C#
delete (*ppMirrorObj); // Deletes object pObj points to
*ppMirrorObj = NULL;  // pObj now NULL


OR

C#
delete (pObj);  // Deletes object pObj points to

pObj = NULL;  // pObj now NULL


Deleting a NULL pointer has no effect so no need to check for NULL.
 
Share this answer
 
First of all your understanding on dangling pointers is wrong. Here there is no dangling pointer. Below I have given an example for dangling pointer
C++
class A
{
	int* pnData;
	public:
	A()
	{
		pnData = new int;
	}
};


A* paObj = new A;
:
:
delete paObj; // This makes pnData a dangling pointer
paObj = 0;

Here you are deleting the pointer paObj, which makes the pointer pnData a dangling pointer.
Because after deleting paObj, there is no way you can delete pnData which makes pnData a dangling pointer.


in your code example both the pointers are refering the same memory. so deleting one will defently delete the other.
To avoid this you have to create separate objects (both the objects using new)and assign the object members using an overloaded assignment operator
 
Share this answer
 
Comments
[no name] 22-Mar-12 7:18am    
You are absolutely correct. I took the example as the indication of what was required.
The problem is really about how to avoid attempting to delete an invalid pointer.
You should use shared pointers (tr1::shared_ptr for example, if you are not on VS2010 or above).
 
Share this answer
 
Any pointer implementation that gives us reference counting will work in this scenario. boost and tr1 contains some shared pointer implementations.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900