Common pointer use cases in Go - Time & Space Complexity
When using pointers in Go, it's important to understand how they affect the speed of your program.
We want to see how the program's running time changes when pointers are used in common ways.
Analyze the time complexity of the following code snippet.
package main
func incrementAll(nums []*int) {
for _, p := range nums {
*p = *p + 1
}
}
This code takes a slice of pointers to integers and adds 1 to each integer by using the pointers.
- Primary operation: Looping through the slice of pointers and dereferencing each pointer to update the value.
- How many times: Once for each element in the slice (n times if the slice has n elements).
As the number of pointers in the slice grows, the time to update all values grows in a straight line.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 updates |
| 100 | 100 updates |
| 1000 | 1000 updates |
Pattern observation: The time grows directly with the number of elements; doubling the slice doubles the work.
Time Complexity: O(n)
This means the time to complete the task grows in direct proportion to the number of pointers you have.
[X] Wrong: "Using pointers makes the loop run faster because it avoids copying values."
[OK] Correct: While pointers avoid copying data, the loop still visits every element, so the total time still grows with the number of elements.
Understanding how pointers affect time helps you explain your code choices clearly and shows you know how your program scales with data size.
"What if we changed the slice of pointers to a slice of values? How would the time complexity change?"
Practice
Solution
Step 1: Understand what a pointer represents
A pointer holds the location in memory where a variable is stored, not the value itself.Step 2: Differentiate pointer from value
The value is stored at the address the pointer points to, not inside the pointer variable.Final Answer:
The memory address of a variable -> Option AQuick Check:
Pointers store addresses = B [OK]
- Confusing pointer with the actual value
- Thinking pointer stores variable type
- Assuming pointer stores variable size
Solution
Step 1: Recall Go pointer syntax
In Go, a pointer type is written as *Type, so a pointer to int is *int.Step 2: Check variable declaration format
The variable name comes after var, then the type. So correct is var p *int.Final Answer:
var p *int -> Option CQuick Check:
Pointer syntax is var name *Type = D [OK]
- Placing * after variable name
- Using & instead of * for pointer type
- Incorrect order of * and variable name
func main() {
x := 10
p := &x
*p = 20
fmt.Println(x)
}Solution
Step 1: Understand pointer assignment
p points to x's address. Changing *p changes the value at x's address.Step 2: Trace value change
*p = 20 sets x to 20. So printing x outputs 20.Final Answer:
20 -> Option DQuick Check:
Pointer changes original value = 20 [OK]
- Thinking x remains 10 after *p = 20
- Printing pointer address instead of value
- Assuming compilation error due to pointer
func updateValue(p *int) {
*p = 100
}
func main() {
var p *int
updateValue(p)
}Solution
Step 1: Check pointer initialization
p is declared but not assigned an address, so it is nil.Step 2: Analyze dereferencing nil pointer
updateValue dereferences *p, causing a runtime panic because p is nil.Final Answer:
p is nil and dereferenced causing runtime panic -> Option BQuick Check:
Dereferencing nil pointer causes panic = A [OK]
- Assuming nil pointer is safe to dereference
- Thinking function must return a value
- Ignoring runtime panic possibility
func swap(a, b *int) {
*a, *b = *b, *a
}
func main() {
x, y := 5, 10
swap(&x, &y)
fmt.Println(x, y)
}Solution
Step 1: Understand swap function with pointers
swap receives pointers to x and y, then swaps the values they point to.Step 2: Trace swap effect on variables
After swap, x becomes 10 and y becomes 5.Final Answer:
10 5 -> Option AQuick Check:
Pointer swap changes original values = 10 5 [OK]
- Expecting original values unchanged
- Printing pointer addresses instead of values
- Thinking swap function syntax is invalid
Ready to prove it?
Pass the test and this pattern moves unknown
