#include <iostream>
#include <atomic>
#include <thread>
#include <memory>
int main() {
std::atomic<int> a(0); // Initialize atomic variable 'a' to 0
// Initialize the unique_ptr to manage a new int
std::unique_ptr<int> ptr = std::make_unique<int>();
// Dereference ptr to access the integer it manages, then assign 5
*ptr = 5;
// Now let's print the value managed by unique_ptr
std::cout << "Value of ptr: " << *ptr << std::endl;
// If you wanted to assign this value to the atomic variable 'a'
a.store(*ptr); // Store the value of *ptr into the atomic variable 'a'
// Print the value of the atomic variable 'a'
std::cout << "Value of atomic 'a': " << a.load() << std::endl;
return 0;
}
/*
A std::unique_ptr is a smart pointer in C++ that provides exclusive ownership of a dynamically allocated object. It ensures that there
is only one unique_ptr pointing to a particular object at any given time, which helps manage memory safely and efficiently by
automatically deallocating the memory when the unique_ptr goes out of scope.
Key Features of std::unique_ptr:
Exclusive Ownership:
A unique_ptr owns the object it points to and ensures that no other unique_ptr owns the same object. This prevents accidental double
deletion and other issues related to manual memory management.
Automatic Deallocation:
When a unique_ptr goes out of scope, it automatically deletes the object it owns, ensuring that memory is properly freed without the
need for explicit delete calls.
Non-Copyable:
unique_ptr cannot be copied to another unique_ptr. This ensures that the ownership of the object is unique. However, it can be moved
using std::move.
Move Semantics:
You can transfer ownership from one unique_ptr to another using move semantics. This is done via std::move. After the move, the original
unique_ptr no longer owns the object.
Custom Deleters:
unique_ptr can be configured to use custom deleters, which are functions or function objects that specify how the object should be
deleted.
To embed this project on your website, copy the following code and paste it into your website's HTML: