Oracle® Solaris Studio 12.4: C User's Guide

Exit Print View

Updated: March 2015
 
 

5.4.4 Example: Struct Fields

Consider the following example source code.

struct foo {
        int f1;
        int f2;
} *fp;

struct bar {
        int b1;
        int b2;
} *bp;

struct cat {
        int c1;
        struct foo cf;
        int c2;
        int c3;
} *cp;

struct dog {
        int d1;
        int d2;
        struct bar db;
        int d3;
} *dp;

If this example is compiled with the -xalias_level=weak option, the compiler assumes the following alias information:

  • fp->f1 can alias bp->b1, cp->c1, dp->d1, cp->cf.f1, and df->db.b1.

  • fp->f2 can alias bp->b2, cp->cf.f1, dp->d2, cp->cf.f2, df->db.b2, cp->c2.

  • bp->b1 can alias fp->f1, cp->c1, dp->d1, cp->cf.f1, and df->db.b1.

  • bp->b2 can alias fp->f2, cp->cf.f1, dp->d2, cp->cf.f1, and df->db.b2.

fp->f2 can alias cp->c2 because *dp can alias *cp and *fp can alias dp->db.

  • cp->c1 can alias fp->f1, bp->b1, dp->d1, and dp->db.b1.

  • cp->cf.f1 can alias fp->f1, fp->f2, bp->b1, bp->b2, dp->d2, and dp->d1.

cp->cf.f1 does not alias dp->db.b1.

  • cp->cf.f2 can alias fp->f2, bp->b2, dp->db.b1, and dp->d2.

  • cp->c2 can alias dp->db.b2.

cp->c2 does not alias dp->db.b1 and cp->c2 does not alias dp->d3.

With respect to offsets, cp->c2 can alias db->db.b1 only if *dp aliases cp->cf. However, if *dp aliases cp->cf, then dp->db.b1 must alias beyond the end of foo cf, which is prohibited by object restrictions. Therefore, the compiler assumes that cp->c2 cannot alias db->db.b1.

cp->c3 can alias dp->d3.

Notice that cp->c3 does not alias dp->db.b2. These memory references do not alias because the offsets of the fields of the types involved in the dereferences differ and do not overlap. Based on this, the compiler assumes they cannot alias.

  • dp->d1 can alias fp->f1, bp->b1, and cp->c1.

  • dp->d2 can alias fp->f2, bp->b2, and cp->cf.f1.

  • dp->db.b1 can alias fp->f1, bp->b1, and cp->c1.

  • dp->db.b2 can alias fp->f2, bp->b2, cp->c2, and cp->cf.f1.

  • dp->d3 can alias cp->c3.

Notice that dp->d3 does not alias cp->cf.f2. These memory references do not alias because the offsets of the fields of the types involved in the dereferences differ and do not overlap. Based on this analysis, the compiler assumes they cannot alias.

If this example is compiled with the -xalias_level=layout option, the compiler assumes only the following alias information:

  • fp->f1, bp->b1, cp->c1 and dp->d1 all can alias each other.

  • fp->f2, bp->b2 and dp->d2 all can alias each other.

  • fp->f1 can alias cp->cf.f1 and dp->db.b1.

  • bp->b1 can alias cp->cf.f1 and dp->db.b1.

  • fp->f2 can alias cp->cf.f2 and dp->db.b2.

  • bp->b2 can alias cp->cf.f2 and dp->db.b2.

If this example is compiled with the -xalias_level=strict option, the compiler assumes only the following alias information:

  • fp->f1 and bp->b1 can alias each other.

  • fp->f2 and bp->b2 can alias each other.

  • fp->f1 can alias cp->cf.f1 and dp->db.b1.

  • bp->b1 can alias cp->cf.f1 and dp->db.b1.

  • fp->f2 can alias cp->cf.f2 and dp->db.b2.

  • bp->b2 can alias cp->cf.f2 and dp->db.b2.

If this example is compiled with the -xalias_level=std option, the compiler assumes only the following alias information:

  • fp->f1 can alias cp->cf.f1.

  • bp->b1 can alias dp->db.b1.

  • fp->f2 can alias cp->cf.f2.

  • bp->b2 can alias dp->db.b2.