ChadS1
January 28th, 2005, 03:46 AM
I am using the stl list class for a class and not a simple type. As a result, I'm having a hard time deciding what copy constructors and deconstructors I need and whether calling list.clear will release all the associated memory. Here are the specifics:
class ReadData {
int nOffset;
string sPath;
};
list<ReadData>ReadList;
Then Say I have the following function, which is called repeatedly:
void AddRead(int nOffset, char * sPath) {
ReadData rd;
rd.nOffset = nOffset;
rd.sPath = sPath;
ReadList.push_back(rd);
}
I have a number of questions regarding this scenario. Will ReadList have trouble when rd goes out of scope? I guess this depends on how rd is added to the list and what the default copy constructor will do. Will the default copy constructor basically do nOffset = rd.nOffset; sPath = rd.sPath, the later statement calling the string's operator= making a deep copy. Or will a shallow copy be made, but when rd goes out of scope, because I have provided no destructor, the shallow copy of rd added to ReadList will still be valid. I suspect the later to be true.
Next, say after adding a bunch of ReadData nodes to the list I call ReadList.clear(). What will happen? Will the allocated memory be effectively freed? Would ReadList.clear() even free the memory associated with the rd objects or would it just free the memory associated with making the linked list, but leave the memory associated w/ the ReadData objects? Say ReadList.clear() does call the nodes' ReadData destructor, would the string object be properly freed without ReadData having a destructor?
Basically I'd like to know what I need to do to this code to make it not have a memory leak while trying to keep it as efficient as possible by using shallow instead of deep copies where permissible. I suspect I'll need a copy constructor and destructor for the ReadData class, but will I also need an operator= function as well?
Much Thanks,
- Chad
class ReadData {
int nOffset;
string sPath;
};
list<ReadData>ReadList;
Then Say I have the following function, which is called repeatedly:
void AddRead(int nOffset, char * sPath) {
ReadData rd;
rd.nOffset = nOffset;
rd.sPath = sPath;
ReadList.push_back(rd);
}
I have a number of questions regarding this scenario. Will ReadList have trouble when rd goes out of scope? I guess this depends on how rd is added to the list and what the default copy constructor will do. Will the default copy constructor basically do nOffset = rd.nOffset; sPath = rd.sPath, the later statement calling the string's operator= making a deep copy. Or will a shallow copy be made, but when rd goes out of scope, because I have provided no destructor, the shallow copy of rd added to ReadList will still be valid. I suspect the later to be true.
Next, say after adding a bunch of ReadData nodes to the list I call ReadList.clear(). What will happen? Will the allocated memory be effectively freed? Would ReadList.clear() even free the memory associated with the rd objects or would it just free the memory associated with making the linked list, but leave the memory associated w/ the ReadData objects? Say ReadList.clear() does call the nodes' ReadData destructor, would the string object be properly freed without ReadData having a destructor?
Basically I'd like to know what I need to do to this code to make it not have a memory leak while trying to keep it as efficient as possible by using shallow instead of deep copies where permissible. I suspect I'll need a copy constructor and destructor for the ReadData class, but will I also need an operator= function as well?
Much Thanks,
- Chad