[Top][All Lists]

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

Re: sizeof in Macros. (Was: Specifying dependencies more clearly)

From: Alejandro Colomar
Subject: Re: sizeof in Macros. (Was: Specifying dependencies more clearly)
Date: Mon, 13 Mar 2023 02:07:34 +0100
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.8.0

Howdy Ralph,

On 11/10/22 10:31, Ralph Corderoy wrote:
> Howdy Alejandro,


>>> My patch just changes
>>>      #define length(s)   (sizeof(s) - 1)
>>> to:
>>>      #define length(s)   (nitems(s) - 1)
>>> (nitems() is defined to be the obvious sizeof division (called
>>> ARRAY_SIZE(9) in the Linux kernel)
> The bug is the original macro str_set() doesn't document its ‘text’
> parameter must be a string constant.  Macros I have to hand are more
> explicit: ‘string constant s’.
>     /* DIM gives the number of elements in the one-dimensional array a. */
>     #define DIM(a) (sizeof (a) / sizeof (*(a)))
>     /* LEN gives the strlen() of string constant s, excluding the
>      * terminating NUL. */
>     #define LEN(s) (sizeof (s) - 1)


> Unless the value needs to be known at compile time, using strlen(3)
> would work in all cases, save much human time at the cost of a little
> accumulated machine time, and in the simple common case of a string
> constant it will probably be evaluated by any optimising compiler into a
> constant.

I remembered this email series as I was writing some patch set for
shadow, and indeed I faced the issue of strlen(3) not being constexpr
(one of the few things I like from C++, although I haven't used it
extensively, and don't know if I'd still like it after knowing all
it's hidden corners, is constexpr functions).

So I had to write STRLEN(s) as a macro that is NITEMS(s) - 1, which
is both constexpr and safe against pointers.  :)




GPG key fingerprint: A9348594CE31283A826FBDD8D57633D441E25BB5

Attachment: OpenPGP_signature
Description: OpenPGP digital signature

reply via email to

[Prev in Thread] Current Thread [Next in Thread]