1

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?

4
  • You are adding CString object when you say List<CString> myList, I don't think so this is best way. store pointers and you are allocating CString and its you responsibility to release it. Also I have doubt your code is compiling successfully? Commented Sep 17, 2012 at 9:27
  • what kind of list is this? MFC? Commented Sep 17, 2012 at 9:32
  • 1
    Note that C++ is not Java, especially new is used differently in C++. Commented Sep 17, 2012 at 9:34
  • Impossible to answer, without seeing the definition of List. Apparently it has a constructor that takes a List*, and when instantiated on a type, it traffics in pointers to that type. Quite an odd class... Commented Sep 17, 2012 at 13:26

2 Answers 2

5

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.

Sign up to request clarification or add additional context in comments.

5 Comments

I'm sorry about my code. My intention about that list is some pointer list which add parameter param and when I delete this list, memory from param is still exist. isn't it?
@barssala the code doesn't even compile, so it can't even run. You can't talk about runtime behavior when you can't even start the program.
@barssala but in theory, no, if you allocate with new[], you have to explicitly delete with delete[].
Ok, if it's not a list. If I use CString* a = param and delete a. What's about that? I mean param still exist? Because I delete only obj. a.
@barssala a is a pointer, not an object. And a and param would point to the same location, so both would be invalid after the delete. Take my advice and read a book. ;)
1

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.

8 Comments

There are some one new object in function. And I can't delete that parameters directly.<br/> I will give you some examples. <br/> void test(){ CString* param("sth"); myString = param;//myString is parameters in header file } and I wanna know that object param in function will exist forever? and I can't delete param because if I delete param, it will cause myString be NULL as well.
I don't know what CString is. And how you can compare it's pointer to a non pointer string.
CString is my implemented string and myString is defined in header file like CString* myString.
Still i don't understand how you can compare a pointer type to a non pointer type. If MyString i also a pointer you can delete him instead of param.
My CString declare in header file such as CString* myString. So, myString is a pointer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.