Source-Changes-D archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: CVS commit: src/sys/kern



On 02.08.2020 16:44, Taylor R Campbell wrote:
>> Date: Sun, 2 Aug 2020 16:04:15 +0200
>> From: Kamil Rytarowski <kamil%netbsd.org@localhost>
>>
>> On 02.08.2020 15:57, Taylor R Campbell wrote:
>>> But it sounds like the original motivation is that it triggered
>>> -Wvla...which frankly strikes me as a compiler bug since there's
>>> obviously no actual VLA created in sizeof; as far as I can tell
>>> there's no semantic difference between sizeof(device_t[n]) and
>>> sizeof(device_t) * n.
>>
>> This is not true:
> 
> Which part of what I said are you claiming is not true, and what are
> you illustrating with the example program below?
> 

Calling it a compiler bug.

Clang behaves the same way.

$ clang -Wvla test.c
test.c:6:37: warning: variable length array used [-Wvla]
        printf("sizeof = %zu\n", sizeof(int[argc]));
                                           ^
1 warning generated.

Creating VLA is not needed for using it as an intermediate. In practice
in most/all cases it is optimized and actual VLA is not allocated.

> It seems to illustrate that sizeof(int[argc]) does exactly what one
> would expect it to do -- return the size of an argc-length array of
> ints, just like sizeof(int) * argc does.
> 
> 

The result is the same and I find the change as beneficial.

> 
>> #include <stdio.h>
>>
>> int
>> main(int argc, char **argv)
>> {
>>         printf("sizeof = %zu\n", sizeof(int[argc]));
>>         return 0;
>> }
>>
>> $ ./a.out
>>
>> sizeof = 4
>> $ ./a.out 12 3
>> sizeof = 12
>> $ ./a.out 12 3 45 6
>> sizeof = 20


Attachment: signature.asc
Description: OpenPGP digital signature



Home | Main Index | Thread Index | Old Index