In a “i < vector.size()" loop condition, is size() called each iteration?

In theory, it is called each time, since a for loop: for(initialization; condition; increment) body; is expanded to something like { initialization; while(condition) { body; increment; } } (notice the curly braces, because initialization is already in an inner scope) In practice, if the compiler understands that a piece of your condition is invariant through … Read more

GCC(/Clang): Merging functions with identical instructions (COMDAT folding)

Neither GCC nor Clang is a linker, and ICF needs to be done by the linker, or at least with cooperation with the linker. Edit: They don’t do ICF, so yes, distinct instantiations produce distinct code. The GNU gold linker supports ICF with the –icf option, which needs the GCC option -ffunction-sections to be used. … Read more

Why does compiler inlining produce slower code than manual inlining?

Short Answer: Your asd array is declared as this: int *asd=new int[16]; Therefore, use int as the return type rather than bool. Alternatively, change the array type to bool. In any case, make the return type of the test function match the type of the array. Skip to bottom for more details. Long Answer: In … Read more

How does switch compile in Visual C++ and how optimized and fast is it?

A switch is often compiled to a jump-table (one comparison to find out which code to run), or if that is not possible, the compiler may still reorder the comparisons, so as to perform a binary search among the values (log N comparisons). An if-else chain is a linear search (although, I suppose, if all … Read more

strcpy()/strncpy() crashes on structure member with extra space when optimization is turned on on Unix?

What you are doing is undefined behavior. The compiler is allowed to assume that you will never use more than sizeof int64_t for the variable member int64_t c. So if you try to write more than sizeof int64_t(aka sizeof c) on c, you will have an out-of-bounds problem in your code. This is the case … Read more

Benefits of ‘Optimize code’ option in Visual Studio build

You are the only person who can answer the “performance hit” question. Try it both ways, measure the performance, and see what happens. The hit could be enormous or it could be nonexistant; no one reading this knows whether “enormous” to you means one microsecond or twenty minutes. If you’re interested in what optimizations are … Read more

GCC removes a bounds check in the right operand of &&, but not in the left operand, why?

Accessing an array out of bounds is undefined behavior so the compiler can assume that it never happens in the LHS of the && expression. It is then jumping through hoops (optimizations) to notice that since ARRAY_LENGTH is the length of the array, the RHS condition must necessarily hold true (otherwise UB would ensue in … Read more