Address and dereference operators in Go - Time & Space Complexity
When using address and dereference operators in Go, it's important to see how accessing values through pointers affects the program's speed.
We want to know how the time to access or change data grows as we work with more elements.
Analyze the time complexity of the following code snippet.
package main
func incrementAll(nums []int) {
for i := 0; i < len(nums); i++ {
p := &nums[i] // get address
*p = *p + 1 // dereference and increment
}
}
This code loops through a list of numbers, uses the address operator to get each element's location, then uses dereference to increase its value by one.
- Primary operation: Looping through each element in the slice and accessing it via pointer.
- How many times: Exactly once for each element in the input slice.
As the number of elements grows, the number of pointer accesses and increments grows at the same rate.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 increments and pointer accesses |
| 100 | 100 increments and pointer accesses |
| 1000 | 1000 increments and pointer accesses |
Pattern observation: The work grows directly with the number of elements, so doubling elements doubles the work.
Time Complexity: O(n)
This means the time to complete the task grows in a straight line with the number of elements.
[X] Wrong: "Using pointers makes the code run faster and changes the time complexity."
[OK] Correct: Using pointers changes how you access data but does not reduce the number of operations needed to process each element.
Understanding how pointer operations scale helps you explain memory access patterns clearly and shows you grasp how low-level details affect program speed.
"What if we replaced the loop with recursion to increment elements? How would the time complexity change?"
Practice
& operator do in Go?Solution
Step 1: Understand the
The&operator&operator is used to get the address of a variable in memory.Step 2: Differentiate from
The*operator*operator is used to access the value at an address, not to get the address itself.Final Answer:
It returns the memory address of a variable. -> Option BQuick Check:
&operator = address retrieval [OK]
- Confusing & with * operator
- Thinking & multiplies values
- Assuming & declares variables
num in Go?Solution
Step 1: Understand pointer declaration syntax
In Go, a pointer to an int is declared as*int. To assign it, use the address operator&on the variable.Step 2: Check each option
var p *int = &num correctly declarespas a pointer to int and assigns the address ofnum. Others misuse types or operators.Final Answer:
var p *int = &num -> Option CQuick Check:
Pointer declaration =var p *int = &num[OK]
- Using * on variable instead of type
- Using & with type instead of variable
- Mixing variable and type names
package main
import "fmt"
func main() {
x := 10
p := &x
*p = 20
fmt.Println(x)
}Solution
Step 1: Understand pointer assignment
Variablepstores the address ofx. Using*p = 20changes the value at that address.Step 2: Check final value of
Sincex*ppoints tox, changing*pto 20 updatesxto 20.Final Answer:
20 -> Option DQuick Check:
Dereferencing pointer changes original variable [OK]
- Thinking *p = 20 creates new variable
- Expecting print to show address
- Confusing & and * operators
func main() {
var a int = 5
var p *int
*p = a
fmt.Println(*p)
}Solution
Step 1: Check pointer initialization
Pointerpis declared but not assigned an address before dereferencing with*p = a.Step 2: Understand consequences
Dereferencing an uninitialized pointer causes a runtime panic (nil pointer dereference).Final Answer:
Pointer p is not initialized before dereferencing. -> Option AQuick Check:
Dereference only initialized pointers [OK]
- Dereferencing nil pointer
- Confusing pointer and value assignment
- Assuming fmt.Println can't print pointers
package main
import "fmt"
func updateValue(p *int) {
*p = *p + 5
}
func main() {
val := 10
updateValue(&val)
fmt.Println(val)
}Solution
Step 1: Understand function parameter
The functionupdateValuetakes a pointer to an int. Passing&valsends the address ofval.Step 2: Analyze pointer dereference and update
Inside the function,*p = *p + 5adds 5 to the value at the address, updatingvalto 15.Final Answer:
15 -> Option AQuick Check:
Pointer parameter updates original variable [OK]
- Expecting original val unchanged
- Confusing pointer with value copy
- Thinking function returns new value
Ready to prove it?
Pass the test and this pattern moves unknown
