Katkat

Design a C++ Shared-Ownership Pointer Library

ByteDanceSoftware Engineer · Technical Screen

Last updated

Explain shared-pointer control blocks, copy and move semantics, lifetime invariants, exception safety, and the limits of reference counting.

Design a small C++ shared-ownership pointer library. Explain the class interface, ownership representation, copy and move behavior, and the conditions under which the managed object is destroyed.

Requirements and Constraints

The requested task is to implement shared-pointer behavior, but the exact library surface is not specified. For a bounded exercise, assume single-threaded ownership of one object allocated for the matching type, with construction, copying, moving, assignment, reset, dereference, and an ownership-count observer. State this scope explicitly. Weak references, array ownership, custom deleters, aliasing constructors, and thread-safe reference counts are extensions to clarify rather than silently assumed requirements.

Clarifying Questions

  • Which constructors and observers are required, and how is an empty pointer represented?
  • Must copies share one control block, and what guarantees should assignment provide if allocation fails?
  • Is concurrent access in scope, and does that refer to the control block, the same pointer object, or the pointed-to object?
  • Are cycles or weak references part of the required behavior?

Hints

Hint 1

The object pointer alone does not identify shared ownership: Two wrappers can point to the same object while maintaining unrelated counts. A correct copy needs to share the same ownership state, not just copy the raw address.

Follow-up questions

  • Why can two independently constructed owners of the same raw pointer cause a double deletion?
  • How does a move differ from a copy in its effect on the reference count?
  • Why would atomic reference counts alone not make concurrent mutation of the pointed-to object safe?