Ok, I got it. So I have to delete the objects at the end and aso declare virtual destructor for the base so when deleting the inherited class to delete the base also.
OK, here let me add a couple of sents.
When I create a vector, I would leave the deletion/destruction of vector to vector itself, meaning, I would use one of its clear() or erase() functions, rather than delete.
I would not recommend to use 'delete' in this case.
And another thing, it is "not" a memory leak in this as the vector will be destroyed any way once the main() functionality is done. Since it is end of the main(), the vector will be gone anyway without a memory leak.
It is called "memory leak" only when the program is still running and the (reserved) memory has run out. Above that is not the case.
I would not recommend to use 'delete' in this case.
The 'delete' doesn't erase the vector, it just free the heap memory!
vector is the stack memory,the OS will free that!
It is called "memory leak" only when the program is still running and the (reserved) memory has run out. Above that is not the case.
Yes,It is called "memory leak" only when the program is still running and the (reserved) memory has run out.
But the heap memory had not freed is mean that "memory leak"!
When the programm is run, the os can't collect the memory while you ‘new’!
To simo110:
You mean that i have two times the same loop?
This code is just for me to understand how it works, as a general example. Usually if i want to do something like that i just use one for loop
Yes, the 'delete' is needed.
Sorry I just overlooked it. Since it is a vector of "new"ly created pointers in the program, the "delete" is right to deallocate the memory.
However, after viewing the vector, the program finishes any way so the no heap memory loss as the program goes out of running scope and the memory reserved (whatever) for program is given to others by OS, anyway.
Unless it is in another called function and the program is still running, then only it is a memory leak.
In the above given code example, it would not leak any memory.
,the program still have many code, the is many memory is loss.Until the program is closed,the memory will be collected!
Sometimes a program will run for 365*24 hours!So the program will closed for the memory will be exhausted!
In a word,the style of writing the code is not good!
I didn't know that it is advantage to use iterator instead of unsigned. If i just use int then the compiler has a warning that it converts int to unsigned. If i use unsigned there is no warning or anything.
When I used it in my program I had the delete at the end because I need the data all the way in my program. If I wanted to delete something from the vector first i delete the object and then erase it from the vector.
To Mitsakos :
You can read the book << The C++ Standard Library >> wrote by Nicolai M,Josuttis.
Yes,the size_type is a type which is similary to unsigned int!The int will be a convertes!
While the vector's element is pointer, it will be a trouble thing!You have to look after your [pointer momently!
I started reading <<The C++ Programming Language 3rd.Ed>> by Bjarne Stroustrup now. It has many things that can help me with the STL, the possibilities of C++ in general and also with my programming style.
o(∩_∩)o...
<<The C++ Programming Language 3rd.Ed>> is just a cookbook.It can't shape your programming style.
You can read the <<C++ Primer 4th>> or <<Effective C++>>!