logoalt Hacker News

vlovich123yesterday at 9:56 PM4 repliesview on HN

And yet, I interviewed 10 people easily where I was using new and delete in the example code and only one person asked "hey - can we use unique_ptr?".


Replies

oxag3nyesterday at 10:11 PM

Ownership problems with pointer/references don't end with allocation.

A codebase can use only std::make_unique() to allocate heap, and still pass around raw pointers to that memory (std::unique_ptr::get()).

The real problem is data model relying on manual lifetime synchronization, e.g. pass raw pointer to my unique_ptr to another thread, because this thread joins that thread before existing and killing the unique_ptr.

show 1 reply
johannes1234321yesterday at 10:37 PM

Well in interviews this is tricky. Sometimes the interviewer wants to see I can new/delete properly, sometimes this tells me "well, if that's the style they are using I better go elsewhere"

If it's done as part of a "here is legacy code, suggest ways to improve it" question one should point it out, though.

show 1 reply
mackeyeyesterday at 10:31 PM

many schools (like mine) don't teach unique pointers in the pure "programming" class sequence, but offer a primer in advanced classes where c++ happens to be used, with the intent to teach manual memory management for a clearer transition to e.g. upper-levels which use c.