Is the condition of a loop re-evaluated each iteration? [duplicate]

Yes, semantically it will be evaluated on every loop. In some cases, compilers may be able to remove the condition from the loop automatically – but not always. In particular: void foo(const struct rect *r) { for (int i = 0; i < r->width * r->height; i++) { quux(); } } The compiler will not … Read more

Is calling std::vector::size() as fast as reading a variable?

Interesting question. So, what’s going to happened ? Well if you debug with gdb you’ll see something like 3 member variables (names are not accurate): _M_begin: pointer to the first element of the dynamic array _M_end: pointer one past the last element of the dynamic array _M_capacity: pointer one past the last element that could … Read more

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