Skip to content

Latest commit

 

History

History
81 lines (57 loc) · 2.61 KB

File metadata and controls

81 lines (57 loc) · 2.61 KB

Smart_ptr — Custom Smart Pointers (DLL)

Part of Project_zero.

A Dynamic-Link Library (DLL) implementing custom smart pointers that mirror the behaviour of their standard library counterparts. All classes are exported with __declspec(dllexport) / SMARTPTR_API so they can be consumed by any project that links the import library.


Features

  • Auto_ptr<T> — ownership-transfer pointer (similar to the deprecated std::auto_ptr); includes a void specialization
  • Unique_ptr<T> / Unique_ptr<T[]> — exclusive ownership; supports custom deleters via a template parameter
  • Shared_ptr<T> / Shared_ptr<T[]> — reference-counted shared ownership with a control block
  • Weak_ptr<T> / Weak_ptr<T[]> — non-owning observer of a Shared_ptr; avoids circular reference leaks

Factory functions

  • make_unique<T>(...) — constructs a Unique_ptr in one allocation
  • make_shared<T>(...) — constructs a Shared_ptr with a single allocation (object + control block together)

Custom deleters built-in

  • Default_deleter<T> — calls delete
  • Array_deleter<T> — calls delete[]
  • Noop_deleter<T> — does nothing (for stack/static objects)
  • Custom_deleter<T> — wraps any callable

Setup & Linking

1. Include the header

#include "Smart_ptr.h"

2. Link the import library

In Visual Studio:

  • Linker → Input → Additional Dependencies → add Smart_ptr.lib
  • Linker → General → Additional Library Directories → add the path to x64/Release/

3. Place the DLL next to your executable

Important: Smart_ptr.dll must be in the same directory as your .exe at runtime, otherwise the application will fail to start with a "DLL not found" error.

Copy x64/Release/Smart_ptr.dll alongside your built executable, or add x64/Release/ to your system PATH.


Quick Start

#include "Smart_ptr.h"
#include <iostream>

struct MyObject {
    int value;
    MyObject(int v) : value(v) { std::cout << "Created " << v << "\n"; }
    ~MyObject()               { std::cout << "Destroyed " << value << "\n"; }
};

int main() {
    // Unique ownership
    auto u = make_unique<MyObject>(10);
    std::cout << u->value << "\n";

    // Shared ownership
    auto s1 = make_shared<MyObject>(20);
    auto s2 = s1;                          // reference count = 2
    std::cout << s1.use_count() << "\n";   // prints 2

    // Weak observer
    Weak_ptr<MyObject> w = s1;
    if (auto locked = w.lock())
        std::cout << "Still alive: " << locked->value << "\n";

    // Array variant
    auto arr = make_unique<int[]>(5);
    arr[0] = 42;
}