Re: Results of survey re. a new array size operator

Liste des GroupesRevenir à cl c 
Sujet : Re: Results of survey re. a new array size operator
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.c
Date : 24. Jan 2025, 21:10:10
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20250124115250.760@kylheku.com>
References : 1 2 3
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
Michael S <already5chosen@yahoo.com> writes:
On Fri, 24 Jan 2025 17:57:22 +1100
Alexis <flexibeast@gmail.com> wrote:
>
Hi all,
=20
JeanHeyd Meneide, a Project Editor for WG14, has just posted the
results of a survey re. the preferred form of a new array size
operator:
=20
"There is a clear preference for a lowercase keyword, here, though it
is not by the biggest margin. One would imagine that with the way we
keep standardizing things since C89 (starting with _Keyword and then
adding a header with a macro) that C folks would be overwhelmingly in
favor of simply continuing that style. The graph here, however, tells
a different story: while there=E2=80=99s a large contingency that clearly
hates having _Keyword by itself, it=E2=80=99s not the _Keyword + stdkeywo=
rd.h
macro that comes out on top! It=E2=80=99s just having a plain lowercase
keyword, instead."
=20
-- https://thephd.dev/the-big-array-size-survey-for-c-results
=20
=20
Alexis.
>
Majority is wrong. What's new?=20
>
Actually, the entire article is bogus.  There's no need for
some new keyword to replace the code that's been  used for
half a century to size a statically allocated array.
>
Using the phrase 'debate perverts' in an attempt to deflect
criticism pretty much eliminates authorial credibility.
>
   int arfarf[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
   return sizeof(arfarf) / sizeof(arfarf[0]);

You can define

  #define arraysize (x) (sizeof (x) / sizeof ((x)[0))

the only problem is that this bug sometimes happens:

  arraysize (ptr)

however, this can be diagnosed. A compiler can look
for occurrences of a match for the abstract syntax
tree pattern:

  (sizeof (x) / sizeof (*(x) + 0)

where x is other than an array type, and issue a warning.

$ cat > arraysize.c
#include <stdio.h>

int main(int argc, char **argv)
{
    printf("array size of argv, wrong = %zd\n",
           sizeof (argv) / sizeof (argv[0]));
    return 0;
}
$ make CFLAGS="-Wall -W -O2" arraysize
cc -Wall -W -O2    arraysize.c   -o arraysize
arraysize.c: In function 'main':
arraysize.c:6:26: warning: division 'sizeof (char **) / sizeof (char *)' does not compute the number of array elements [-Wsizeof-pointer-div]
    6 |            sizeof (argv) / sizeof (argv[0]));
      |                          ^
arraysize.c:3:27: note: first 'sizeof' operand was declared here
    3 | int main(int argc, char **argv)
      |                    ~~~~~~~^~~~
arraysize.c:3:14: warning: unused parameter 'argc' [-Wunused-parameter]
    3 | int main(int argc, char **argv)
      |          ~~~~^~~~


Thus, this is a solved problem, except for everyone reinventing
their own name for the array size macro.

It was enough of a problem for everyone reinventing their own name
for a 32 bit unsigned integer that we got uint32_t.

I'd be in favor of <sttddef.h> incorporating an arraysize
macro similar to how it has offsetof.

This macro definition would be hidden unless the compiler operator
selects a the dialect of C where that has become available,
or newer.

The standard can have a footnote encouraging implementors to
diagnose sizeof (ptr) / sizeof (ptr[0]).

It could be a constraint violation in the division operator, e.g.

  Constraints [ proposed ]
  ...
  When the left operand of / is an expression of the form sizeof expr, or
  sizeof (type), where the size obtained of a type pointer to T,
  the right operand shall not be an expression of the form sizeof expr
  or sizeof (type), where the size obtained is of a type T.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Date Sujet#  Auteur
24 Jan 25 * Results of survey re. a new array size operator36Alexis
24 Jan 25 +* Re: Results of survey re. a new array size operator10Michael S
24 Jan 25 i`* Re: Results of survey re. a new array size operator9Kaz Kylheku
25 Jan 25 i `* Re: Results of survey re. a new array size operator8Kaz Kylheku
29 Jan 25 i  `* Re: Results of survey re. a new array size operator7Tim Rentsch
29 Jan 25 i   `* Re: Results of survey re. a new array size operator6bart
29 Jan 25 i    +- Re: Results of survey re. a new array size operator1Michael S
29 Jan 25 i    +* Re: Results of survey re. a new array size operator2Richard Damon
29 Jan 25 i    i`- Re: Results of survey re. a new array size operator1Tim Rentsch
29 Jan 25 i    +- Re: Results of survey re. a new array size operator1James Kuyper
29 Jan 25 i    `- Re: Results of survey re. a new array size operator1Tim Rentsch
24 Jan 25 +* Re: Results of survey re. a new array size operator13James Kuyper
24 Jan 25 i+* Re: Results of survey re. a new array size operator5Kaz Kylheku
25 Jan 25 ii+* Re: Results of survey re. a new array size operator3James Kuyper
25 Jan 25 iii`* Re: Results of survey re. a new array size operator2Kaz Kylheku
25 Jan 25 iii `- Re: Results of survey re. a new array size operator1James Kuyper
29 Jan 25 ii`- Re: Results of survey re. a new array size operator1Tim Rentsch
25 Jan 25 i`* Re: Results of survey re. a new array size operator7Waldek Hebisch
25 Jan 25 i +- Re: Results of survey re. a new array size operator1Kaz Kylheku
25 Jan 25 i `* Re: Results of survey re. a new array size operator5James Kuyper
25 Jan 25 i  `* Re: Results of survey re. a new array size operator4Waldek Hebisch
26 Jan 25 i   `* Re: Results of survey re. a new array size operator3Keith Thompson
26 Jan 25 i    +- Re: Results of survey re. a new array size operator1Waldek Hebisch
29 Jan 25 i    `- Re: Results of survey re. a new array size operator1Tim Rentsch
24 Jan 25 +* Re: Results of survey re. a new array size operator4Kaz Kylheku
24 Jan 25 i+* Re: Results of survey re. a new array size operator2Alexis
25 Jan 25 ii`- Re: Results of survey re. a new array size operator1Kaz Kylheku
29 Jan 25 i`- Re: Results of survey re. a new array size operator1Tim Rentsch
29 Jan 25 +- Re: Results of survey re. a new array size operator1Tim Rentsch
29 Jan 25 `* Re: Results of survey re. a new array size operator7Ben Bacarisse
29 Jan 25  `* Re: Results of survey re. a new array size operator6David Brown
30 Jan 25   `* Re: Results of survey re. a new array size operator5Ben Bacarisse
30 Jan 25    +- Re: Results of survey re. a new array size operator1David Brown
30 Jan 25    `* Re: Results of survey re. a new array size operator3Tim Rentsch
30 Jan 25     +- Re: Results of survey re. a new array size operator1Kaz Kylheku
19 Feb 25     `- Re: Results of survey re. a new array size operator1Tim Rentsch

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal