Safer code in C++ with lifetime bounds
Daniel Lemire's blog emphasizes using lifetime bounds in C++ to improve code safety and performance, highlighting the role of std::string_view in avoiding unnecessary copies and preventing dangling references.
Read original articleDaniel Lemire's blog discusses the use of lifetime bounds in C++ to enhance code safety and performance. The blog highlights the importance of avoiding unnecessary copies in software development, which can be achieved through the use of references or pointers, exemplified by the std::string_view class. This class allows developers to create a view of a string without owning the underlying memory, but it necessitates careful tracking of memory ownership to prevent dangling references.
Lemire points out that while modern tools can detect such bugs, it would be more efficient if compilers could provide immediate feedback. Some C++ compilers, including Visual Studio and LLVM, now support lifetime-bound annotations that help identify potential issues. He illustrates this with an example involving URL parsing, where returning a std::string_view from a temporary object can lead to unsafe code.
By annotating functions with lifetime-bound attributes, developers can receive compile-time warnings about potential dangling references. Although this feature is not yet perfect and may not always trigger warnings, it represents progress in catching errors early in the development process. Lemire credits Denis Yaroshevskiy for bringing this compiler feature to his attention and encourages further exploration of the LLVM documentation for more information.
Related
Weekend projects: getting silly with C
The C programming language's simplicity and expressiveness, despite quirks, influence other languages. Unconventional code structures showcase creativity and flexibility, promoting unique coding practices. Subscription for related content is encouraged.
Some Tricks from the Scrapscript Compiler
The Scrapscript compiler implements optimization tricks like immediate objects, small strings, and variants for better performance. It introduces immediate variants and const heap to enhance efficiency without complexity, seeking suggestions for future improvements.
Malloc() and free() are a bad API (2022)
The post delves into malloc() and free() limitations in C, proposing a new interface with allocate(), deallocate(), and try_expand(). It discusses C++ improvements and emphasizes the significance of a robust API.
New Features in C++26
The ISO released new C++ standards on a three-year cycle, with C++26 proposals open until January 2025. Updates include hazard pointers, user-space RCU support, debugging headers, and template enhancements for improved functionality.
What's so hard about constexpr allocation?
C++20 allows allocations during constant evaluation, but they must be deallocated in the same context, limiting constructs like `constexpr std::vector`. Challenges include constant destruction and access problems, requiring clearer rules.
The static approach used here may have the advantage of giving a more categorical assurance, i.e. one that doesn't depend on the particular behaviour exercised at runtime, but it requires annotations, meaning that:
1. It can't be used on an existing codebase lacking these annotations
2. It trusts the programmer to get the annotations right
(I'm not particularly well informed of recent developments in C++, perhaps such runtime instrumentation already exists?)
object backing the pointer will be destroyed at the end of the full-expression [-Werror,-Wdangling-gsl]
Related
Weekend projects: getting silly with C
The C programming language's simplicity and expressiveness, despite quirks, influence other languages. Unconventional code structures showcase creativity and flexibility, promoting unique coding practices. Subscription for related content is encouraged.
Some Tricks from the Scrapscript Compiler
The Scrapscript compiler implements optimization tricks like immediate objects, small strings, and variants for better performance. It introduces immediate variants and const heap to enhance efficiency without complexity, seeking suggestions for future improvements.
Malloc() and free() are a bad API (2022)
The post delves into malloc() and free() limitations in C, proposing a new interface with allocate(), deallocate(), and try_expand(). It discusses C++ improvements and emphasizes the significance of a robust API.
New Features in C++26
The ISO released new C++ standards on a three-year cycle, with C++26 proposals open until January 2025. Updates include hazard pointers, user-space RCU support, debugging headers, and template enhancements for improved functionality.
What's so hard about constexpr allocation?
C++20 allows allocations during constant evaluation, but they must be deallocated in the same context, limiting constructs like `constexpr std::vector`. Challenges include constant destruction and access problems, requiring clearer rules.