Bloomberg LP Interview Question
Software Engineer / Developerscheck out
http://en.wikipedia.org/wiki/Garbage_collection_(computer_science)#Implementation_strategies
The correct answer is: don't. Garbage collection does not work well with C++.
Better to use RAII (which you can Google). Simply put, make sure that you have destructors to properly clean up all the resources you aquire.
std::mutex m;
void bad()
{
m.lock(); // acquire the mutex
f(); // if f() throws an exception, the mutex is never released
if(!everything_ok()) return; // early return, the mutex is never released
m.unlock(); // if bad() reaches this statement, the mutex is released
}
void good()
{
std::lock_guard<std::mutex> lk(m); // RAII class: mutex acquisition is initialization
f(); // if f() throws an exception, the mutex is released
if(!everything_ok()) return; // early return, the mutex is released
}
the easiest way is to use ref count list
- asuran October 13, 2008