News for hackers. Stuff that matters.
Re:What is your favorite C programming trick? (2009) by jhallenworld on Sunday November 22, 2020 @11:02AM (#25178494)
>A '->' is always a runtime indirection

It's not true if the compiler can figure out that the left side is a constant, consider:

    struct foo { int a; };
    struct foo z = { 7 };
    struct foo *const p = &z;
Then in z->a, no indirection is necessary. GCC -O2 makes this:

    int fred() { return p->a; }

    fred:
        movl    z(%rip), %eax
        ret

    p:
        .quad   z

    z:
        .long   7
Re:What is your favorite C programming trick? (2009) by flohofwoe on Sunday November 22, 2020 @11:11AM (#25178572)
That's (usually) only true for the very first '->' in a chain and as you said, depends on the compiler figuring out if the pointer indirection can be resolved at compile time.

A chain of '.' on the other hand is always guaranteed to be resolved into a single offset at compile time.