0

Tried to use a macro instead of a small function to acces an Array on position "index mod 256":

static uchar08 ACC_MOD_256(uchar08 * buf, ulong32 index)
{
    return buf[index % 256];
}

This works fine and just as expected.

#define ACC_MOD_256(arr, ind)   (arr[ind % 256])

However the modulo (%) seems not to be working when the code is used as a macro. The first time the index gets over 255, I get a stack overflow.

I'm quite new to macros, so forgive me if I made an obvious mistake here.

EDIT: MCVE, as asked:

#define ACC_MOD_256(arr, ind)   (arr[(ind) % 256])

int main()
{
    int value;
    int array[256] = {0};

    fillArray(array);


    for(int i = 0; i < 1024; i++)
    {
        value = ACC_MOD_256(array, i + 3);
    }

    return 0;
}

I can see now, thanks to squeamish ossifrage, that the macro will parse this line:

value = arr[i + 3 % 256];

Which apparently is equivalent to:

value = arr[i + (3 % 256)];

Which is equivalent to:

value = arr[i + 3];

Which will fail once 'i + 3' is bigger than the array.

Ultra.-
  • 1
  • 2

0 Answers0