IPP Software Navigation Tools IPP Links Communication Pan-STARRS Links

Ticket #21: alternate.c

File alternate.c, 720 bytes (added by eugene, 22 years ago)

example using psFree with merged DecrRef

Line 
1struct {
2 float f;
3} Foo;
4
5struct {
6 Foo *foo;
7} Bar;
8
9Foo *psFooAlloc () {
10 Foo *foo = (Foo *) psAlloc (1);
11 return (foo);
12}
13
14psFooFree (Foo *foo) {
15 psFree (foo);
16 return;
17}
18
19Bar *psBarAlloc () {
20 Bar *bar = (Bar *) psAlloc (1);
21 bar[0].foo = NULL;
22 return (bar);
23}
24
25psBarFree (Bar *bar) {
26 psFooFree (bar[0].foo);
27 psFree (bar);
28 return;
29}
30
31Bar *psBarSet (Foo *foo) {
32 Bar *bar = NULL;
33 bar = psBarAlloc ();
34 bar[0].foo = foo;
35 IncrRef (foo);
36}
37
38main () {
39
40 Foo *foo = NULL;
41 Bar *bar = NULL;
42
43 foo = psFooAlloc (); // Nref(foo) = 1
44 bar = psBarSet (foo); // Nref(foo) = 2
45 psFooFree (foo); // Nref(foo) = 1
46 psBarFree (bar); // Nref(foo) = 0
47}