Reference declaration in C++ - Time & Space Complexity
Let's see how declaring references affects the time it takes for a program to run.
We want to know if using references changes how the program's work grows as input gets bigger.
Analyze the time complexity of the following code snippet.
int sumArray(int arr[], int n) {
int &total = *new int(0); // reference to an int
for (int i = 0; i < n; i++) {
total += arr[i];
}
int result = total;
delete &total; // free memory
return result;
}
This code sums all elements of an array using a reference to an integer.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The for-loop that adds each array element to total.
- How many times: Exactly n times, once for each element in the array.
As the array size grows, the loop runs more times, adding each element once.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 10 additions |
| 100 | About 100 additions |
| 1000 | About 1000 additions |
Pattern observation: The work grows directly with the number of elements.
Time Complexity: O(n)
This means the time to finish grows in a straight line as the input size grows.
[X] Wrong: "Using a reference makes the loop run faster or slower."
[OK] Correct: The reference itself does not add extra loops or slow down the repeated work; it just points to the same memory, so the main work is still the loop over the array.
Understanding how references affect time helps you explain your code clearly and shows you know what really costs time in programs.
"What if we replaced the reference with a normal integer variable inside the loop? How would the time complexity change?"
Practice
Solution
Step 1: Understand the definition of a reference
A reference acts as another name for an existing variable, not a separate variable.Step 2: Compare with other options
Unlike pointers or constants, references directly alias the original variable.Final Answer:
An alias for another variable -> Option CQuick Check:
Reference = Alias [OK]
- Confusing references with pointers
- Thinking references create new memory
- Believing references can be reassigned
x?Solution
Step 1: Recall correct reference syntax
References are declared with the & symbol after the type:int &ref = x;.Step 2: Check other options for syntax errors
int ref& = x; places & incorrectly, C uses parentheses which is invalid here, D uses pointer syntax.Final Answer:
int &ref = x; -> Option AQuick Check:
Reference syntax = type &name = variable [OK]
- Placing & after variable name
- Using parentheses instead of = for initialization
- Confusing references with pointers (& usage)
int a = 5; int &ref = a; ref = 10; std::cout << a << std::endl;
Solution
Step 1: Understand reference assignment
Referencerefaliasesa, so assigning torefchangesa.Step 2: Trace the output
Afterref = 10;,abecomes 10, so printingaoutputs 10.Final Answer:
10 -> Option DQuick Check:
Reference assignment changes original variable [OK]
- Thinking ref is separate variable
- Expecting original variable unchanged
- Confusing reference with pointer syntax
int x = 5; int &ref; ref = x;
Solution
Step 1: Check reference declaration rules
References must be initialized at declaration;int &ref;without initialization is invalid.Step 2: Analyze the code
Here,refis declared without initialization, causing a compilation error.Final Answer:
Reference must be initialized when declared -> Option BQuick Check:
References need initialization [OK]
- Declaring references without initialization
- Confusing assignment after declaration with initialization
- Ignoring compiler errors on references
int a = 3; int b = 4; int &ref = a; ref = b; std::cout << a << ", " << b << std::endl;What does this demonstrate about references?
Solution
Step 1: Understand reference initialization
refis a reference toa, fixed at initialization.Step 2: Analyze
This assigns the value ofref = b;btoathroughref, not reassigning the reference.Step 3: Determine output
After assignment,abecomes 4,bremains 4, so output is4, 4.Final Answer:
Reference assignment changes the original variable's value -> Option AQuick Check:
Reference fixed; assignment changes value [OK]
- Thinking reference can be reassigned to another variable
- Confusing reference with pointer behavior
- Assuming reference copies value
Ready to prove it?
Pass the test and this pattern moves unknown
