Alex Rivera | Logout

How to guard against memory leaks?

Asked 2010-02-02T16:14:33.933
11

I was recently interviewing for a C++ position, and I was asked how I guard against creating memory leaks. I know I didn't give a satisfactory answer to that question, so I'm throwing it to you guys. What are the best ways to guard against memory leaks?

Thanks!

Edit
Report

2 Answers

8

You'd do well to read up on RAII.

answered 2010-02-02T16:33:54.380
1

A very good way is using Smart Pointers, the boost/tr1::shared_ptr. The memory will be free'd, once the (stack allocated) smart pointer goes out of scope.

answered 2010-02-02T16:17:18.340

Your Answer