Sounds like he's confused.
Before C++11, there were just pointers, and new and delete. If you created an object with new, you must also call delete on it, through the pointer.
After C++11, there were
smart pointers.
unique_ptr and
shared_ptr. If you use these correctly, you don't need to call delete; when the
smart pointer goes out of scope (or you reset it or reassign it or some other such), it will handle that deleting for you.
If you're using a plain pointer, you must still call delete yourself, for every new.
If you're using a smart pointer, and you're using it correctly, you use new to create the object and give it to the smart pointer, and the smart pointer takes care of the delete for you.
You can also use make_unique and make_shared instead of new, but that's an extra topic in this discussion.
The key point here is that you are right, and your instructor is confused and mistaken, mixing up different standards of C++. If you wish to correct your instructor, be careful how you do it. Find a way for your instructor to keep face. It's really not worth the bother otherwise.
I'm not sure how I could even test it, since I won't have access to memory to see if it's still allocated... |
Some people call it computer science. I see very little science in it, but what separates science from philosophy is the ability to gather experimental evidence. Like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
|
#include <iostream>
class testClass
{
public:
testClass() { std::cout << "I'm being constructed!" << '\n';}
~testClass() {std::cout << "I'm being destroyed!" << '\n';}
};
int main()
{
std::cout << "Automatic variable in a small scope:" << '\n';
{
testClass one;
}
std::cout << "Manual new and delete:" << '\n';
testClass* pointer_one = new testClass;
delete pointer_one;
std::cout << "Manual new but no delete, just assign NULL:" << '\n';
testClass* pointer_two = new testClass;
pointer_two = NULL;
}
|
Output:
Automatic variable in a small scope:
I'm being constructed!
I'm being destroyed!
Manual new and delete:
I'm being constructed!
I'm being destroyed!
Manual new but no delete, just assign NULL:
I'm being constructed! |
The last one is never destructed. Its destructor does not run. Nothing ever called delete on it. It's a memory leak.