Implement your own shared pointer and unique pointer
Difficulty: MEDIUMCategory: LLDFrequency: 1Asked On 2021-08-18
Problem Statement
Design and implement custom smart pointers similar to std::shared_ptr and std::unique_ptr in C++. Focus on automatic memory management, ownership semantics, reference counting, and thread-safety (optional).
Functional Requirements (FR)
Unique Pointer (UniquePointer)
•Owns a single object.
•Cannot be copied; can only be moved.
•Deletes the object automatically when the pointer goes out of scope.
•Provides standard pointer operations (*, ->).
Shared Pointer (SharedPointer)
•Allows multiple pointers to share ownership of a single object.
•Keeps a reference count to track number of owners.
•Deletes the object when the reference count reaches zero.
•Supports standard pointer operations (*, ->).
•Can provide a use_count() method to query number of active owners.
Optional Features
•Thread-safe reference counting for SharedPointer.
•Weak pointer implementation to avoid cyclic references.
Non-Functional Requirements (NFR)
•Memory Safety → No memory leaks; objects deleted when no longer in use.
•Performance → Minimal overhead for reference counting.
•Thread Safety (Optional) → Safe use of SharedPointer across multiple threads.
•Extensibility → Easy to extend with weak pointers or custom deleters.
Core Entities & Attributes
UniquePointer<T>
•ptr: T* → Raw pointer to the managed object.
Methods:
•get() → Returns raw pointer.
•release() → Releases ownership without deleting.
•reset(T* newPtr) → Deletes current object and takes ownership of new object.
•operator*() → Dereference operator.
•operator->() → Access object members.
SharedPointer<T>
•ptr: T* → Raw pointer to the managed object.
•refCount: int → Tracks number of active owners.
Methods:
•get() → Returns raw pointer.
•use_count() → Returns reference count.
•reset(T* newPtr) → Releases old object (if no other owners) and takes ownership of new object.
•operator*() → Dereference operator.
•operator->() → Access object members.
Core Components
Reference Counting Module
•Tracks number of owners for each SharedPointer.
•Optionally thread-safe using mutex/atomic counters.
UniquePointer Class
•Provides exclusive ownership.
•Disallows copy but allows move semantics.
SharedPointer Class
•Provides shared ownership.
•Deletes object automatically when reference count reaches zero.
APIs / Operations
UniquePointer APIs
•UniquePointer(T* ptr) → Constructor.
•~UniquePointer() → Destructor deletes object.
•operator*() → Dereference.
•operator->() → Access object members.
•release() → Releases ownership.
•reset(T* newPtr) → Replace managed object.
SharedPointer APIs
•SharedPointer(T* ptr) → Constructor.
•SharedPointer(const SharedPointer<T>& other) → Copy constructor (increments ref count).
•~SharedPointer() → Destructor decrements ref count and deletes if zero.
•operator*() → Dereference.
•operator->() → Access object members.
•use_count() → Returns reference count.
•reset(T* newPtr) → Replace managed object.
LLD Design Goals
•Class Diagram → UniquePointer<T>, SharedPointer<T>, ReferenceCounter (internal for SharedPointer).
•Design Patterns:
•RAII (Resource Acquisition Is Initialization) → Automatic cleanup when object goes out of scope.
•Singleton (Optional) → For managing global reference count registry if needed.
•Concurrency Handling (Optional) → Use atomic operations for refCount in SharedPointer.
•Trade-offs →1. Thread-safe reference counting adds minor performance overhead.2. UniquePointer is faster and simpler but cannot share ownership.