Objects Appears to get Destroyed..Please Help!

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Nov 2007
Posts: 12
Reputation: djJonno is an unknown quantity at this point 
Solved Threads: 0
djJonno djJonno is offline Offline
Newbie Poster

Objects Appears to get Destroyed..Please Help!

 
0
  #1
Dec 24th, 2007
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:
  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

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

 
0
  #2
Dec 24th, 2007
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
  1. bool IsSubset(Multiset &MSet2Check);

  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.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 12
Reputation: djJonno is an unknown quantity at this point 
Solved Threads: 0
djJonno djJonno is offline Offline
Newbie Poster

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

 
0
  #3
Dec 24th, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

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

 
0
  #4
Dec 24th, 2007
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.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 12
Reputation: djJonno is an unknown quantity at this point 
Solved Threads: 0
djJonno djJonno is offline Offline
Newbie Poster

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

 
0
  #5
Dec 24th, 2007
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!
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 176
Reputation: dubeyprateek is an unknown quantity at this point 
Solved Threads: 22
dubeyprateek's Avatar
dubeyprateek dubeyprateek is offline Offline
Junior Poster

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

 
0
  #6
Dec 24th, 2007
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.
I know I am. Therefore I am.
Reply With Quote Quick reply to this message  
Join Date: Nov 2007
Posts: 12
Reputation: djJonno is an unknown quantity at this point 
Solved Threads: 0
djJonno djJonno is offline Offline
Newbie Poster

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

 
0
  #7
Dec 24th, 2007
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC