← 返回 nvidia 的题目列表C++ Debugging and Output Prediction (Pointers, Inheritance, Multithreading)
类型:online_judge
You are given 4 independent C++ code snippets. For each snippet, do the following:
Identify the bug(s) in the code (including but not limited to undefined behavior, memory-management issues, out-of-bounds access, dangling pointers, lifetime issues, inheritance/polymorphism misuse, and data races).
Assuming the code can run as-is, or after fixing the critical bug(s) you identified, predict what the program prints. If the output is nondeterministic, explicitly state why (e.g., thread scheduling or undefined behavior).
The snippets cover:
Pointers and object lifetime (raw pointers, new/delete, returning references, aliasing, etc.)
Inheritance and polymorphism (virtual functions/virtual destructor, object slicing, base pointer to derived, etc.)
Multithreading (std::thread, shared state, missing synchronization leading to data races)
Input/Output: No stdin. This is a static analysis and reasoning question.
Constraints/Notes:
Clearly separate “definite bugs/UB” from “potential issues”.
For the multithreading snippet, state whether the output depends on scheduling.