Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

If you have a procedure with 10 parameters, you probably missed some.


devel / comp.lang.c / Re: array arguments passed as reference

SubjectAuthor
* array arguments passed as referenceThiago Adams
`* Re: array arguments passed as referenceKeith Thompson
 `* Re: array arguments passed as referenceThiago Adams
  `* Re: array arguments passed as referenceDavid Brown
   `* Re: array arguments passed as referenceKaz Kylheku
    +- Re: array arguments passed as referenceBlue-Maned_Hawk
    `- Re: array arguments passed as referenceDavid Brown

1
Re: array arguments passed as reference

<ec6a0716-59d4-4619-9837-0aec6cc330fcn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25437&group=comp.lang.c#25437

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:129b:b0:74a:db09:d2ef with SMTP id w27-20020a05620a129b00b0074adb09d2efmr1581369qki.8.1681589402261;
Sat, 15 Apr 2023 13:10:02 -0700 (PDT)
X-Received: by 2002:a05:622a:493:b0:3ed:330f:5d67 with SMTP id
p19-20020a05622a049300b003ed330f5d67mr615284qtx.1.1681589401972; Sat, 15 Apr
2023 13:10:01 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 15 Apr 2023 13:10:01 -0700 (PDT)
In-Reply-To: <87o7npdmyn.fsf@nosuchdomain.example.com>
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.251.163; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.251.163
References: <b2945d3f-8799-4a3d-86e7-9f841ddfe32cn@googlegroups.com> <87o7npdmyn.fsf@nosuchdomain.example.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <ec6a0716-59d4-4619-9837-0aec6cc330fcn@googlegroups.com>
Subject: Re: array arguments passed as reference
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Sat, 15 Apr 2023 20:10:02 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
 by: Thiago Adams - Sat, 15 Apr 2023 20:10 UTC

On Saturday, April 15, 2023 at 3:23:12 PM UTC-3, Keith Thompson wrote:
> Thiago Adams <thiago...@gmail.com> writes:
> > Arrays passed as function arguments are pointers in C.
> >
> > For instance, this is valid code
> >
> > void F(int a[2]) {
> > typeof(a) p;
> > static_assert(_Generic(p, int * : 1, default: 0));
> > static_assert(_Generic(a, int * : 1, default: 0));
> > static_assert(sizeof(a) == sizeof(int*));
> > a = 0;
> > a++;
> > a--;
> > }
> Not relevant to your main point, but `a = 0;` makes a null pointer;
> then `a++` has undefined behavior. (I can't persuade gcc to warn
> about that.)
>
yes!

More warnings I would like to have

void F(int a[2]) {
if (a) { } //warning
} because if a should be a reference to something..otherwise better to use pointer.

array arguments passed as reference

<b2945d3f-8799-4a3d-86e7-9f841ddfe32cn@googlegroups.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25438&group=comp.lang.c#25438

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ae9:c10c:0:b0:748:60ff:dff0 with SMTP id z12-20020ae9c10c000000b0074860ffdff0mr1789981qki.8.1681581604693;
Sat, 15 Apr 2023 11:00:04 -0700 (PDT)
X-Received: by 2002:a05:622a:2512:b0:3e3:876e:d7dc with SMTP id
cm18-20020a05622a251200b003e3876ed7dcmr3610615qtb.6.1681581604341; Sat, 15
Apr 2023 11:00:04 -0700 (PDT)
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!proxad.net!feeder1-2.proxad.net!209.85.160.216.MISMATCH!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 15 Apr 2023 11:00:04 -0700 (PDT)
Injection-Info: google-groups.googlegroups.com; posting-host=189.6.251.163; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 189.6.251.163
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <b2945d3f-8799-4a3d-86e7-9f841ddfe32cn@googlegroups.com>
Subject: array arguments passed as reference
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Sat, 15 Apr 2023 18:00:04 +0000
Content-Type: text/plain; charset="UTF-8"
 by: Thiago Adams - Sat, 15 Apr 2023 18:00 UTC

Arrays passed as function arguments are pointers in C.

For instance, this is valid code

void F(int a[2]) {
typeof(a) p;
static_assert(_Generic(p, int * : 1, default: 0));
static_assert(_Generic(a, int * : 1, default: 0));
static_assert(sizeof(a) == sizeof(int*));
a = 0;
a++;
a--;
}

One thing we can do is:
void F(int a[const 2]) then a = 0; a++; a--; etc will not compile.

What I suggest is just deprecate with warning all of this
(and what I will implement in cake)

void F(int a[2]) {

//warning use typeof(&a[0]) instead
static_assert(_Generic(a, int * : 1, default: 0));

//warning use sizeof(&a[0]) instead
static_assert(sizeof(a) == sizeof(int*));

a = 0; //warning deprecated and may be removed
a++; //warning deprecated and may be removed
a--; //warning deprecated and may be removed
}

Does it make sense for you?

In 20 years we can make sizeof and typeof be array type.

Re: array arguments passed as reference

<87o7npdmyn.fsf@nosuchdomain.example.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25439&group=comp.lang.c#25439

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: array arguments passed as reference
Date: Sat, 15 Apr 2023 11:22:56 -0700
Organization: None to speak of
Lines: 25
Message-ID: <87o7npdmyn.fsf@nosuchdomain.example.com>
References: <b2945d3f-8799-4a3d-86e7-9f841ddfe32cn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="ae06fccdd2df4d3dc58079c3c63f005b";
logging-data="2250707"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/CAt7s+BbtFgGZfZqTH/6I"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:igxd2djl5g3+srGK2lZEbU/H3D0=
sha1:Mgc6jegBCzY8U0Unjc8wKf/4ZuQ=
 by: Keith Thompson - Sat, 15 Apr 2023 18:22 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
> Arrays passed as function arguments are pointers in C.
>
> For instance, this is valid code
>
> void F(int a[2]) {
> typeof(a) p;
> static_assert(_Generic(p, int * : 1, default: 0));
> static_assert(_Generic(a, int * : 1, default: 0));
> static_assert(sizeof(a) == sizeof(int*));
> a = 0;
> a++;
> a--;
> }

Not relevant to your main point, but `a = 0;` makes a null pointer;
then `a++` has undefined behavior. (I can't persuade gcc to warn
about that.)

[...]

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Working, but not speaking, for XCOM Labs
void Void(void) { Void(); } /* The recursive call of the void */

Re: array arguments passed as reference

<u1g7u1$2f5f6$1@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25440&group=comp.lang.c#25440

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: array arguments passed as reference
Date: Sun, 16 Apr 2023 09:25:52 +0200
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <u1g7u1$2f5f6$1@dont-email.me>
References: <b2945d3f-8799-4a3d-86e7-9f841ddfe32cn@googlegroups.com>
<87o7npdmyn.fsf@nosuchdomain.example.com>
<ec6a0716-59d4-4619-9837-0aec6cc330fcn@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 16 Apr 2023 07:25:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="985ceb6503b5b11b4fa0d50fa9e47bee";
logging-data="2594278"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+MnifglvWa8MizOThLnNhiMcOBG1sR2Zg="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.7.1
Cancel-Lock: sha1:TsgQI7jouvk6ZfbVqLkLvyKVOOk=
Content-Language: en-GB
In-Reply-To: <ec6a0716-59d4-4619-9837-0aec6cc330fcn@googlegroups.com>
 by: David Brown - Sun, 16 Apr 2023 07:25 UTC

On 15/04/2023 22:10, Thiago Adams wrote:
> On Saturday, April 15, 2023 at 3:23:12 PM UTC-3, Keith Thompson wrote:
>> Thiago Adams <thiago...@gmail.com> writes:
>>> Arrays passed as function arguments are pointers in C.
>>>
>>> For instance, this is valid code
>>>
>>> void F(int a[2]) {
>>> typeof(a) p;
>>> static_assert(_Generic(p, int * : 1, default: 0));
>>> static_assert(_Generic(a, int * : 1, default: 0));
>>> static_assert(sizeof(a) == sizeof(int*));
>>> a = 0;
>>> a++;
>>> a--;
>>> }
>> Not relevant to your main point, but `a = 0;` makes a null pointer;
>> then `a++` has undefined behavior. (I can't persuade gcc to warn
>> about that.)
>>
> yes!
>
> More warnings I would like to have
>
> void F(int a[2]) {
> if (a) { } //warning
> }
> because if a should be a reference to something..otherwise better to use pointer.
>

It may be /better/ to use a pointer argument when it should never be 0,
but the standard is clear that "F(int * a)" and "F(int a[2])" are
identical. The C way to indicate that "a" cannot be a null pointer is
the ugly and unintuitive "F(int a[static 2])" (or whatever size you
want). (There really should be a "[[notnull]]" attribute.)

gcc still doesn't put a warning in your code here if you declare "void
F(int a [static 2])", but at least it will warn if you try to call F
with a null pointer.

Incidentally, on many systems "sizeof(int[2])" always equals
"sizeof(int*)", making your third static assertion a bit pointless. It
would make more sense to use a bigger array, rather than just 2 int's.

Re: array arguments passed as reference

<20230416142723.519@kylheku.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25443&group=comp.lang.c#25443

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: array arguments passed as reference
Date: Sun, 16 Apr 2023 21:33:36 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <20230416142723.519@kylheku.com>
References: <b2945d3f-8799-4a3d-86e7-9f841ddfe32cn@googlegroups.com>
<87o7npdmyn.fsf@nosuchdomain.example.com>
<ec6a0716-59d4-4619-9837-0aec6cc330fcn@googlegroups.com>
<u1g7u1$2f5f6$1@dont-email.me>
Injection-Date: Sun, 16 Apr 2023 21:33:36 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="dc80bee735e7e2f02a4fb8e425ead72e";
logging-data="2839460"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18miqo8WeBOVPOWs7HK5G5YOvIcaqFvfok="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:1Q9GnA8oLrdvqgh4ut8Rb0rOIro=
 by: Kaz Kylheku - Sun, 16 Apr 2023 21:33 UTC

On 2023-04-16, David Brown <david.brown@hesbynett.no> wrote:
> It may be /better/ to use a pointer argument when it should never be 0,
> but the standard is clear that "F(int * a)" and "F(int a[2])" are
> identical. The C way to indicate that "a" cannot be a null pointer is
> the ugly and unintuitive "F(int a[static 2])" (or whatever size you
> want). (There really should be a "[[notnull]]" attribute.)

There is in the GCC function attribute syntax:

All pointer args non-null:

void fun(char *str, int *array, size_t) __attribute__((nonnull));

Only the second:

void fun(char *str, int *array, size_t) __attribute__((nonnull, 2));

Use of attributes common in ecosystems built on GCC or Clang.

THus, if/when this appears in the standard language, we can expect it to
be gratuitously different, just for shits and giggles. Or, more
cynically, for the sake of spiting the existing users of the extension.

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

Re: array arguments passed as reference

<u1i59o$2o85f$1@bluemanedhawk.eternal-september.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25444&group=comp.lang.c#25444

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!bluemanedhawk.eternal-september.org!.POSTED!not-for-mail
From: bluemane...@gmail.com (Blue-Maned_Hawk)
Newsgroups: comp.lang.c
Subject: Re: array arguments passed as reference
Date: Sun, 16 Apr 2023 20:53:09 -0400
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <u1i59o$2o85f$1@bluemanedhawk.eternal-september.org>
References: <b2945d3f-8799-4a3d-86e7-9f841ddfe32cn@googlegroups.com>
<87o7npdmyn.fsf@nosuchdomain.example.com>
<ec6a0716-59d4-4619-9837-0aec6cc330fcn@googlegroups.com>
<u1g7u1$2f5f6$1@dont-email.me> <20230416142723.519@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: base64
Injection-Date: Mon, 17 Apr 2023 00:53:13 -0000 (UTC)
Injection-Info: bluemanedhawk.eternal-september.org; posting-host="f57e34506a21a18c38e6b17980111711";
logging-data="2891951"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/pfMT5WN13fC8eJJ20IPOSdC8h2uDkVM8="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.10.0
Cancel-Lock: sha1:hZpPijDM5nQ9/LnyM15dnS7fSvc=
In-Reply-To: <20230416142723.519@kylheku.com>
Content-Language: en-US
 by: Blue-Maned_Hawk - Mon, 17 Apr 2023 00:53 UTC

On 4/16/23 17:33, Kaz Kylheku wrote:
> There is in the GCC function attribute syntax:
>
> All pointer args non-null:
>
> void fun(char *str, int *array, size_t) __attribute__((nonnull));
>
> Only the second:
>
> void fun(char *str, int *array, size_t) __attribute__((nonnull(2)));
>
> Use of attributes common in ecosystems built on GCC or Clang.
>
> THus, if/when this appears in the standard language, we can expect it to
> be gratuitously different, just for shits and giggles. Or, more
> cynically, for the sake of spiting the existing users of the extension.
>
​You can also use the standard double-brackets syntax with the attribute
in the gnu:: namespace, e.g.
void fun(char *str, int *array, size_t) [[gnu::nunnull(2)]];
, which will also mean that it will still be accepted (if not
recognized) by other C23 compilers, and it will relieve the issue of
migration to a standard attribute should it be added.
Clang (but not gcc) also allows the potentially-less-confusing syntax of
marking the _arguments_ as nonnull (though confusingly, it still uses
the gnu:: prefix…):
void fun(char *str, int *array [[gnu::nonnull]], size_t);
…and there's also the returns_nonnull attribute:
void *my_malloc(size_t) [[gnu::returns_nonnull]];
/* now, instead of this: */
void *x;
if ((x = my_malloc(0xFF)) != NULL)
fun(str, x, 0xFF);
else
unreachable();
/* you can just do this: */
fun(str, my_malloc(0xFF), 0xFF);
Oh, but of course, it's not a C extension without two completely
different ways to do the same thing, because there's also the _Nonnull
type qualifier.
--
⚗︎ | /blu.mɛin.dʰak/ | shortens to "Hawk" | he/him/his/himself/Mr.
bluemanedhawk.github.io
Bitches stole my whole ass ␔🭖᷿᪳𝼗᷍⏧𒒫𐻾ࣛ↉�⃣ quoted-printable, can't
have shit in Thunderbird 😩

Re: array arguments passed as reference

<u1itn0$2v00b$2@dont-email.me>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=25446&group=comp.lang.c#25446

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: array arguments passed as reference
Date: Mon, 17 Apr 2023 09:49:52 +0200
Organization: A noiseless patient Spider
Lines: 45
Message-ID: <u1itn0$2v00b$2@dont-email.me>
References: <b2945d3f-8799-4a3d-86e7-9f841ddfe32cn@googlegroups.com>
<87o7npdmyn.fsf@nosuchdomain.example.com>
<ec6a0716-59d4-4619-9837-0aec6cc330fcn@googlegroups.com>
<u1g7u1$2f5f6$1@dont-email.me> <20230416142723.519@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 17 Apr 2023 07:49:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="ded965c87e311b25549d463817a4bbea";
logging-data="3112971"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19mJaURVCCvtWqhtFdhC6UTfRFBuv0muGE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.9.0
Cancel-Lock: sha1:ADh1HGG+SC+tc/j5lnc24cmBSLM=
In-Reply-To: <20230416142723.519@kylheku.com>
Content-Language: en-GB
 by: David Brown - Mon, 17 Apr 2023 07:49 UTC

On 16/04/2023 23:33, Kaz Kylheku wrote:
> On 2023-04-16, David Brown <david.brown@hesbynett.no> wrote:
>> It may be /better/ to use a pointer argument when it should never be 0,
>> but the standard is clear that "F(int * a)" and "F(int a[2])" are
>> identical. The C way to indicate that "a" cannot be a null pointer is
>> the ugly and unintuitive "F(int a[static 2])" (or whatever size you
>> want). (There really should be a "[[notnull]]" attribute.)
>
> There is in the GCC function attribute syntax:
>
> All pointer args non-null:
>
> void fun(char *str, int *array, size_t) __attribute__((nonnull));
>
> Only the second:
>
> void fun(char *str, int *array, size_t) __attribute__((nonnull, 2));
>

Sure - but I think the OP was interested in standard C.

> Use of attributes common in ecosystems built on GCC or Clang.
>
> THus, if/when this appears in the standard language, we can expect it to
> be gratuitously different, just for shits and giggles. Or, more
> cynically, for the sake of spiting the existing users of the extension.
>

The standards committee takes inspiration from existing extensions, but
they have to fit them in a wider context. Sometimes gcc (or clang)
extensions are rather weakly specified, or tightly tied to other details
of the compiler, or fail to consider all the edge cases. And often the
choices made by gcc developers decades ago are not ideal in modern
times. And unlike the gcc developers, the standards committee has to
take into account that there are other compilers around - there will be
many compilers that have some way of marking a pointer as non-nullable.

In this case, I'd far rather see an attribute that works more like
"restrict" -

void fun(char * str, int * [[nonnull]] array, size_t);

That would be incompatible with the gcc extension but much better.

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor