3

Is this safe?

foo.h:

struct A {
        struct B *b;
        // more fields
};
struct B {
        // some fields
};

void foo(struct A *restrict a, struct B *restrict b);

foo.c:

void foo(struct A *restrict a, struct B *restrict b)
{
        a->b = &b;
}

bar.c:

void bar(void)
{
        struct A a;
        struct B b;

        // some code. a and b are independent here.
        foo(&a, &b);
        // some more code.  Now a and b aren't independent anymore.
}

I wonder if the compiler may incorrectly assume from the function declaration that a can't be used to access b after foo(&a, &b); and therefore may optimize things that shouldn't.

0 Answers0