🧵RAII — главный секрет устойчивого к утечкам C++ кода
Привет! Сегодня хочу напомнить о технике, без которой невозможно писать безопасный и устойчивый C++ код — это RAII (Resource Acquisition Is Initialization).
RAII — это идиома, в которой захват ресурса (файл, сокет, память, мьютекс) происходит в конструкторе объекта, а освобождение — в деструкторе. Благодаря этому ресурсы освобождаются автоматически, даже при исключениях.
Пример:
#include <fstream>
void saveData(const std::string& filename) { std::ofstream file(filename); // открытие файла if (!file.is_open()) throw std::runtime_error("Cannot open file");
file << "some data"; // файл закроется автоматически }
RAII делает твой код: ✅ Безопасным к утечкам ✅ Устойчивым к исключениям ✅ Лёгким для чтения и сопровождения
💡Совет: всегда оборачивай "ручные" ресурсы в обёртки — std::unique_ptr, std::lock_guard, std::ofstream, std::thread и т.д.
🧵RAII — главный секрет устойчивого к утечкам C++ кода
Привет! Сегодня хочу напомнить о технике, без которой невозможно писать безопасный и устойчивый C++ код — это RAII (Resource Acquisition Is Initialization).
RAII — это идиома, в которой захват ресурса (файл, сокет, память, мьютекс) происходит в конструкторе объекта, а освобождение — в деструкторе. Благодаря этому ресурсы освобождаются автоматически, даже при исключениях.
Пример:
#include <fstream>
void saveData(const std::string& filename) { std::ofstream file(filename); // открытие файла if (!file.is_open()) throw std::runtime_error("Cannot open file");
file << "some data"; // файл закроется автоматически }
RAII делает твой код: ✅ Безопасным к утечкам ✅ Устойчивым к исключениям ✅ Лёгким для чтения и сопровождения
💡Совет: всегда оборачивай "ручные" ресурсы в обёртки — std::unique_ptr, std::lock_guard, std::ofstream, std::thread и т.д.
The global forecast for the Asian markets is murky following recent volatility, with crude oil prices providing support in what has been an otherwise tough month. The European markets were down and the U.S. bourses were mixed and flat and the Asian markets figure to split the difference.The TSE finished modestly lower on Friday following losses from the financial shares and property stocks.For the day, the index sank 15.09 points or 0.49 percent to finish at 3,061.35 after trading between 3,057.84 and 3,089.78. Volume was 1.39 billion shares worth 1.30 billion Singapore dollars. There were 285 decliners and 184 gainers.
How Does Bitcoin Mining Work?
Bitcoin mining is the process of adding new transactions to the Bitcoin blockchain. It’s a tough job. People who choose to mine Bitcoin use a process called proof of work, deploying computers in a race to solve mathematical puzzles that verify transactions.To entice miners to keep racing to solve the puzzles and support the overall system, the Bitcoin code rewards miners with new Bitcoins. “This is how new coins are created” and new transactions are added to the blockchain, says Okoro.