Master C++ Inc/Dec, Move, Copy, Reset, Destroy to Avoid Interview Pitfalls
This article systematically explains C++ reference‑count changes, the precise moments when ++/-- operators trigger in shared_ptr, move, copy, reset and destroy operations, and illustrates common mistakes with concrete code examples, helping developers and interviewees avoid subtle bugs and logic errors.
In C++ development, the logic behind shared_ptr reference‑count increments and decrements is a frequent interview focus and a source of subtle bugs. The article first clarifies that the counter changes only when object ownership changes, not on every copy or assignment, and explains why mis‑understanding this can lead to memory leaks, dangling pointers, or crashes.
Simple examples demonstrate the difference between post‑increment ( count++) and pre‑increment ( ++count) in a password‑checking loop, showing that count++ returns the original value before adding one, while ++count increments first. A second example with an array sum ( sum += arr[i++]) illustrates how using arr[++i] would skip the first element, producing incorrect results. A conditional example ( if (a++ > 5)) shows how post‑increment evaluates the condition before the increment, whereas pre‑increment changes the outcome.
The article then details the two forms of the ++/-- operators. Pre‑increment ( ++i) and pre‑decrement ( --i) modify the variable before its value is used; post‑increment ( i++) and post‑decrement ( i--) use the original value first and then modify it. A code snippet prints the differing results of pre‑ and post‑increment on variables a and c.
Next, the piece explores common scenarios where counting changes occur:
Variable initialization and assignment : Demonstrates a = a + b++ versus a = a + ++b, emphasizing the order of evaluation.
Loop counters : Shows the classic for (int i = 0; i < 10; i++) and equivalent while loop, warning that omitting the increment leads to infinite loops.
Conditional statements : Provides an if‑else example where using num++ > 5 yields a different branch than ++num > 5.
The article then shifts to move semantics. It explains that std::move casts an lvalue to an rvalue reference, enabling resource transfer without copying. A vector example shows std::vector<int> b = std::move(a) moving the internal memory pointer, leaving a in a valid but empty state. The performance benefit is highlighted for complex objects, while noting that moving built‑in types like int offers no advantage.
Copy operations are examined next. The article distinguishes shallow copy (pointer copy) from deep copy (full data duplication). Using a SimpleArray class, it shows that a shallow‑copied object shares the same memory, so modifying the original affects the copy, whereas a deep‑copied object has independent memory, preserving data integrity after changes.
Reset operations are illustrated with a FileHandler class. The reset() method closes the file, clears error flags, and reopens it, demonstrating how resetting restores an object to a usable initial state and prevents resource dead‑locks. The same principle applies to database connections or network handles.
Finally, destroy operations are covered via a MemoryManager class. Its destructor releases dynamically allocated memory, and the article notes the importance of virtual destructors in inheritance hierarchies and correct release order to avoid double‑free or dangling pointers.
Throughout, the article stresses careful attention to the exact timing of ++/--, move, copy, reset, and destroy actions to write correct C++ code and succeed in technical interviews.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Deepin Linux
Research areas: Windows & Linux platforms, C/C++ backend development, embedded systems and Linux kernel, etc.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
