I know that "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 would have assumed the code looked safe enough. In this case, the real problem happened only on a specific platform using a...
For test purpose, I need to construct an object on non-zero memory. This could be done with:Since this is tedious and made multiple times, I'd like to provide a function returning a smart pointer to such a Type instance. I came up with the following, but I fear undefined behavior...
The following code works in clang++, but crashes spectacularly in g++The culprit turns out to be this line:
In C++11, according to en.cppreference.com,For signed and non-negative a, the value of a << b is a * 2b if it is representable in the return type, otherwise the behavior is undefined.
https://en.cppreference.com/w/cpp/language/lifetime in Notes section has this code, reproduced here:What is it trying to say in this Notes section?
For example, in one source file:and in a second source file we have:Does the code above cause undefined behavior? As far as I know, in C it says:
A couple of months ago I asked this question where I asked why there was a memory leak. Apparently, I forgot a virtual destructor.
I'm compiling a C++ program to run in a freestanding environment and the CPU I'm running on defines a 32-bit peripheral register to be available (edit: memory-mapped) at PERIPH_ADDRESS (aligned correctly, and not overlapping with any other C++ object, stack etc.).
I stumbled upon the following code snippet:The text said that this snippet should cause a runtime error. Now, I wasn't really sure about that, so I tried to compile and run it. It worked. The weird thing is, despite the simplicity of the data involved, the program stuttered after printing...
Since C++17, associative containers support the extraction of a node and its re-insertion (possibly into another container of the same type). The object returned by extract(key) is a node_handle, which is move-only and for map containers has member functions