All Topics
All Topics
Technology
Technology
Design
Design
Programming
Programming
Science
Science
News
News
Gaming
Gaming
Entertainment
Entertainment
Business
Business
Finance
Finance
Sports
Sports
Health
Health
Food
Food
Travel
Travel
Art
Art
Music
Music
Books
Books
Education
Education
Politics
Politics
Personal
Personal
No algorithm. No AI slop. No ads. Just RSS. Pro-human. Indie writers. Real journalism. Open web. Chronological. Hand toasted.

Bjarne Stroustrup on Safe C++: RAII vs Manual Resource Management

By

signa11

6mo ago· 8 min readenInsight

Summary

The article discusses Bjarne Stroustrup's presentation on Safe C++ programming, focusing on resource management pitfalls in C code and the RAII (Resource Acquisition Is Initialization) pattern in C++ as a solution. It analyzes a specific example where a file handle can leak if cleanup is missed, contrasts this with C++'s RAII approach using destructors for automatic cleanup, and explores broader implications for safe programming practices and language design.

Key quotes

· 5 pulled
void f(const char* p) { FILE *f = fopen(p, "r"); // use f fclose(f); }
class File_handle { FILE *p; public: File_handle(const char *pp, const char *r) { p = fopen(pp, r); } ~File_handle() { fclose(p); } };
Any code that exits the function inside // use f that doesn't also call fclose results in a leak.
He shows this code to demonstrate how easy it is to miss resource cleanup.
He provides a C++ equivalent with RAII to avoid this footgun.
Snippet from the RSS feed
About 23 minutes into his talk about Safe C++ [1], Bjarne shows a slide with this code:

You might also wanna read