2 ms·
A '->' is always a runtime indirection involving an extra memory access, while a '.' is always resolved into a single offset at compile time. E.g. a: int x =
by flohofwoe 6y ago
A '->' is always a runtime indirection involving an extra memory access, while a '.' is always resolved into a single offset at compile time.
E.g. a:
int x = a->b->c->d;
means there's 3 memory accesses, while
int x = a.b.c.d;
means there's one memory access for the whole expression.
Also consider this:
int x = a->b.c->d;
I can immediately see where pointer indirections are happening.
...unless you're in C++ of course which messed up this simple rule when references were added to the language.
- jhallenworld 6y ago>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
- flohofwoe 6y agoThat'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.