| | |
Objects Appears to get Destroyed..Please Help!
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2007
Posts: 12
Reputation:
Solved Threads: 0
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:
Implementation of Class MultiSet:
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.
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)
class Multiset { public: Multiset(); //default constructor for multiset virtual ~Multiset(); //default destructor for multiset Create(int newsize); //Creates an array of a given size. virtual bool AddElement(int ElementValue,int Position); //Adds an element to a given position in the array. int &ReadElement(int index); //Returns Address of element bool IsMember(int Element2Check); //Checks if a given element is a member of the multi/set int GetCardinality(); //Returns the number of elements in a multi/set bool IsSubset(Multiset MSet2Check); //Returns whether the current multi/set is a Subset of a given multi/set protected: //Only derived classes should have access int *data; //Pointer to int int size; //Holds the size of the Array bool ArrayExists; };
Implementation of Class MultiSet:
cpp Syntax (Toggle Plain Text)
///////////////////////////////////////////////////////////////////////////////////// // Construction/Destruction ///////////////////////////////////////////////////////////////////////////////////// Multiset::Multiset() { ArrayExists = false; //Memory has not yet been allocated for the array. } Multiset::~Multiset() { ArrayExists = false; //Memory has been de-allocated for the array. delete[] data; } ///////////////////////////////////////////////////////////////////////////////////// // // Create() // Method to create an array of a given size. (Largest size is determined by int) // Passed IN : Int Size (Size of array to be created) // Passed OUT: Nothing // ///////////////////////////////////////////////////////////////////////////////////// Multiset::Create(int newsize) { // set size to newsize and allocate memory size = newsize; data = new int[size]; // returns 0 if new failed, otherwise returns 1 if (data == NULL){ cout << "Not enough memory available"; exit(-1); } else ArrayExists = true; } //Multiset object passed in to this method,original gets destroyed!! bool Multiset::IsSubset(Multiset MSet2Check){ int i=0,j=0; bool FoundInInner = false ; if (size > MSet2Check.size) return false; //Cannot be a Subset as it is larger than MSet2Check else //Work out if it's a subset of MSet2Check for (i=0; i<size; i++){ for(j=0; j<MSet2Check.size; j++){ if (data[i] == MSet2Check.data[j]){ FoundInInner = true; MSet2Check.data[j] = Reset; break; } else FoundInInner = false; } if (FoundInInner == false) return false; else { j = 0; //j counter is reset to 0 for next i loop if (i == (size - 1)) return true; } } }
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.
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
Call this method as you were calling it previously.
Declare the member function as
C++ Syntax (Toggle Plain Text)
bool IsSubset(Multiset &MSet2Check);
C++ Syntax (Toggle Plain Text)
//Multiset object passed in to this method,original gets destroyed!! bool Multiset::IsSubset(Multiset &MSet2Check){ int i=0,j=0; bool FoundInInner = false ; if (size > MSet2Check.size) return false; //Cannot be a Subset as it is larger than MSet2Check else //Work out if it's a subset of MSet2Check for (i=0; i<size; i++){ for(j=0; j<MSet2Check.size; j++){ if (data[i] == MSet2Check.data[j]){ FoundInInner = true; MSet2Check.data[j] = Reset; break; } else FoundInInner = false; } if (FoundInInner == false) return false; else { j = 0; //j counter is reset to 0 for next i loop if (i == (size - 1)) return true; } } }
Call this method as you were calling it previously.
I know I am. Therefore I am.
•
•
Join Date: Nov 2007
Posts: 12
Reputation:
Solved Threads: 0
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.
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.
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.
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.
•
•
Join Date: Nov 2007
Posts: 12
Reputation:
Solved Threads: 0
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!
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!
![]() |
Similar Threads
Other Threads in the C++ Forum
- Previous Thread: variables naming variables.
- Next Thread: Problem with Inserting Nodes at Binary Trees
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings struct temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets





