Reverse iterator returns garbage when optimized

Looking at std::reverse_iterator‘s libstdc++ implementation reveals something interesting: /** * @return A reference to the value at @c –current * * This requires that @c –current is dereferenceable. * * @warning This implementation requires that for an iterator of the * underlying iterator type, @c x, a reference obtained by * @c *x remains valid … Read more

Why not always use compiler optimization?

If there is no undefined behavior, but there is definite broken behavior (either deterministic normal bugs, or indeterminate like race-conditions), it pays to turn off optimization so you can step through your code with a debugger. Typically, when I reach this kind of state, I like to do a combination of: debug build (no optimizations) … Read more

Why is initializing a string to “” more efficient than the default constructor?

This is an intentional decision in libc++’s implementation of std::string. First of all, std::string has so-called Small String Optimization (SSO), which means that for very short (or empty) strings, it will store their contents directly inside of the container, rather than allocating dynamic memory. That’s why we don’t see any allocations in either case. In … Read more

GCC: Difference between -O3 and -Os

The GCC documentation describes what these options do very explicitly. -O3 tries to optimize code very heavily for performance. It includes all of the optimizations -O2 includes, plus some more. -Os, on the other hand, instructs GCC to “optimize for size.” It enables all -O2 optimizations which do not increase the size of the executable, … Read more

Why do none of the major compilers optimize this conditional store that checks if the value is already set?

The object might be const It wouldn’t be safe for static const int val = 1; living in read-only memory. The unconditional-store version will segfault trying to write to read-only memory. The version that checks first is safe to call on that object in the C++ abstract machine (via const_cast), so the optimizer has to … Read more

Is it possible to implement bitwise operators using integer arithmetic?

First solutions for shifting (shift is the shift distance, must not be negative, a is the operand to be shifted and contains also the result when done). The power table is used by all three shift operations. // table used for shift operations powtab = { 1, 2, 4, 8, 16, 32, 64, 128, 256, … Read more

How do I make an infinite empty loop that won’t be optimized away?

The C11 standard says this, 6.8.5/6: An iteration statement whose controlling expression is not a constant expression,156) that performs no input/output operations, does not access volatile objects, and performs no synchronization or atomic operations in its body, controlling expression, or (in the case of a for statement) its expression-3, may be assumed by the implementation … Read more

Why is a switch not optimized the same way as chained if else in c/c++?

The generated code for switch-case conventionally uses a jump table. In this case, the direct return through a look-up table seems to be an optimization making use of the fact that every case here involves a return. Though the standard makes no guarantees to that effect, I would be surprised if a compiler were to … Read more

Is it true that having lots of small methods helps the JIT compiler optimize?

The Hotspot JIT only inlines methods that are less than a certain (configurable) size. So using smaller methods allows more inlining, which is good. See the various inlining options on this page. EDIT To elaborate a little: if a method is small it will get inlined so there is little chance to get penalised for … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)