1

I have the following for loop and would like to give the index a meaningful name, as required by my obsessive compulsive disorder coding standards.

/* Loop over 64-bit word */
for (i = 0; i < WORD_SIZE / ALIGNMENT; i++)
    /* Do work with i */

I wanted to use offset or word_offset as the name of the index, until I stumbled upon the last sentence from the technical definition of offset as given by Wikipedia:

The concept of a distance is valid only if all elements of the object are of the same size (typically given in bytes or words).

Unfortunately, all elements of a word are not the same size. I can have a 64-bit word of five elements of varying sizes (i.e. two chars, one int and one short...or 2*8, 1*32, 1*16 = 64). So according to the above definition, offset should not be the name of i.

I suppose word_idx would do since that is essentially what the index represents, however, I'm wondering if there is a more precise/technical term I'm forgetting or never heard of.

Is there a word (no pun intended) for word size divided by alignment?

Barmar
  • 741,623
  • 53
  • 500
  • 612
lolololol ol
  • 848
  • 1
  • 8
  • 18
  • 2
    If you are looping over objects of the same size, then I guess in that context the name `offset` can be used. But I'd rather use index as a name, or just keep using `i` and `j` , which is what most people use and expect when they see a `for`-loop. – Pablo Mar 02 '18 at 21:35
  • In your example, is `WORD_SIZE` something like 64? And is `ALIGNMENT` something like 8? So does `i` go from 0 to 7? Or what? – Steve Summit Mar 02 '18 at 21:41
  • 1
    That Wikipedia article does not cite a source for its claim that the concept of distance is valid only if all elements of the object are the same size. A mathematician might tell you that a [metric](https://en.wikipedia.org/wiki/Metric_(mathematics)#Definition) is a distance if it satisfies the [Triangle Inequality](https://en.wikipedia.org/wiki/Triangle_inequality). You can simply say the offset measures distance in units of elements rather than bytes, even if the elements are composed of different numbers of bytes. For example, “I have relatives that live three states over.” – Eric Postpischil Mar 02 '18 at 21:43
  • That quote from wikipedia is talking about whatever underlying elements are used for the counting. In your case, it's presumably bytes as counted by `sizeof()` and `offsetof()`. The fact that the struct contains elements of other sizes is not relevant. – Barmar Mar 02 '18 at 21:46
  • Is `i` counting bytes and increments by different amounts as objects of different size are processed, or does it count elements and increment by 1 as each object is processed? In the former case, it is an offset in units of bytes. In the latter case, it is an offset in units of elements. – Eric Postpischil Mar 02 '18 at 21:49
  • I too have the same disorder, and I use `index`. It makes more sense than `offset` IMHO. Whereas if the content of the block has any pointer arithmetics I usually use `offset` instead. – Iharob Al Asimi Mar 02 '18 at 21:54
  • @SteveSummit Yes, exactly. – lolololol ol Mar 02 '18 at 23:10

0 Answers0