Code RoomBuffer destructor called twice
HardPrep Room Coding #1789

Buffer destructor called twice

Code reviewCode quality & reviewSenior–Staff~30 min

Review this C++ RAII buffer wrapper.

What a strong answer looks like

Separate real bugs from style. Rank issues by severity, point at the root cause rather than the symptom, and suggest a concrete fix, specific and kind.

0:00 of about 30 min
Mark a line and say what kind of problem it is.0 findings
1class Buffer {
2public:
3 explicit Buffer(size_t n) : data_(new char[n]), size_(n) {}
4 ~Buffer() { delete[] data_; }
5 char* get() { return data_; }
6private:
7 char* data_;
8 size_t size_;
9};
10 
11void consume(Buffer b); // takes by value
12 
13void run() {
14 Buffer buf(4096);
15 consume(buf); // pass a copy
16 buf.get()[0] = 'x';
17}
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.