Re: [dev] [st] [patch] misplaced parenthesis in LEN macro

From: Rob <robpilling_AT_gmail.com>
Date: Sun, 20 Apr 2014 20:24:37 +0100 (BST)

On 20/04/14, sin wrote:
> On Sun, Apr 20, 2014 at 02:43:53PM +0200, FRIGN wrote:
>> On Sun, 20 Apr 2014 13:53:33 +0200
>>
>> #define LEN(a) (sizeof a / sizeof *a)
>>
>> is the right way to do it.
>
> You are missing the parentheses there.
>
> Your macro provides the wrong answer for something like:
>
> int a[] = { 1, 2, 3, 4, 5 };
> printf("%zu\n", LEN(a + 2));

Into the bikeshed I go...

LEN(a + 2) doesn't mean anything anyway, as a's type decays.

To do it properly there should be some kind of static assert in the
macro that the argument is of array type. But this is a small code base
and you'd expect that the code would be run and checked before
committing, which renders the assert pretty useless.

I think it's fine as it is, in the original C way of doing things,
garbage in, garbage out, undefined behaviour etc etc.

Rob
Received on Sun Apr 20 2014 - 21:24:37 CEST

This archive was generated by hypermail 2.3.0 : Sun Apr 20 2014 - 21:36:06 CEST