Skip to content

Instantly share code, notes, and snippets.

@spytheman
Last active September 23, 2019 08:24
Show Gist options
  • Select an option

  • Save spytheman/673b733b1759d8eb1fefc82eb4e3f90a to your computer and use it in GitHub Desktop.

Select an option

Save spytheman/673b733b1759d8eb1fefc82eb4e3f90a to your computer and use it in GitHub Desktop.
0[11:23:46] /v/v $ cat x.v
struct Node {
mut:
head &Node
tail &Node
}
fn (n Node) str() string {
return 'node content: { head: $n.head , tail: $n.tail }'
}
fn print_node(label string, n &Node) {
println('Node $label: ' + ptr_str(voidptr(n)) + ' : ' + n.str() )
}
mut n1 := Node{}
mut n2 := Node{}
n1.tail = &n2
n2.head = &n1
print_node('n1', n1)
print_node('n2', n2)
n1.tail = &Node(voidptr(0))
print_node('n1', n1)
0[11:23:48] /v/v $ ./v run x.v
Node n1: 0x7ffffabee4b0 : node content: { head: (nil) , tail: 0x7ffffabee4c0 }
Node n2: 0x7ffffabee4c0 : node content: { head: 0x7ffffabee4b0 , tail: (nil) }
Node n1: 0x7ffffabee4b0 : node content: { head: (nil) , tail: (nil) }
0[11:23:50] /v/v $
@spytheman
Copy link
Copy Markdown
Author

Setting the pointers to 0 after usage:

n1.tail = voidptr(0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment