Hi all,
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:
"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’s a large contingency that clearly
hates having _Keyword by itself, it’s not the _Keyword + stdkeyword.h
macro that comes out on top! It’s just having a plain lowercase
keyword, instead."
-- https://thephd.dev/the-big-array-size-survey-for-c-results
Alexis.Majority is wrong. What's new?
Hi all,
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:
"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’s a large contingency that clearly hates having
_Keyword by itself, it’s not the _Keyword + stdkeyword.h macro that
comes out on top! It’s just having a plain lowercase keyword, instead."
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
Hi all,
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:
"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’s a large contingency that clearly hates having
_Keyword by itself, it’s not the _Keyword + stdkeyword.h macro that
comes out on top! It’s just having a plain lowercase keyword, instead."
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]);
On 1/24/25 01:57, Alexis wrote:
Hi all,
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:
"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’s a large contingency that clearly hates having
_Keyword by itself, it’s not the _Keyword + stdkeyword.h macro that
comes out on top! It’s just having a plain lowercase keyword, instead."
One of the most important goals of the C standard is backwards
compatibility.
A lower case keyword would break any program that was
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 best way to have versioning for this in a C compiler is a
language dialect selection option.
On 1/24/25 01:57, Alexis wrote:
Hi all,
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:
"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’s a large contingency that clearly hates having
_Keyword by itself, it’s not the _Keyword + stdkeyword.h macro that
comes out on top! It’s just having a plain lowercase keyword, instead."
One of the most important goals of the C standard is backwards
compatibility. A lower case keyword would break any program that was
already using that keyword as a user-defined identifier.
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
You can define
#define arraysize (x) (sizeof (x) / sizeof ((x)[0))
You can, but you don't need to.
Often readability suffers
when you use macros, not to mention the other quirks of
C macro use (in C++, a constexpr function might be
suitable, but the naming being arbitrary (e.g. arraysize,
NUM_ELEMENTS, SIZE, et alia) doesn't aid in readability
or maintainability.
Kaz Kylheku <643-408-1753@kylheku.com> writes:
The best way to have versioning for this in a C compiler is a
language dialect selection option.
Indeed, the article links to a PDF of slides, "Pitch for #dialect
directive" (N3407):
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3407.pdf
Lower case _reserved word_ would break compatibility. But in most
cases there is no need to reserve a keyword: simply treat it as
predefined identifier with magic meaning. I user want gives it
different meaning, the new meaning would be used instead of
predefiend one.
On 2025-01-24, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:...
One of the most important goals of the C standard is backwards
compatibility.
Backward compatibility matters in software.
People use C compiler applications to open text documents of type C.
All that matter is that there is a way to use their old document
with the new application.
It is almost purely a software matter, not requiring anything in the specification.
It doesn't matter if the current language has a keyword "arraysize"
which breaks every program that uses it as the name of something
(goto label, struct member, variable, function ...) if
the language implementation has an option like -std=c11
under which that is not a keyword.
James Kuyper <jameskuyper@alumni.caltech.edu> wrote:...
One of the most important goals of the C standard is backwards
compatibility. A lower case keyword would break any program that was
already using that keyword as a user-defined identifier.
Lower case _reserved word_ would break compatibility. But in most
cases there is no need to reserve a keyword: simply treat it as
predefined identifier with magic meaning. I user want gives it
different meaning, the new meaning would be used instead of
predefiend one.
On 1/24/25 15:24, Kaz Kylheku wrote:
On 2025-01-24, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:...
One of the most important goals of the C standard is backwards
compatibility.
Backward compatibility matters in software.
C code is software, and so are C implementations, so I'm not sure what
that has to do with anything.
The committee has explicitly stated a
priority between those two kinds of software: "existing user code
matters; existing implementations don't".
It doesn't matter if the current language has a keyword "arraysize"
which breaks every program that uses it as the name of something
(goto label, struct member, variable, function ...) if
the language implementation has an option like -std=c11
under which that is not a keyword.
Backwards compatibility doesn't mean that you can still build your
program using an older version of the language.
It means that you can
compile your code your code without change using the newest version of
the language. You only have to make changes to make use of new features
of the language, and you should be confident that you can do so without worrying about some of the other new features breaking your existing code.
On 2025-01-25, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:...
C code is software, and so are C implementations, so I'm not sure what
that has to do with anything.
The committee has explicitly stated a
priority between those two kinds of software: "existing user code
matters; existing implementations don't".
Ensuring existing user code keeps working doesn't have to be a
responsibility of ISO C.
ISO C has not remained perfectly backward comaptible, so why
pretend to be the keepers of backward compatibility?
Backwards compatibility doesn't mean that you can still build your
program using an older version of the language.
That's not all it means, sure.
On 1/24/25 18:13, Waldek Hebisch wrote:
James Kuyper <jameskuyper@alumni.caltech.edu> wrote:...
One of the most important goals of the C standard is backwards
compatibility. A lower case keyword would break any program that was
already using that keyword as a user-defined identifier.
Lower case _reserved word_ would break compatibility. But in most
cases there is no need to reserve a keyword: simply treat it as
predefined identifier with magic meaning. I user want gives it
different meaning, the new meaning would be used instead of
predefiend one.
Unless the syntax that contains the new keyword is such that a
user-defined identifier could never occur in the same location, I don't
see how that would work.
In this particular case, countof(array) certainly does NOT qualify. It's meant to be used wherever an integer value for the length of the array
is needed. And in any such context, a user-named function returning an integer value or a user-named macro expanding to an expression with an integer value is also allowed (in some contexts, only the latter would
be a possibility).
AFAICS there is no trouble. Namely, before C23 compiler had to
handle undeclared function using "implicit int" rule.
So it had
to distingush between declartations, function calls to declared
functions and function calls to undeclared functions. The
new rule informally could be:
whenever pre-C23 compiler would use implicit int rule and the
function name is 'countof' use new semantics of 'countof'
There is some complication: Presumably 'countof' should be
applicable to types. AFAICS when parser sees 'countof' with no
prior declaration it can activate mode of allowing both
expressions and types. This should be no big burden as already
'sizeof' gets similar treatment. So parsing could go on
with minor tweak and the rest can be handled at semantic level.
AFAICS what I describe is compatible extention: it will treat
any valid C23 program as before and only assign meaning to
previously invalid programs. At it should be implementable
with modest effort in any C compiler.
Of course, it would break compatibility for C compilers that
want to allow undeclared functions as an extention to C23,
but IIUC this is not a concern to the standard.
antispam@fricas.org (Waldek Hebisch) writes:
[...]
AFAICS there is no trouble. Namely, before C23 compiler had to
handle undeclared function using "implicit int" rule.
You're off by 24 years. C99 dropped the "implicit int" rule.
So it had
to distingush between declartations, function calls to declared
functions and function calls to undeclared functions. The
new rule informally could be:
whenever pre-C23 compiler would use implicit int rule and the
function name is 'countof' use new semantics of 'countof'
It would be pre-C2Y. C23 is frozen, and "countof won't be added to it.
There is some complication: Presumably 'countof' should be
applicable to types. AFAICS when parser sees 'countof' with no
prior declaration it can activate mode of allowing both
expressions and types. This should be no big burden as already
'sizeof' gets similar treatment. So parsing could go on
with minor tweak and the rest can be handled at semantic level.
The syntax for sizeof applied to a type is different from the syntax
when applied to an expression:
sizeof unary-expression
sizeof ( type-name )
Of course a unary-expression can be a parenthesized expression, but
there's no ambiguity.
AFAICS what I describe is compatible extention: it will treat
any valid C23 program as before and only assign meaning to
previously invalid programs. At it should be implementable
with modest effort in any C compiler.
Of course, it would break compatibility for C compilers that
want to allow undeclared functions as an extention to C23,
but IIUC this is not a concern to the standard.
I personally dislike the idea of making countof a reserved identifier
rather than a keyword. It makes the rules around it more complicated,
which makes the language harder to understand. And I'm not convinced it avoids breaking old code. If the point is to allow for old code that
uses countof as an identifier, then this valid pre-C2Y code:
size_t countof(int *ptr);
int arr[10];
countof(arr);
would change its meaning (though the existing function couldn't actually determine the number of element in the array).
I understand the need for backward compatibility, but most new editions
of the C standard has broken *some* existing code by defining new
lowercase keywords, starting with inline and restrict in C99. (C11
added only _Keywords, but C23 adds 11 new lowercase keywords.)
I doubt that there's all that much existing code that uses "countof" as
an identifier, and that can be dealt with by compiling it in C23 mode or earlier.
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
You can define
#define arraysize (x) (sizeof (x) / sizeof ((x)[0))
You can, but you don't need to.
The repetition in things like:
sizeof foo->bar.buf / *sizeof foo->bar.buf
is just irksome. Why do I have to say that thing twice,
to get its number of elements?
Often readability suffers
when you use macros, not to mention the other quirks of
C macro use (in C++, a constexpr function might be
suitable, but the naming being arbitrary (e.g. arraysize,
NUM_ELEMENTS, SIZE, et alia) doesn't aid in readability
or maintainability.
The naming being arbitrary is the argument for standardizing the
name for the macro and sticking it into, for instance, <stddef.h>.
If we didn't have offsetof there, we might have to deal with
OFFSETOF, offsetof, offset, member_offset, and others.
On 2025-01-24, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
On 1/24/25 01:57, Alexis wrote:
Hi all,
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:
"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's a
large contingency that clearly hates having _Keyword by itself,
it's not the _Keyword + stdkeyword.h macro that comes out on top!
It's just having a plain lowercase keyword, instead."
One of the most important goals of the C standard is backwards
compatibility.
Backward compatibility matters in software.
People use C compiler applications to open text documents of type
C.
All that matter is that there is a way to use their old document
with the new application.
It is almost purely a software matter, not requiring anything in
the specification.
The C++ people have already figured out this out, and are running
with it (like crazy).
It doesn't matter if the current language has a keyword "arraysize"
which breaks every program that uses it as the name of something
(goto label, struct member, variable, function ...) if
the language implementation has an option like -std=c11
under which that is not a keyword.
A lower case keyword would break any program that was
That's like saying that the existence of Office 356 breaks every
Word 97 document.
That's only if Office 365 loses the ability to work with such a
document; the mere existence of the new format /per se/
perpetrates no such harm.
The problem with what I'm saying here is that it requires trust.
The people specifying the language have to abandon their grasp of
the reins of control on the compatibility issue and trust that the implementors will handle it in good ways for the benefit of their
users.
The people specifying the language also have to accept that
the backward compatibility mechanism is not only out of their
control, but that it has implementation-specific manifestations:
the means by which an implementation is instructed to obey an
older dialect isn't specified in the standard because they have
decided that the manner of presenting a program for processing
by an implementation is out of the Scope.
Even if it were something that were somehow brought within the
Scope, the standard couldn't go as far as to give a requirement
like "a conforming impelmentation shall provide configurations
for accepting programs in the following historic dialects of C:
[...]" You just can't do that.
On 2025-01-24, Alexis <flexibeast@gmail.com> wrote:
Hi all,
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:
"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's a large
contingency that clearly hates having _Keyword by itself, it's not
the _Keyword + stdkeyword.h macro that comes out on top! It's just
having a plain lowercase keyword, instead."
The best way to have versioning for this in a C compiler is a
language dialect selection option. [...]
Hi all,
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: [...]
I understand the need for backward compatibility, but most new
editions of the C standard has broken *some* existing code by
defining new lowercase keywords, starting with inline and restrict
in C99. (C11 added only _Keywords, but C23 adds 11 new lowercase
keywords.)
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
You can define
#define arraysize (x) (sizeof (x) / sizeof ((x)[0))
You can, but you don't need to.
The repetition in things like:
sizeof foo->bar.buf / *sizeof foo->bar.buf
is just irksome. Why do I have to say that thing twice,
to get its number of elements?
Often readability suffers
when you use macros, not to mention the other quirks of
C macro use (in C++, a constexpr function might be
suitable, but the naming being arbitrary (e.g. arraysize,
NUM_ELEMENTS, SIZE, et alia) doesn't aid in readability
or maintainability.
The naming being arbitrary is the argument for standardizing the
name for the macro and sticking it into, for instance, <stddef.h>.
If we didn't have offsetof there, we might have to deal with
OFFSETOF, offsetof, offset, member_offset, and others.
That's a flawed analogy. A macro to compute the number of
elements in an array can be done in standard C. The
functionality of offsetof cannot be done in standard C, and
that's what it needs to be in the standard library.
On 29/01/2025 09:48, Tim Rentsch wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
You can define
#define arraysize (x) (sizeof (x) / sizeof ((x)[0))
You can, but you don't need to.
The repetition in things like:
sizeof foo->bar.buf / *sizeof foo->bar.buf
is just irksome. Why do I have to say that thing twice,
to get its number of elements?
Often readability suffers
when you use macros, not to mention the other quirks of
C macro use (in C++, a constexpr function might be
suitable, but the naming being arbitrary (e.g. arraysize,
NUM_ELEMENTS, SIZE, et alia) doesn't aid in readability
or maintainability.
The naming being arbitrary is the argument for standardizing the
name for the macro and sticking it into, for instance, <stddef.h>.
If we didn't have offsetof there, we might have to deal with
OFFSETOF, offsetof, offset, member_offset, and others.
That's a flawed analogy. A macro to compute the number of
elements in an array can be done in standard C. The
functionality of offsetof cannot be done in standard C, and
that's what it needs to be in the standard library.
Can't it? The various versions I've seen, including mine, look like
this:
#define offsetof(a,b) (size_t) &( ((a*)0) -> b)
On 29/01/2025 09:48, Tim Rentsch wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
You can define
#define arraysize (x) (sizeof (x) / sizeof ((x)[0))
You can, but you don't need to.
The repetition in things like:
sizeof foo->bar.buf / *sizeof foo->bar.buf
is just irksome. Why do I have to say that thing twice,
to get its number of elements?
Often readability suffers
when you use macros, not to mention the other quirks of
C macro use (in C++, a constexpr function might be
suitable, but the naming being arbitrary (e.g. arraysize,
NUM_ELEMENTS, SIZE, et alia) doesn't aid in readability
or maintainability.
The naming being arbitrary is the argument for standardizing the
name for the macro and sticking it into, for instance, <stddef.h>.
If we didn't have offsetof there, we might have to deal with
OFFSETOF, offsetof, offset, member_offset, and others.
That's a flawed analogy. A macro to compute the number of
elements in an array can be done in standard C. The
functionality of offsetof cannot be done in standard C, and
that's what it needs to be in the standard library.
Can't it? The various versions I've seen, including mine, look like this:
#define offsetof(a,b) (size_t) &( ((a*)0) -> b)
As for the other point that was made, when looking at open source code, every other program seems to contain macros like
MAX
ARRAYLEN
STREQ
But with assorted spellings (the first program I looked at today used MZ_MAX).
However, every other program also seems to use typedefs to define their
own versions of INT32 etc, even with stdint.h type being available for
25 years.
So being in the standard is not enough if the official name is too ugly
or it is fiddly to type.
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:
-- https://thephd.dev/the-big-array-size-survey-for-c-results
On 1/29/25 6:45 AM, bart wrote:
On 29/01/2025 09:48, Tim Rentsch wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
You can define
#define arraysize (x) (sizeof (x) / sizeof ((x)[0))
You can, but you don't need to.
The repetition in things like:
sizeof foo->bar.buf / *sizeof foo->bar.buf
is just irksome. Why do I have to say that thing twice,
to get its number of elements?
Often readability suffers
when you use macros, not to mention the other quirks of
C macro use (in C++, a constexpr function might be
suitable, but the naming being arbitrary (e.g. arraysize,
NUM_ELEMENTS, SIZE, et alia) doesn't aid in readability
or maintainability.
The naming being arbitrary is the argument for standardizing the
name for the macro and sticking it into, for instance, <stddef.h>.
If we didn't have offsetof there, we might have to deal with
OFFSETOF, offsetof, offset, member_offset, and others.
That's a flawed analogy. A macro to compute the number of
elements in an array can be done in standard C. The
functionality of offsetof cannot be done in standard C, and
that's what it needs to be in the standard library.
Can't it? The various versions I've seen, including mine, look
like this:
#define offsetof(a,b) (size_t) &( ((a*)0) -> b)
Which has undefined behavior, the deferencing of a null pointer.
Only if the implementation defines that behavior to be what we want,
can that be done. Most implementtions, that sort of behavior turns
out to work out, but it isn't mandated by the Standard.
On 29/01/2025 09:48, Tim Rentsch wrote:...
That's a flawed analogy. A macro to compute the number of
elements in an array can be done in standard C. The
functionality of offsetof cannot be done in standard C, and
that's what it needs to be in the standard library.
Can't it? The various versions I've seen, including mine, look like
this:
#define offsetof(a,b) (size_t) &( ((a*)0) -> b)
On 29/01/2025 09:48, Tim Rentsch wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
Kaz Kylheku <643-408-1753@kylheku.com> writes:
On 2025-01-24, Scott Lurndal <scott@slp53.sl.home> wrote:
You can define
#define arraysize (x) (sizeof (x) / sizeof ((x)[0))
You can, but you don't need to.
The repetition in things like:
sizeof foo->bar.buf / *sizeof foo->bar.buf
is just irksome. Why do I have to say that thing twice,
to get its number of elements?
Often readability suffers
when you use macros, not to mention the other quirks of
C macro use (in C++, a constexpr function might be
suitable, but the naming being arbitrary (e.g. arraysize,
NUM_ELEMENTS, SIZE, et alia) doesn't aid in readability
or maintainability.
The naming being arbitrary is the argument for standardizing the
name for the macro and sticking it into, for instance, <stddef.h>.
If we didn't have offsetof there, we might have to deal with
OFFSETOF, offsetof, offset, member_offset, and others.
That's a flawed analogy. A macro to compute the number of
elements in an array can be done in standard C. The
functionality of offsetof cannot be done in standard C, and
that's what it needs to be in the standard library.
Can't it? The various versions I've seen, including mine, look
like this:
#define offsetof(a,b) (size_t) &( ((a*)0) -> b)
As for the other point that was made, when looking at open source
code, every other program seems to contain macros like
MAX
ARRAYLEN
STREQ
But with assorted spellings (the first program I looked at today used MZ_MAX).
However, every other program also seems to use typedefs to define
their own versions of INT32 etc, even with stdint.h type being
available for 25 years.
So being in the standard is not enough if the official name is
too ugly or it is fiddly to type.
Alexis <flexibeast@gmail.com> writes:
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:
-- https://thephd.dev/the-big-array-size-survey-for-c-results
Curious. The top objection to the usual macro solution is given as:
* double-evaluation of e.g. getting the size of the 1-d part of a 2-d
array int meow[3][4]; /* ... */ SIZE_KEYWORD(meow[first_idx()]);
Does the author not know that there is no evaluation of the operands of sizeof in this example?
His "About" pages says "Project Editor for ISO/IEC JTC1 SC22 WG14 - Programming Languages, C".
On 29/01/2025 17:00, Ben Bacarisse wrote:
Alexis <flexibeast@gmail.com> writes:
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:Curious. The top objection to the usual macro solution is given as:
-- https://thephd.dev/the-big-array-size-survey-for-c-results
* double-evaluation of e.g. getting the size of the 1-d part of a 2-d
array int meow[3][4]; /* ... */ SIZE_KEYWORD(meow[first_idx()]);
Does the author not know that there is no evaluation of the operands of
sizeof in this example?
6.5.3.4p2 :
"""
If the type of the operand is a variable length array type, the operand is evaluated; otherwise, the operand is not evaluated and the result is an integer constant.
"""
I don't know if that is the source of the double-evaluation concern here,
but it is certainly a situation in which sizeof /does/ evaluate its
operand.
David Brown <david.brown@hesbynett.no> writes:
On 29/01/2025 17:00, Ben Bacarisse wrote:
Alexis <flexibeast@gmail.com> writes:
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:Curious. The top objection to the usual macro solution is given as:
-- https://thephd.dev/the-big-array-size-survey-for-c-results
* double-evaluation of e.g. getting the size of the 1-d part of a 2-d >>> array int meow[3][4]; /* ... */ SIZE_KEYWORD(meow[first_idx()]);
Does the author not know that there is no evaluation of the operands of
sizeof in this example?
6.5.3.4p2 :
"""
If the type of the operand is a variable length array type, the operand is >> evaluated; otherwise, the operand is not evaluated and the result is an
integer constant.
"""
I don't know if that is the source of the double-evaluation concern here,
but it is certainly a situation in which sizeof /does/ evaluate its
operand.
It would have been a good idea to pick an example that behaves as
claimed. Let's hope this sort of casual approach is reserved for blogs.
David Brown <david.brown@hesbynett.no> writes:
On 29/01/2025 17:00, Ben Bacarisse wrote:
Alexis <flexibeast@gmail.com> writes:
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:
-- https://thephd.dev/the-big-array-size-survey-for-c-results
Curious. The top objection to the usual macro solution is given
as:
* double-evaluation of e.g. getting the size of the 1-d part of
a 2-d array
int meow[3][4]; /* ... */ SIZE_KEYWORD(meow[first_idx()]);
Does the author not know that there is no evaluation of the
operands of sizeof in this example?
6.5.3.4p2 :
"""
If the type of the operand is a variable length array type, the
operand is evaluated; otherwise, the operand is not evaluated and
the result is an integer constant.
"""
I don't know if that is the source of the double-evaluation concern
here, but it is certainly a situation in which sizeof /does/
evaluate its operand.
It would have been a good idea to pick an example that behaves as
claimed. Let's hope this sort of casual approach is reserved for
blogs.
Ben Bacarisse <ben@bsb.me.uk> writes:
David Brown <david.brown@hesbynett.no> writes:
On 29/01/2025 17:00, Ben Bacarisse wrote:
Alexis <flexibeast@gmail.com> writes:
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:
-- https://thephd.dev/the-big-array-size-survey-for-c-results
Curious. The top objection to the usual macro solution is given
as:
* double-evaluation of e.g. getting the size of the 1-d part of
a 2-d array
int meow[3][4]; /* ... */ SIZE_KEYWORD(meow[first_idx()]);
Does the author not know that there is no evaluation of the
operands of sizeof in this example?
6.5.3.4p2 :
"""
If the type of the operand is a variable length array type, the
operand is evaluated; otherwise, the operand is not evaluated and
the result is an integer constant.
"""
I don't know if that is the source of the double-evaluation concern
here, but it is certainly a situation in which sizeof /does/
evaluate its operand.
It would have been a good idea to pick an example that behaves as
claimed. Let's hope this sort of casual approach is reserved for
blogs.
All of the motivational examples listed are lame. Everyone knows
that macro calls might evaluate an argument twice, and so to avoid
calling macros on expressions with side-effects. It's true that
the usual macro definition to determine array extent misbehaves
but that can simply be called out as a warning without needing to
codify the situation by putting it in the C standard; in other
words it's a quality of implementation issue, not a language issue
(and some cases are already diagnosed by both gcc and clang). As
for the problem of name collision, choosing a longer name and
having it be all caps (as most macro names should be) gives a
collision cross section that is vanishingly small. Either of the
names ARRAY_INDEX_LIMIT() or ARRAY_EXTENT() are both descriptive
enough and unlikely-to-collide enough that they can be used
without any significant danger of collision.
What would be better is to give some general tools that would
allow a user-defined macro to be written safely. For example:
_Has_array_type( e ) 1 if and only if the expression
'e' has array type
_Is_side_effect_free( e ) 1 if and only if the expression
'e' has no side effects, so
multiple evaluations have no
negative consequences
Furthermore because these tests are likely to be called only
inside macro definitions, using the _Leading_capital style of
naming shouldn't be a problem.
Sysop: | DaiTengu |
---|---|
Location: | Appleton, WI |
Users: | 1,007 |
Nodes: | 10 (0 / 10) |
Uptime: | 196:36:08 |
Calls: | 13,143 |
Files: | 186,574 |
D/L today: |
257 files (65,603K bytes) |
Messages: | 3,310,127 |