Telegram Group & Telegram Channel
🧵 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 и т.д.


➡️ @cpp_geek



tg-me.com/cpp_geek/295
Create:
Last Update:

🧵 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 и т.д.


➡️ @cpp_geek

BY C++ geek




Share with your friend now:
tg-me.com/cpp_geek/295

View MORE
Open in Telegram


telegram Telegram | DID YOU KNOW?

Date: |

Telegram Be The Next Best SPAC

I have no inside knowledge of a potential stock listing of the popular anti-Whatsapp messaging app, Telegram. But I know this much, judging by most people I talk to, especially crypto investors, if Telegram ever went public, people would gobble it up. I know I would. I’m waiting for it. So is Sergei Sergienko, who claims he owns $800,000 of Telegram’s pre-initial coin offering (ICO) tokens. “If Telegram does a SPAC IPO, there would be demand for this issue. It would probably outstrip the interest we saw during the ICO. Why? Because as of right now Telegram looks like a liberal application that can accept anyone - right after WhatsApp and others have turn on the censorship,” he says.

Export WhatsApp stickers to Telegram on iPhone

You can’t. What you can do, though, is use WhatsApp’s and Telegram’s web platforms to transfer stickers. It’s easy, but might take a while.Open WhatsApp in your browser, find a sticker you like in a chat, and right-click on it to save it as an image. The file won’t be a picture, though—it’s a webpage and will have a .webp extension. Don’t be scared, this is the way. Repeat this step to save as many stickers as you want.Then, open Telegram in your browser and go into your Saved messages chat. Just as you’d share a file with a friend, click the Share file button on the bottom left of the chat window (it looks like a dog-eared paper), and select the .webp files you downloaded. Click Open and you’ll see your stickers in your Saved messages chat. This is now your sticker depository. To use them, forward them as you would a message from one chat to the other: by clicking or long-pressing on the sticker, and then choosing Forward.

telegram from in


Telegram C++ geek
FROM USA