Skip to content

Latest commit

 

History

History
157 lines (109 loc) · 6.2 KB

File metadata and controls

157 lines (109 loc) · 6.2 KB

Pointers

⏱ Agenda

  1. [20m] ☀️ Warm Up: Deep Thoughts on Variables
  2. [30m] 💬 TT: Pointers
    1. How RAM Works (Whiteboard)
    2. Pointer Definition
    3. Pointers Analogy: URLs
    4. When To Use Pointers
    5. Pointers in Golang
      1. Operators: * and &
      2. Declaring and Initializing Pointers
      3. Rewriting the Warmup Using Pointers
  3. [10m] 🌴 BREAK
  4. [30m] 💻 Activity: Pointer Drills
|

[20m] ☀️ Warm Up: Deep Thoughts on Variables

  1. What is a variable?
  2. What part(s) of a variable are stored?
  3. Where are these things stored in the computer?
  4. Predict the output of the code snippet below. Justify your answer.
func zero(x int) {
  x = 0
}

func main() {
  x := 5
  zero(x)
  fmt.Println(x)
}

[30m] 💬 TT: Pointers

We use pointers because it's easier to give someone an address to your home than to give a copy of your home to everyone.
–  Rishi Dua Oct 4 '13 at 14:51

How RAM Works (Whiteboard)

  • Imagine a sequence of boxes, placed one after another in a line.
  • Each box is labeled with a unique number, which increments sequentially; this is the address of the box, its memory location.
  • Each box can fit only one value.
  • That value can be an integer, or a string, or an instance of a struct with 100 defined fields.
  • If you know the memory address of a box, you can go to that box and see what's inside.
  • You can place a new value in that box; anything that was inside the box vanishes and is replaced by the new value.

Pointer Definition

  • Pointers are a way of getting an indirect reference to another variable.
  • Variables hold values. Pointers hold the address where the variable's values are stored in memory.
  • Pointers allow us to access or modify the original value of a variable from anywhere in the program.

Pointers Analogy: URLs

Pointers exist all over the place in the real world.

A great example are links on any web page!

A link on any page points to another page on the internet. When you copy the link and paste it into another web page, both links will point to the same, original web page. If the webmaster modifies that web page, the links will yield the new, updated page.

When To Use Pointers

  • Pointers are important in many data structures whose design requires the ability to link or chain one "node" to another efficiently. You would not "choose" a pointer over say a normal data type like float, they simply have different purposes.
  • Pointers are useful where you require high performance and/or compact memory footprint.

Pointers in Golang

Operators: * and &

  • Pointers are declared using an asterisk (*) followed by the type of the stored value.
  • An asterisk is also used to dereference pointer values. Dereferencing a pointer returns the value stored at the address the pointer is holding.
  • The & operator finds the address of a variable.

Declaring and Initializing Pointers

Pointers can be declared for addresses with primitive types, as well as structs, slices, maps, functions, and even other pointers!

package main

import "fmt"

func main() {
    var x int = 5748

    // declare pointer
    var p *int

    // initialize pointer
    p = &x

   // displaying the result
    fmt.Println("Value stored in x = ", x)
    fmt.Println("Address of x = ", &x)
    fmt.Println("Value stored in variable p = ", p)

QUESTION: Comment out the line where the pointer is initialized, then run the program again. What is the value stored in p?

Rewriting the Warmup Using Pointers

When we call any function with an argument in Golang, that argument is copied to the function. The zero function cannot modify the original x variable declared in the main function, so x still equals 5.

What if we wanted to modify the original variable?

We could use a pointer!

func zero(xPtr *int) {
  *xPtr = 0
}
func main() {
  x := 5
  zero(&x)
  fmt.Println(x) // x is 0
}

[10m] 🌴 BREAK

[45m] 💻 Activity: Pointer Drills

Visit Gradescope and begin working on Drill 3: Pointers.

📚 Resources & Credits