Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.
- From: John Reye <jononanon@xxxxxxxxxxxxxx>
- Date: Sun, 27 May 2012 10:30:55 -0700 (PDT)
Hi!
In writing code I often find numerous ways of expressing the same
thing.
I have some questions of "style" and "optimization" that I'd like to
ask.
Example1
========
Variant 1a)
arr[i].a = f1();
arr[i].b = f2();
arr[i].c = f3();
OR
Variant 1b)
struct tmp * const pArr = &arr[i]; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
Here I probably prefer the 1b). But not on reasons of style, but on
the suspicion (*) that some compilers will produce more efficient code
with the 1b).
(*) - what do you think?
Example2
========
Variant 2a)
arr[i].a = f1();
arr[i].b = f2();
arr[i].c = f3();
arr[i].d[j++] = f4();
arr[i].d[j] = f5();
OR
Variant 2b)
struct tmp * const pArr = &arr[i]; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
pArr->d[j++] = f4();
pArr->d[j] = f5();
OR
Variant 2c)
struct tmp * const pArr = &arr[i]; // arr + i
pArr->a = f1();
pArr->b = f2();
pArr->c = f3();
int * pd = &(pArr->d[j++]);
*pd++ = f4();
*pd = f5();
Here I'd definatley NOT use 2a) because I suspect that double array-
indexing via 2 `[]'-operators is not efficient.
I'd use either 2b) or 2c).
What are you're opinions on this? Which style do you use?
Any tips are highly appreciated. Thanks.
.
- Follow-Ups:
- Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.
- From: Tim Rentsch
- Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.
- From: Thomas Richter
- Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.
- From: Eric Sosman
- Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.
- From: John Reye
- Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.
- Prev by Date: Re: Open letter to Ian Collins
- Next by Date: Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.
- Previous by thread: "Open letter" to jake
- Next by thread: Re: Array addressing styles: arr[i].d[j] versus pArr->d[j], etc.
- Index(es):