#include <locale>
#include <iostream>
#include <windows.h>
#include <string>
#include <memory>
class SystemResource {
public:
std::wstring name;
SystemResource(std::wstring n) : name(n) {
std::wcout << L"[+] Ресурс '" << name << L"' создан (выделена память)." << std::endl;
}
~SystemResource() {
std::wcout << L"[-] Ресурс '" << name << L"' уничтожен (память освобождена)." << std::endl;
}
};
void CloseMockHandle(HANDLE h) {
std::wcout << L"[*] Системный вызов: закрытие дескриптора " << h << std::endl;
}
int main()
{
std::wcout.imbue(std::locale("rus_rus.866"));
std::wcin.imbue(std::locale("rus_rus.866"));
std::wcout << L"Управление памятью и умные указатели" << std::endl;
//// ========================================================
//// РУЧНОЕ УПРАВЛЕНИЕ
//// ========================================================
const size_t size_arr = 3;
int *arr = new int[size_arr];
arr[0] = 10;
arr[1] = 20;
arr[2] = 30;
for (int i = 0; i < size_arr; i++) {
std::wcout << "элемент номер "<< i << " - " << arr[i] << std::endl;
}
delete[] arr;
arr = nullptr;
std::wcout << L"\n Конец блока 1 \n" << std::endl;
// ========================================================
// ЭКСКЛЮЗИВНОЕ ВЛАДЕНИЕ (std::unique_ptr)
// ========================================================
std::unique_ptr<SystemResource> p1;
std::make_unique<SystemResource>(L"UniqueResource");
std::wcout << p1->name << std::endl;
std::unique_ptr<SystemResource> p2;
//p2 = p1;
p2 = std::move(p1);
//if (p1 == nullptr) {
// std::wcout << L"p1 пустой" << std::endl;
//}
std::wcout << p2->name << std::endl;
std::wcout << L"\n Конец блока 2 \n" << std::endl;
//// ========================================================
//// СОВМЕСТНОЕ ВЛАДЕНИЕ (std::shared_ptr)
//// ========================================================
return 0;
}