In this post, we will see how to resolve Compiler optimizations may cause integer overflow. Is that okay? Question: I have an int x. For simplicity, say ints occupy the range -2^31 to 2^31-1. I want to compute 2*x-1. I ...

In this post, we will see how to resolve Double free in the C++ standard library using only std::function and std::shared_pointer Question: I recently came across a weird double-free bug in a program when capturing a shared_ptr in a lambda. ...

Question: I wonder how the compilers deal with undefined behavior. I will take GCC 10.4 for x86 architecture and -O2 -std=c++03 flags as an example, but please feel free to comment on other compilers. What does it take to alter ...

Question: I know that an “undefined behaviour” in C++ can pretty much allow the compiler to do anything it wants. However, I had a crash that surprised me, as I assumed that the code was safe enough. In this case, ...

Question: What are “sequence points”? What is the relation between undefined behaviour and sequence points? I often use funny and convoluted expressions like a[++i] = i;, to make myself feel better. Why should I stop using them? If you’ve read ...

Question: In <napi.h>, there are some overloads declaration for the static method Accessor in the class PropertyDescriptor. One of such overloads declaration is here: That declaration wants a reference to a const std::string The definition of that declaration is in ...

Question: I am wondering about a short code example of an application of downcast via static_cast, under conditions where there is no undefined behaviour. I have looked around and I found quite a few texts (posts, Q&A, etc.) referring to ...

Question: On Windows, wchar_t is a UTF-16(LE) formatted character, which is — for the most part — equivalent to char16_t. However, these two character types are still distinct types in the C++ type-system — which makes me uncertain whether converting ...

Question: I have no idea why suddenly the value of integer is changing even though there’s no process of caluclation, here’s the code of my program : So if I input the value of n lower than 5, there’s no ...

Question: How does default initialization work in C++11 if the default constructor is explicit? For example: In main, the variable foo is default initialized. Based on my understanding, this will call a default constructor, if one exists. Otherwise, no initialization ...