aboutsummaryrefslogtreecommitdiff
path: root/tests/programs/alias.zc
blob: 7d394c5b1b80e5b62db17fb8553a256c39ff7e4e (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace;
include rt, run;

// a pointer computed from a live object is a borrow of it: fine while the owner lives
struct node { int v; node* next; };
fn main() {
	let p = rt.new(4);
	p[0] = 11; p[1] = 22;
	let q = p + 1;
	rt.put_n(q[-1]); rt.put_c(' ');
	rt.put_n(q[0]); rt.put_c(' ');
	node* a = rt.new(node), b = rt.new(node);
	a.v = 3; b.v = 4; a.next = b;
	node* c = a.next;
	rt.put_n(c.v); rt.put_c(' ');
	int** list = rt.new(int*, 2);
	int* item = rt.new(int);
	*item = 8;
	list[1] = item;
	int* e = list[1];
	rt.put_n(*e); rt.put_c(10);
	rt.free(d*p); rt.free(d*a); rt.free(d*list);
	return 0;
}