Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Dynamic object creation and dangling pointers.

Status
Not open for further replies.

0x4B47

Programmer
Jun 22, 2003
60
US
Hi fellow coders,

I have a problem that I need assistance with.

Consider this class in intset.h:

Code:
class CIntSet
{
public:
   CIntSet();
   ~CIntSet();
   CIntSet* intersect( CIntSet* is1, CIntSet* is2 );
   CIntSet* unionofSets( CIntSet* is1, CIntSet* is2 );

private:
   int* intSet;
};

Constructor implementation in intset.cpp
Code:
CIntSet::CIntSet()
{
    intSet = new int[5];
    // initialise intset[]
}

Now when I use the member functions, I want to create a third set which is the union of the sets or intersection of the sets which are passed as arguments. But obviously when the function exits it will destroy the third set so I'm left with the so-called 'dangling pointer'. If I declare the third set as static then I end up modifying the same third set everytime I call the functions so the original third set is lost. I don't want to create a third object in main() and assign the result of a intersection or union to it. I want to return a pointer of the newly created third set and assign it to a pointer in main().
ie.
Code:
main()
{
   CIntSet* intSetPtr = intSetPtr->intersect( is1, is2 );
}
Can someone please help? If I can't do what I'm doing above can someone out there tell me why and/or suggest any alternative method (without creating the third object in main() ofcourse).

Thanks in advance guys!
Kunal.
 
Some suggestions:

1) Skip the pointers
Use references instead. I mean, what would intersect(0,0); represent?

2) Use a auto-cleaning member like std::vector rather than a new:ed array

3) Let the class behave any other "regular" type.

With that you'd have something like:
Code:
#include <vector>

class CIntSet
{
public:
   CIntSet();
   ~CIntSet();
   ...
   CIntSet intersect(const CIntSet& other) 
   {
		// Return the intersection of "this" and "other"
   }
   CIntSet union(const CIntSet&  other)
   {
		// Return the union of "this" and "other"
   }
   ...
private:
   std:vector<int> mIntSet;
};
...
main()
{
	CIntSet is1;  
	CIntSet is2;  
	CIntSet is3 =is1.intersect(is2);
}


Just my $0.02

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
By the way...are you aware that quite generic set functionality is provided by the STL?

See MSDN on set_intersection and set_union.

And then there is of course the std::set class too...

/Per
[sub]
&quot;It was a work of art, flawless, sublime. A triumph equaled only by its monumental failure.&quot;[/sub]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top