943,779 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 771
  • C++ RSS
Dec 24th, 2007
0

Objects Appears to get Destroyed..Please Help!

Expand Post »
Hi,
I've now spent hours looking at this and no joy, wondering if any of you guys can help.
I am passing an object that uses dynamically allocated memory into a method, but for some reason when it finishes my original object has changed/been destroyed.

header file:
cpp Syntax (Toggle Plain Text)
  1. class Multiset
  2. {
  3. public:
  4.  
  5. Multiset(); //default constructor for multiset
  6. virtual ~Multiset(); //default destructor for multiset
  7.  
  8. Create(int newsize); //Creates an array of a given size.
  9.  
  10. virtual bool AddElement(int ElementValue,int Position); //Adds an element to a given position in the array.
  11. int &ReadElement(int index); //Returns Address of element
  12.  
  13. bool IsMember(int Element2Check); //Checks if a given element is a member of the multi/set
  14. int GetCardinality(); //Returns the number of elements in a multi/set
  15. bool IsSubset(Multiset MSet2Check); //Returns whether the current multi/set is a Subset of a given multi/set
  16.  
  17.  
  18. protected: //Only derived classes should have access
  19. int *data; //Pointer to int
  20. int size; //Holds the size of the Array
  21. bool ArrayExists;
  22.  
  23. };


Implementation of Class MultiSet:

cpp Syntax (Toggle Plain Text)
  1. /////////////////////////////////////////////////////////////////////////////////////
  2. // Construction/Destruction
  3. /////////////////////////////////////////////////////////////////////////////////////
  4.  
  5.  
  6. Multiset::Multiset()
  7. {
  8. ArrayExists = false; //Memory has not yet been allocated for the array.
  9. }
  10.  
  11.  
  12. Multiset::~Multiset()
  13. {
  14. ArrayExists = false; //Memory has been de-allocated for the array.
  15. delete[] data;
  16. }
  17.  
  18.  
  19. /////////////////////////////////////////////////////////////////////////////////////
  20. //
  21. // Create()
  22. // Method to create an array of a given size. (Largest size is determined by int)
  23. // Passed IN : Int Size (Size of array to be created)
  24. // Passed OUT: Nothing
  25. //
  26. /////////////////////////////////////////////////////////////////////////////////////
  27.  
  28. Multiset::Create(int newsize)
  29. {
  30. // set size to newsize and allocate memory
  31. size = newsize;
  32. data = new int[size];
  33.  
  34. // returns 0 if new failed, otherwise returns 1
  35. if (data == NULL){
  36. cout << "Not enough memory available";
  37. exit(-1);
  38. }
  39. else
  40. ArrayExists = true;
  41. }
  42.  
  43. //Multiset object passed in to this method,original gets destroyed!!
  44. bool Multiset::IsSubset(Multiset MSet2Check){
  45.  
  46. int i=0,j=0;
  47. bool FoundInInner = false ;
  48.  
  49.  
  50. if (size > MSet2Check.size)
  51. return false; //Cannot be a Subset as it is larger than MSet2Check
  52. else //Work out if it's a subset of MSet2Check
  53. for (i=0; i<size; i++){
  54. for(j=0; j<MSet2Check.size; j++){
  55. if (data[i] == MSet2Check.data[j]){
  56. FoundInInner = true;
  57. MSet2Check.data[j] = Reset;
  58. break;
  59. }
  60. else
  61. FoundInInner = false;
  62. }
  63. if (FoundInInner == false)
  64. return false;
  65. else
  66. {
  67. j = 0; //j counter is reset to 0 for next i loop
  68. if (i == (size - 1))
  69. return true;
  70. }
  71. }
  72.  
  73. }


Sorry for the big block of code guys, basically when I use mySet1.IsSubset(mySet2) the original mySet2 values all change on leaving the method IsSubset. It is calling the destructor on leaving and then I think it is killing my original object,
How do I get it to leave my passed in object alone!!??
Thanks for your time guys.
Last edited by djJonno; Dec 24th, 2007 at 10:45 am.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djJonno is offline Offline
12 posts
since Nov 2007
Dec 24th, 2007
0

Re: Objects Appears to get Destroyed..Please Help!

It is not calling distructor, you are passing object by value therefore you do not see the change in values of the passed object.
Declare the member function as
C++ Syntax (Toggle Plain Text)
  1. bool IsSubset(Multiset &MSet2Check);

C++ Syntax (Toggle Plain Text)
  1. //Multiset object passed in to this method,original gets destroyed!!
  2. bool Multiset::IsSubset(Multiset &MSet2Check){
  3.  
  4. int i=0,j=0;
  5. bool FoundInInner = false ;
  6.  
  7.  
  8. if (size > MSet2Check.size)
  9. return false; //Cannot be a Subset as it is larger than MSet2Check
  10. else //Work out if it's a subset of MSet2Check
  11. for (i=0; i<size; i++){
  12. for(j=0; j<MSet2Check.size; j++){
  13. if (data[i] == MSet2Check.data[j]){
  14. FoundInInner = true;
  15. MSet2Check.data[j] = Reset;
  16. break;
  17. }
  18. else
  19. FoundInInner = false;
  20. }
  21. if (FoundInInner == false)
  22. return false;
  23. else
  24. {
  25. j = 0; //j counter is reset to 0 for next i loop
  26. if (i == (size - 1))
  27. return true;
  28. }
  29. }
  30.  
  31. }

Call this method as you were calling it previously.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 24th, 2007
0

Re: Objects Appears to get Destroyed..Please Help!

Hi,
Thanks for the suggestion, sorry maybe I didn't explain myself very well, the problem is that the values are changing and I don't want them to. That is why I thought that by passing the object by value, it would leave the original object alone...but it doesn't. When I step out of IsSubset my original object is full of random values. Any ideas?
Thanks.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djJonno is offline Offline
12 posts
since Nov 2007
Dec 24th, 2007
0

Re: Objects Appears to get Destroyed..Please Help!

Because of in class
int *data;

and "MSet2Check.data[j] = Reset;" in the method. It changes the value memory location pointed by data member of MSet2Check.

There are couple of solutions
1)Prepare a deep copy of MSet2Check in the function and use it inside the function. You may like to overload '=' operator and
Multiset localobj = MSet2Check and operate on localobj ie "localobj .data[j] = Reset;"
2) If you dont want MSet2Check to change why do you do "MSet2Check.data[j] = Reset;" ? Better change the decleartion of the method as
bool Multiset::IsSubset(const Multiset MSet2Check) ; and nevre do "MSet2Check.data[j] = Reset;" anyways; I don't see any reason of doing this.

Above solutions will help not changing the object being passed. Caller object is certenly going to change. I hope as the name suggests "IsSubset" should not change either caller or callie.

For that you need to modify your logic in the member function and declare it as
bool Multiset::IsSubset(const Multiset MSet2Check) const; and overload ‘=’ operator to prepare deep copy and use solution 1.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 24th, 2007
0

Re: Objects Appears to get Destroyed..Please Help!

Thanks again,
Yes it is true that I do not want to change my original object. From what you have been saying about creating a "deep copy", it has reminded me that I think i need to make a copy constructor to allocate memory for the local object. At the moment I think it is using the same address for the local object as it is for the original object, then when it leaves the function it is freeing up both local and original as they have the same address.
I think I am on the right track now, thanks for your suggestion, gave me a few ideas to try anyway. Let me know what you think!
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djJonno is offline Offline
12 posts
since Nov 2007
Dec 24th, 2007
0

Re: Objects Appears to get Destroyed..Please Help!

What do you mean by original object the one used to call the function or the one which is passed or both ?
You need a copy constructor indeed.
Always declare a copy constructor and an assignment operator for classes with dynamically allocated memory.
Reputation Points: 39
Solved Threads: 24
Junior Poster
dubeyprateek is offline Offline
176 posts
since Mar 2006
Dec 24th, 2007
0

Re: Objects Appears to get Destroyed..Please Help!

Ya, by original object I mean the passed in object. I started coding a copy constructor now, think this should do it, I understand now what you mean by overloading the = operator. That way I can 'really' copy my objects.
Thanks for your suggestions, it put me on the right track.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
djJonno is offline Offline
12 posts
since Nov 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: variables naming variables.
Next Thread in C++ Forum Timeline: Problem with Inserting Nodes at Binary Trees





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC