Code RoomLog message dangling pointer
MediumPrep Room Coding #2010

Log message dangling pointer

Code reviewAlgorithms & data structuresMid–Senior~20 min

Review this C++ code that caches a C string for a logging library.

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 20 min
Mark a line and say what kind of problem it is.0 findings
1struct LogLine { const char* msg; };
2 
3LogLine make_line(int code) {
4 std::string s = "error " + std::to_string(code);
5 return LogLine{ s.c_str() }; // hand the C string to the sink
6}
7 
8auto line = make_line(500);
9logger.emit(line.msg);
Which questions mattered is sealed until you submit. Telling you now would just be handing over the edge cases.