Reputation: 483
I will give some examples and explain. First, I declare some object like
CString* param = new CString[100]
And when I declare this one, my memory would increase a little bit because it's some implemented string. Then I store this object in some list of CString just like
List<CString> myList = new List<CString>; // new list of CString
myList.add(param);
This is my question: I wanna know, when I delete myList, my param isn't deleted, right? And memory in param still exists.
Do I misunderstand?
Upvotes: 1
Views: 156
Reputation: 2039
Rule of thumb for c++ : If you type new
you'll need a delete
, except if you're using some kind of smart pointer.
Notice in your case you'll need to use
delete [] param ;
As you are deleting an array.
Upvotes: 1
Reputation: 258608
That code won't compile because myList
holds objects, not pointers, and because myList
is an object, not a pointer, so new
is illegal there:
List<CString> myList;
is an object of type List<CString>
. new List<CString>;
returns a List<CString>*
. param
is a CString*
. myList.add()
expects a CString
, not a CString*
.
Bottom line: these are all basic concepts, grab a good book and read it. C++ is a complicated language, you can't just learn it from example snippets or assume the same concepts and syntax are the same as other languages.
Upvotes: 5