Tyler Jantzen
Tyler Jantzen

Reputation: 57

(c++) what happens to objects on the heap that don't have any pointers to them?

do they become memory leaks or does c++ realize they have no pointers aiming at them and free up that memory? If they must be deleted i just use the delete command right?

Upvotes: 0

Views: 94

Answers (2)

John Phu Nguyen
John Phu Nguyen

Reputation: 319

Cody is correct, C++ does not have a garbage collector so you would have a memory leak when the pointer to the object is lost.

C++ does have something in the std library to address this. The std::shared_ptr will automatically delete the object if the object no longer has any std::shared_ptr pointed to it.

Upvotes: 0

Cody Gray
Cody Gray

Reputation: 244782

does c++ realize they have no pointers aiming at them and free up that memory?

C++ does not "realize" any such thing. There is no built-in garbage collector.

If you allocate memory with new and don't free it with a corresponding call to delete, you have a memory leak.

Upvotes: 2

Related Questions