Explore essential concepts in C and C++ performance optimization, including memory usage, compiler behavior, and efficient programming techniques. This quiz is designed to reinforce understanding of low-level language features and practical strategies that impact program efficiency.
What is one typical benefit of using the 'inline' keyword for a small function in C++?
Explanation: Using 'inline' suggests to the compiler to replace the function call with the function’s code, reducing the overhead of a call. It does not disable recursion by itself, nor does it convert code to assembly directly. The keyword is only a hint and does not force the compiler to ignore the function definition.
Why is loop unrolling sometimes used as a performance optimization technique in C/C++?
Explanation: Loop unrolling reduces the overhead of loop control instructions by performing more work per iteration. It does not affect recursion or necessarily improve code readability, and it's not primarily aimed at suppressing warnings. The main goal is faster execution by reducing the number of jumps and comparisons.
For optimal cache performance when processing large arrays in C, which data access pattern is preferred?
Explanation: Sequential access ensures that data is loaded into the cache efficiently due to spatial locality. Random or skipping patterns do not make good use of cache lines, and focusing on just one element doesn't utilize cache bandwidth. Accessing sequentially benefits most from the memory subsystem.
How does a 'static' local variable differ from an automatic local variable in C?
Explanation: Static local variables keep their value between invocations because they are stored in static storage. They are not kept on the stack, unlike automatic variables. Automatic locals are initialized every time the function is called and are not visible outside the function.
Given 'int arr[10];', what is a valid result of the expression 'arr + 3' in C?
Explanation: In pointer arithmetic, 'arr + 3' advances the pointer to the address of the fourth element, since arrays are zero-indexed. It does not yield the number 3, nor does it directly dereference or cause a compilation error if used correctly. Pointer addition is a valid low-level operation in C.
What is the purpose of the 'restrict' keyword in C function parameters?
Explanation: 'restrict' tells the compiler no other pointer will access the memory object, allowing better optimization. It does not make variables constant or allocate memory, nor does it relate to static storage. The keyword is specifically for optimizing pointer usage.
Which practice helps prevent memory leaks when using dynamic memory allocation with 'malloc' in C?
Explanation: Memory allocated with 'malloc' must be released with 'free' to avoid leaks. Using automatic variables or static arrays can avoid allocation altogether, but do not address leaks if dynamic allocation is used. Avoiding pointer arithmetic alone does not prevent leaks.
What optimization benefit does short-circuit evaluation provide in C logical expressions such as 'if(a u0026u0026 b)'?
Explanation: Short-circuiting means that if the first operand of 'u0026u0026' is false, the second is not evaluated, saving processing time. This reduces code execution, not increases it. All conditions are not always checked, and it actually enables, not disables, further optimization.
Which statement accurately describes a zero-cost abstraction in C++?
Explanation: A zero-cost abstraction strives to be as efficient as hand-written code, with no extra overhead. It is not related to runtime interpretation, increased code size, or macro-only usage. The goal is to provide both expressiveness and speed.
Why should function parameters be declared as 'const' when possible in C/C++?
Explanation: 'const' parameters signal that values will not change, allowing both the compiler and the reader to assume immutability, which can lead to better optimization. It does not affect function calls, force heap usage, or prevent inlining. Using 'const' is a best practice for clarity and performance.