File: fields2.c

package info (click to toggle)
lclint 1%3A2.4b-1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 7,996 kB
  • ctags: 9,589
  • sloc: ansic: 100,034; lex: 2,754; yacc: 2,472; makefile: 835
file content (47 lines) | stat: -rw-r--r-- 1,089 bytes parent folder | download | duplicates (10)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
typedef struct 
{
  int *x;
  int *y;
  /*@dependent@*/ int *z;
} *pair;

extern void pair_keep (/*@keep@*/ pair p);

extern /*@only@*/ /*@out@*/ void *smalloc (size_t);
extern /*@partial@*/ pair pair_part (void);

/*@only@*/ pair pair_copy (pair p)
{
  pair ret = (pair) smalloc (sizeof (*ret));

  ret->x = p->x;
  ret->y = p->y;
  ret->z = p->z;

  return (ret); /* 1. Storage p->x reachable from parameter is kept (should be only) */
}               /* 2. Storage p->y reachable from parameter is kept (should be only) */

/*@only@*/ pair pair_create (void)
{
  pair p = (pair) smalloc (sizeof (*p));

  p->x = smalloc (sizeof (int));
  p->y = smalloc (sizeof (int));
  p->z = p->y; /* 3. Only storage p->y assigned to dependent: p->z = p-y */

  *(p->x) = 3;
  *(p->y) = 12;

  return p; /* 4. Storage p->y reachable from return value is unqualified */
}

pair pair_swankle (/*@keep@*/ pair p)
{
  pair ret = pair_part ();
 
  ret->x = p->x;
  pair_keep (p); /* 5. Storage p->x reachable from passed parameter is kept */ 
  p->x = smalloc (sizeof (int));
  *p->x = 3;
  return ret;
}