Understanding Coverity's WRAPPER_ESCAPE Warning

Understanding Coverity’s WRAPPER_ESCAPE Warning Originally published in Chinese on 2020-03-15; this English edition preserves the original scope and technical context. const char* Foo() { std::string str_msg("test"); return str_msg.c_str(); } int main() { const char *p_msg = Foo(); printf("%s\n", p_msg); return 0; } // output: (empty, or garbled) D? | Above code’s Foo function is reported with the coverity warning WRAPPER_ESCAPE. The detailed explanation is as follows:| Above code, the Foo function reports a WRAPPER_ESCAPE warning from Coverity. The issue is detailed as follows: ...

March 15, 2020 · 2 min · Zhengyu Chen

C++ Coroutines (1): Functions and Coroutines

C++ Coroutines (1): Functions and Coroutines Originally published in Chinese on 2020-01-20; this English edition preserves the original scope and technical context. This article aims to explore the mechanism and usage of coroutines in C++, and how to leverage the properties of coroutines to build libraries and applications at the upper layer. Stack Frames and Functions Stack frames are environments for a function call, including parameters, return addresses, and local variables of the function. Each time an operating system invokes a function, it allocates a new stack frame for it. Related concepts include: ...

January 20, 2020 · 6 min · Zhengyu Chen

C++ Smart Pointers (3): shared_ptr

C++ Smart Pointers (3): shared_ptr Originally published in Chinese on 2019-01-25; this English edition preserves the original scope and technical context. Analysis UniquePointer objects can only bind to a single pointer. To achieve automatic management and destruction of the pointer, a counter is needed. private: int *counter; T *pointer; D *deleter; The primary function of the counter is to indicate how many smart pointer objects reference the current pointer. When the destructor of the current object is called, it decrements the counter by one. If the counter equals zero, it means that no other objects are using the current pointer. At this point, the pointer can be destroyed, along with the counter and the destructor. ...

January 25, 2019 · 8 min · Zhengyu Chen

C++ Smart Pointers (2): unique_ptr

C++ Smart Pointers (2): unique_ptr Originally published in Chinese on 2019-01-19; this English edition preserves the original scope and technical context. Analysis When using AutoPointer, there are issues with ownership transfer and memory leaks. Therefore, we can modify the AutoPointer class to fix these problems. Ownership Transfer To avoid potential ownership transfers, we can directly disallow the use of the copy constructor and assignment operator. UniquePointer(UniquePointer<T> &other) = delete; UniquePointer<T> &operator=(const UniquePointer<T> &other) = delete; But often we need to use operations involving pointers. If we only use the deleted function to prohibit copy constructor and assignment operators, the meaning of this smart pointer becomes less significant. We can achieve move semantics through move semantics to implement move constructor and move assignment operators. This way, when using UniquePointer, we can transfer ownership under certain circumstances. ...

January 19, 2019 · 8 min · Zhengyu Chen

C++ Smart Pointers (1.5): Move Semantics

C++ Smart Pointers (1.5): Move Semantics Originally published in Chinese on 2019-01-02; this English edition preserves the original scope and technical context. Move Semantics Definition Right-Value References (Rvalue References) are a new feature introduced in C++ 11, which implements move semantics and perfect forwarding. The primary purposes include: Eliminating unnecessary object copies during interactions between two objects, conserving storage resources and improving efficiency. Providing a more concise and explicit definition for generic functions. Implementation The implementation of move semantics is quite simple. It transforms the passed parameter _Tp&& __t into the corresponding type’s right value using static type conversion static_cast<_Up&&>(__t). Thus, by using move semantics, the compiler steals (generally in the move constructor and move assignment operator) the original object’s right value, extending its lifetime and using it to assign to other objects without performing any copying of the right value. ...

January 2, 2019 · 4 min · Zhengyu Chen