Referring to array element by descriptive name

tcak via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Jan 14 07:28:20 PST 2017


On Saturday, 14 January 2017 at 15:11:40 UTC, albert-j wrote:
> Is it possible to refer to an array element by a descriptive 
> name, just for code clarity, without performance overhead? E.g.
>
> void aFunction(double[] arr) {
>     double importantElement = arr[3];
>     ... use importantElement ...
> }
>
> But the above, I suppose, introduces an extra copy operation?

Unless the item type of that array is a complex like a big 
struct, copying basic types won't have much effect at all. You 
wouldn't notice it.

You could point to that element with a pointer:

double* importantElement = &arr[3];

But then you are going to define that pointer variable anyway. On 
top of that, for every access, instead of using the available 
data, CPU would look at the pointed memory address to get the 
value again and again (ignoring the cache).


More information about the Digitalmars-d-learn mailing list