Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

You cannot have a science without measurement. -- R. W. Hamming


devel / comp.lang.c / Re: Dymamic arrays: memory management and naming

SubjectAuthor
* Dymamic arrays: memory management and namingAnton Shepelev
+- Re: Dymamic arrays: memory management and namingMalcolm McLean
+- Re: Dymamic arrays: memory management and namingKaz Kylheku
+- Re: Dymamic arrays: memory management and namingScott Lurndal
+* Re: Dymamic arrays: memory management and namingSpiros Bousbouras
|`* Re: Dymamic arrays: memory management and namingAnton Shepelev
| `- Re: Dymamic arrays: memory management and namingKaz Kylheku
+* Re: Dymamic arrays: memory management and namingTim Rentsch
|+- Re: Dymamic arrays: memory management and namingAnton Shepelev
|`- Re: Dymamic arrays: memory management and namingAnton Shepelev
`* Re: Dymamic arrays: memory management and namingTim Rentsch
 `* Re: Dymamic arrays: memory management and namingAnton Shepelev
  +- Re: Dymamic arrays: memory management and namingTim Rentsch
  `* Re: Dymamic arrays: memory management and namingTim Rentsch
   +* Re: Dymamic arrays: memory management and namingKeith Thompson
   |+- Re: Dymamic arrays: memory management and namingChris M. Thomasson
   |+* Re: Dymamic arrays: memory management and namingKaz Kylheku
   ||`* Re: Dymamic arrays: memory management and namingTim Rentsch
   || +* Re: Dymamic arrays: memory management and namingKeith Thompson
   || |`- Re: Dymamic arrays: memory management and namingTim Rentsch
   || `* Re: Dymamic arrays: memory management and namingKaz Kylheku
   ||  `* Re: Dymamic arrays: memory management and namingTim Rentsch
   ||   `- Re: Dymamic arrays: memory management and namingKaz Kylheku
   |`- Re: Dymamic arrays: memory management and namingTim Rentsch
   `* Re: Dymamic arrays: memory management and namingAnton Shepelev
    +- Re: Dymamic arrays: memory management and namingBlue-Maned_Hawk
    +- Re: Dymamic arrays: memory management and namingJames Kuyper
    +* Re: Dymamic arrays: memory management and namingSpiros Bousbouras
    |+* Re: Dymamic arrays: memory management and namingKaz Kylheku
    ||+* Re: Dymamic arrays: memory management and namingTim Rentsch
    |||`* Re: Dymamic arrays: memory management and namingKaz Kylheku
    ||| `- Re: Dymamic arrays: memory management and namingTim Rentsch
    ||`* Re: Dymamic arrays: memory management and namingKaz Kylheku
    || `* Re: Dymamic arrays: memory management and namingTim Rentsch
    ||  +* Re: Dymamic arrays: memory management and namingKaz Kylheku
    ||  |`- Re: Dymamic arrays: memory management and namingTim Rentsch
    ||  `* Re: Dymamic arrays: memory management and namingKeith Thompson
    ||   `* Re: Dymamic arrays: memory management and namingTim Rentsch
    ||    `* Re: Dymamic arrays: memory management and namingKeith Thompson
    ||     `- Re: Dymamic arrays: memory management and namingTim Rentsch
    |+- Re: Dymamic arrays: memory management and namingKaz Kylheku
    |+* Re: Dymamic arrays: memory management and namingJames Kuyper
    ||`* Re: Dymamic arrays: memory management and namingKaz Kylheku
    || `- Re: Dymamic arrays: memory management and namingJames Kuyper
    |`* Re: Dymamic arrays: memory management and namingPhil Carmody
    | `- Re: Dymamic arrays: memory management and namingSpiros Bousbouras
    `* Re: Dymamic arrays: memory management and namingTim Rentsch
     `* Re: Dymamic arrays: memory management and namingKaz Kylheku
      `* Re: Dymamic arrays: memory management and namingTim Rentsch
       +- Re: Dymamic arrays: memory management and namingKaz Kylheku
       `- Re: Dymamic arrays: memory management and namingKaz Kylheku

Pages:123
Re: Dymamic arrays: memory management and naming

<20230924081911.947@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Sun, 24 Sep 2023 16:08:46 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 46
Message-ID: <20230924081911.947@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co>
Injection-Date: Sun, 24 Sep 2023 16:08:46 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de7138e0de32fc51e80c05ed2d00fab7";
logging-data="1504205"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19MPU6S5dP8ahcdU7Lzc9dRwc3ItJq202k="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:unDwF2LZP8WzEjlBlVYZqpn4JOo=
 by: Kaz Kylheku - Sun, 24 Sep 2023 16:08 UTC

On 2023-09-24, Spiros Bousbouras <spibou@gmail.com> wrote:
> On Sun, 24 Sep 2023 00:39:15 +0300
> Anton Shepelev <anton.txt@gmail.moc> wrote:
>> Futhermore, some systems seem not to require alignment,
>> e.g.:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> void print_as( char const * data )
>> { for( int i = 0; i < 2; i += 1 )
>> { printf( "%i ", *((short*)(data+i*2)) ); }
>
> %hi would be more precise than %i .At the very least it saves the reader
> the trouble of trying to collect from the various points in the standard
> the information to decide whether just %i is also guaranteed to work.

People reviewing C code should know the utter basics, like that short
promotes to int. This is not an unreasonable expectation.

If the code creates an opportunity to get a refresher in a forgotten
matter in a fundamental language area, that's a good thing.

There is no reason to use a the h modifier with the signed conversion
specifiers d and i, if the argument is (before promotion) of type
short. In that situation it obviously does nothing.

All that the h does is insert an downcast. %hi is like %i except
that internally it convert the value to short, which can be imagined to
occur by a (short) cast.

That either does nothing (the value is in range of short due to having
been promoted out of short) or else produces an implementation-defined
result. Why bring that into the picture? That could become a problem
if the code is changed, and someone forgets to remove the h.

h only makes sense if you're passing a signed argument and want it
printed as unsigned. Say we have 16 bit short and 32 bit int.
Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
will obtain the desired 65535.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<20230924090859.714@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Sun, 24 Sep 2023 16:11:53 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <20230924090859.714@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co>
Injection-Date: Sun, 24 Sep 2023 16:11:53 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de7138e0de32fc51e80c05ed2d00fab7";
logging-data="1504205"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/8mpFYXRlO+vzRUjVOl3AQuBof/uFXJOU="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:YwYdArAGgVXhX9biu2NuvzP96c0=
 by: Kaz Kylheku - Sun, 24 Sep 2023 16:11 UTC

On 2023-09-24, Spiros Bousbouras <spibou@gmail.com> wrote:
>> { data[i] = (char)i%2; }
>
> (char)i will be promoted to something else (most likely int) before the
> % operation takes place so the cast is pointless. Even if you had written
> (char)(i%2) , given the values involved I don't see the point.

It has exactly the same point as your suggestion to use %hi instead of %i.

It feels more precise, by following an imaginary type system in which
imprecise features like promotions don't exist.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<uepo37$1du2j$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Sun, 24 Sep 2023 12:28:23 -0400
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <uepo37$1du2j$2@dont-email.me>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 16:28:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="02f9defeecc8712da321e422f8c6bcdf";
logging-data="1505363"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX185hhNzz3pbtPcgtGqqp5DK68LzL5PhJzE="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:4E6HEGhdWGkaNaq7ct07UQjplis=
In-Reply-To: <KZf6tlV2s29qC6sCU@bongo-ra.co>
Content-Language: en-US
 by: James Kuyper - Sun, 24 Sep 2023 16:28 UTC

On 9/24/23 07:48, Spiros Bousbouras wrote:
> On Sun, 24 Sep 2023 00:39:15 +0300
> Anton Shepelev <anton.txt@gmail.moc> wrote:
>> Futhermore, some systems seem not to require alignment,
>> e.g.:
>>
>> #include <stdio.h>
>> #include <stdlib.h>
>>
>> void print_as( char const * data )
>> { for( int i = 0; i < 2; i += 1 )
>> { printf( "%i ", *((short*)(data+i*2)) ); }
>
> %hi would be more precise than %i .At the very least it saves the reader
> the trouble of trying to collect from the various points in the standard
> the information to decide whether just %i is also guaranteed to work.

Just in case you wanted to know, here's the relevant sections:
"i The int argument ..." (7.19.6.1p8), which tells you that the argument
that corresponds to %i must be an int.

"If an int can represent all values of the original type, the value is
converted to an int; otherwise, it is converted to an unsigned int."
(6.3.1.1p2). Since all values representable as a short are also
representable as an int, short is always promoted to an int.

As a result, for printf(), "%hi" means the same thing as "%i"; it was
added solely for the purpose of allowing the same format string to be
used with both the printf family and the scanf family of functions.

>> printf("\n");
>> }
>>
>> int main()
>> { char* const data = malloc(5);
>> for( int i = 0; i < 5; i += 1 )
>> { data[i] = (char)i%2; }
>
> (char)i will be promoted to something else (most likely int) before the

Correct. It will normally be an int, unless CHAR_MAX > INT_MAX, in which
case char will promote to to unsigned int. That is possible only if char
is unsigned, and CHAR_BIT >= 16 (which does happen, but it's pretty
uncommon).

> % operation takes place so the cast is pointless. Even if you had written
> (char)(i%2) , given the values involved I don't see the point.

And, after being promoted to int or unsigned int, it will be converted
back to char.

Re: Dymamic arrays: memory management and naming

<20230924100501.143@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Sun, 24 Sep 2023 17:10:15 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 19
Message-ID: <20230924100501.143@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co> <uepo37$1du2j$2@dont-email.me>
Injection-Date: Sun, 24 Sep 2023 17:10:15 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="de7138e0de32fc51e80c05ed2d00fab7";
logging-data="1524931"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/HG6o+bxe0/x8iGl4nKO2U5OujcNkQDss="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Bv7fSkB4G0c+wWm6RbiKryzaDoQ=
 by: Kaz Kylheku - Sun, 24 Sep 2023 17:10 UTC

On 2023-09-24, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
> As a result, for printf(), "%hi" means the same thing as "%i"; it was
> added solely for the purpose of allowing the same format string to be
> used with both the printf family and the scanf family of functions.

It isn't the same. The h means that, internally, printf converts the
int argument value to short.

Thus, we cannot expect the correct decimal representation of INT_MAX
using printf("%hi\n", INT_MAX).

When used with d or i, h means "show me the implementation-defined
value when we truncate the argument to short".

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<uept87$1eto8$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Sun, 24 Sep 2023 13:56:23 -0400
Organization: A noiseless patient Spider
Lines: 12
Message-ID: <uept87$1eto8$1@dont-email.me>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co> <uepo37$1du2j$2@dont-email.me>
<20230924100501.143@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 24 Sep 2023 17:56:23 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="02f9defeecc8712da321e422f8c6bcdf";
logging-data="1537800"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18JqUI3wwqkaZYvfuNV5xuokKm/+nSs4hM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.15.1
Cancel-Lock: sha1:cYIrxLgqg4tri5fiuYtj0SdRZGE=
Content-Language: en-US
In-Reply-To: <20230924100501.143@kylheku.com>
 by: James Kuyper - Sun, 24 Sep 2023 17:56 UTC

On 9/24/23 13:10, Kaz Kylheku wrote:
> On 2023-09-24, James Kuyper <jameskuyper@alumni.caltech.edu> wrote:
>> As a result, for printf(), "%hi" means the same thing as "%i"; it was
>> added solely for the purpose of allowing the same format string to be
>> used with both the printf family and the scanf family of functions.
>
> It isn't the same. The h means that, internally, printf converts the
> int argument value to short.

I meant that it is the same when passing a short argument. Sorry for not
being sufficiently specific.

Re: Dymamic arrays: memory management and naming

<86edimjkab.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 09:21:32 -0700
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <86edimjkab.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="b3d2a53e0ccd70cd5158b7b6e81b5d4b";
logging-data="2117292"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+FBppAxigaIuG59KrFit+arles7uIBajc="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:TqHx2fTZuoHcucwaYEuLrJ6BqNk=
sha1:EbYmJjYjVm/xEPTfBFHBYIxxgxo=
 by: Tim Rentsch - Mon, 25 Sep 2023 16:21 UTC

Anton Shepelev <anton.txt@gmail.moc> writes:

> Tim Rentsch:
>
>> One further thought...
>>
>> As of C11, there is a type max_align_t, defined in
>> <stddef.h>, whose alignment is the maximum alignment
>> supported by the implementation. The grain size for
>> allocation should be a multiple of
>>
>> _Alignof (max_align_t)
>>
>> so this alignment can be supported. Also, the amount of
>> space you use for your "pre-array" metadata should be
>> chosen to be a multiple of _Alignof (max_align_t),
>> otherwise the array alignments might be off in some cases.
>
> [...] Yes, I have discussed this issue here before, and
> implemented a (I think Ben's) suggestion to calculate the
> negative offset as the size of the metadata structure
> rounded up to a power of two, which should ensure correct
> alignment in most cases.

Sometimes this technique will use too much space, and sometimes
too little. It's better to use a method that always works, if
one is available.

> Futhermore, some systems seem not to require alignment,
> [example removed]

It can happen that an implmentation doesn't need alignment on
some types but does need it on others. Moreover you want the
code that you write to be portable, so it's necessary to assume
that alignment requirements are in force.

In your particular case, a nice way to do it is as part of the
type definition for the array prefix metadata (I've forgotten
what you called it; here it is Header, and for expository reasons
I chose longer names for the structure members):

#include <stddef.h>

#define ALIGN_MAX _Alignas (max_align_t)

typedef struct {
size_t first_unused;
size_t total_available;
size_t element_size;
ALIGN_MAX char data[];
} Header;

Given this definition for the prefix metadata, there are simple
definitions for META() and DATA(), as follows:

Header *
META( void *elements ){
char *data = elements;
return data ? (Header*) (data - offsetof( Header, data )) : NULL;
}

void *
DATA( Header *h ){
return h ? h->data : NULL;
}

This approach provides just what you need, if C11 is available.

Re: Dymamic arrays: memory management and naming

<20230925104829.275@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 18:06:49 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 52
Message-ID: <20230925104829.275@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<86edimjkab.fsf@linuxsc.com>
Injection-Date: Mon, 25 Sep 2023 18:06:49 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="597126d75b51c4a1021936afd4da1e2b";
logging-data="2156184"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18fxPnO6Xf/U0Mahq6QWp6nOTvVEubhUU4="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:NeFPEWscLz98JkD4ticqHha6dg0=
 by: Kaz Kylheku - Mon, 25 Sep 2023 18:06 UTC

On 2023-09-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> #include <stddef.h>
>
> #define ALIGN_MAX _Alignas (max_align_t)
>
> typedef struct {
> size_t first_unused;
> size_t total_available;
> size_t element_size;
> ALIGN_MAX char data[];
> } Header;
>
> Given this definition for the prefix metadata, there are simple
> definitions for META() and DATA(), as follows:
>
> Header *
> META( void *elements ){
> char *data = elements;
> return data ? (Header*) (data - offsetof( Header, data )) : NULL;
> }
>
> void *
> DATA( Header *h ){
> return h ? h->data : NULL;
> }

How about:

typedef ALIGN_MAX struct {
size_t first_unused;
size_t total_available;
size_t element_size;
} Header;

Header *
META(void *elements) {
return elements ? (Header *) elements - 1 : NULL;
}

void *
DATA(Header *h) {
return h ? h + 1 : NULL;
}

Can't we have ALIGN_MAX on the struct type, like with
__attribute__((aligned))?

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<86a5taj96a.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 13:21:33 -0700
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <86a5taj96a.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="b3d2a53e0ccd70cd5158b7b6e81b5d4b";
logging-data="2202477"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/3QBBhh6nJZvBR4eFMHzVBg5iSAEU7jTk="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:Xofo4sSG3E2+ShANYN6fs/O2a7k=
sha1:KRgo8Q9NtdQhw7ovvMUkk0HCH6k=
 by: Tim Rentsch - Mon, 25 Sep 2023 20:21 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> People reviewing C code should know the utter basics, like that short
> promotes to int. This is not an unreasonable expectation.

Amusing, in light of what follows.

> [...]
>
> h only makes sense if you're passing a signed argument and want it
> printed as unsigned. Say we have 16 bit short and 32 bit int.
> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
> will obtain the desired 65535.

Both of those have undefined behavior.

Re: Dymamic arrays: memory management and naming

<20230925134735.419@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 21:06:24 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <20230925134735.419@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com>
<86a5taj96a.fsf@linuxsc.com>
Injection-Date: Mon, 25 Sep 2023 21:06:24 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="597126d75b51c4a1021936afd4da1e2b";
logging-data="2220064"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19K5Vx/cT8GyXqXKjyfJNhEA+fsUi4/M78="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Glpm4JxHoYx6AJdiM6VSh1f0QRw=
 by: Kaz Kylheku - Mon, 25 Sep 2023 21:06 UTC

On 2023-09-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>
>> People reviewing C code should know the utter basics, like that short
>> promotes to int. This is not an unreasonable expectation.
>
> Amusing, in light of what follows.
>
>> [...]
>>
>> h only makes sense if you're passing a signed argument and want it
>> printed as unsigned. Say we have 16 bit short and 32 bit int.
>> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
>> will obtain the desired 65535.
>
> Both of those have undefined behavior.

Not both; the second one is okay.

(I should definitely not have asserted that the wrong example will print
a certain output, though it is an example of surprising output that
occurs in practice on common platforms.)

N3096 states, and C99 had a similar wording:

h Specifies that a following b, d, i, o, u, x, or X conversion
specifier applies to a short int or unsigned short int argument.

When the h is present, punning is allowed between signed and unsigned.

There is no wording which restricts by subset: that b, o, u, x and X
take an unsigned short, and only d and i take a short.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<865y3yj70m.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 14:08:09 -0700
Organization: A noiseless patient Spider
Lines: 50
Message-ID: <865y3yj70m.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <87led1izo1.fsf@nosuchdomain.example.com> <20230919154533.263@kylheku.com> <86bkdxl8da.fsf@linuxsc.com> <20230920080251.690@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="b3d2a53e0ccd70cd5158b7b6e81b5d4b";
logging-data="2220809"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18FqIW6sjqsk4k9Njjmge5bOmwqT2KpYw0="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:oFD4mQdetEMAaUrwhQ7Qu9TCxqc=
sha1:r2g8x/7GXfMaQXjXun2DbYr4j9c=
 by: Tim Rentsch - Mon, 25 Sep 2023 21:08 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> On 2023-09-20, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>
>>> On 2023-09-19, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>
>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>> [...]
>>>>
>>>>> Note by the way that sizeof (max_align_t) might not be the same
>>>>> as _Alignof (max_align_t).
>>>>
>>>> Indeed. I was curious about this, so I wrote a small test program that
>>>> shows that _Alignof (max_align_t) is 16, but sizeof (max_align_t) is 32
>>>> on the implementation I'm using.
>>>>
>>>> I'm using gcc, which defines max_align_t as a struct containing a long
>>>> long, a long double, and optionally a __float128. It could have reduced
>>>> the size by making it a union rather than a struct, but there's rarely
>>>> (?) any reason to create objects of type max_align_t.
>>>
>>> Yes there is!
>>>
>>> union {
>>> foo_t x;
>>> bar_t y;
>>>
>>> // please give my own union maximum alignment --- without undue bloat
>>>
>>> max_align_t align;
>>> }
>>>
>>> A max_align_t that can only be used as _Alignof(max_align_t) is useless;
>>> it might as well be a #define ALIGN_MAX 16 constant in some header
>>> like <limits.h>.
>>
>> You need to think harder.
>
> I'm "thinking broader" instead, maybe?

No, you aren't. First you gave faulty reasoning for needing to
declare something that has type max_align_t. When that reasoning
was shown to be wrong you fell back to an imaginary world to say
something would be nice to have when the something provides no
greater expressive power than what is there already. It's great
to have new ideas, but not all new ideas are good ideas. There
is no need for what you propose, and thus adding it to C would
have negative value.

Re: Dymamic arrays: memory management and naming

<20230925141208.949@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 21:27:13 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 86
Message-ID: <20230925141208.949@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com> <87led1izo1.fsf@nosuchdomain.example.com>
<20230919154533.263@kylheku.com> <86bkdxl8da.fsf@linuxsc.com>
<20230920080251.690@kylheku.com> <865y3yj70m.fsf@linuxsc.com>
Injection-Date: Mon, 25 Sep 2023 21:27:13 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="597126d75b51c4a1021936afd4da1e2b";
logging-data="2226608"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+RYGAVtyvbmRPA8cLDMsnDkgdSRpXWSTw="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:Sl2dnbpmD7cW5mdcKhorDCdZ24Y=
 by: Kaz Kylheku - Mon, 25 Sep 2023 21:27 UTC

On 2023-09-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>
>> On 2023-09-20, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>>
>>> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>>
>>>> On 2023-09-19, Keith Thompson <Keith.S.Thompson+u@gmail.com> wrote:
>>>>
>>>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>>> [...]
>>>>>
>>>>>> Note by the way that sizeof (max_align_t) might not be the same
>>>>>> as _Alignof (max_align_t).
>>>>>
>>>>> Indeed. I was curious about this, so I wrote a small test program that
>>>>> shows that _Alignof (max_align_t) is 16, but sizeof (max_align_t) is 32
>>>>> on the implementation I'm using.
>>>>>
>>>>> I'm using gcc, which defines max_align_t as a struct containing a long
>>>>> long, a long double, and optionally a __float128. It could have reduced
>>>>> the size by making it a union rather than a struct, but there's rarely
>>>>> (?) any reason to create objects of type max_align_t.
>>>>
>>>> Yes there is!
>>>>
>>>> union {
>>>> foo_t x;
>>>> bar_t y;
>>>>
>>>> // please give my own union maximum alignment --- without undue bloat
>>>>
>>>> max_align_t align;
>>>> }
>>>>
>>>> A max_align_t that can only be used as _Alignof(max_align_t) is useless;
>>>> it might as well be a #define ALIGN_MAX 16 constant in some header
>>>> like <limits.h>.
>>>
>>> You need to think harder.
>>
>> I'm "thinking broader" instead, maybe?
>
> No, you aren't. First you gave faulty reasoning for needing to
> declare something that has type max_align_t. When that reasoning
> was shown to be wrong you fell back to an imaginary world to say
> something would be nice to have when the something provides no
> greater expressive power than what is there already.

That sounds tautological; nothing provides greater expressive
power than that power which it has already.

> It's great
> to have new ideas, but not all new ideas are good ideas. There

I'm pretty sure that sticking a dummy member into a union so that it has
at least that member's alignment isn't a new idea, let alone my idea.

> is no need for what you propose, and thus adding it to C would
> have negative value.

Can you clarify what it is you think that I proposed?

I believe that it consisted entirely of the idea that max_align_t would
be improved if it were required to have a size that does not exceed its
alignment.

However, that literal idea has flaws, because what if there is no such
type? Say max alignment is four bytes, on a machine that doesn't have a
four byte type, only one or eight. max_align_t will have to be eight,
which will make the union larger than necessary. (This is why in the
code comment I wrote "without undue bloat": if bloat is due, it's due.)

The example given by Keith of how GCC defined max_align_t, however,
gratuitously makes the type larger by simply aggregating various types
via struct. The underlying platform does have types whose size is equal
to the alignment of that max_align_t.

If a reasonable requirement could be articulated to discourage that, it
would be good.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<861qelki2i.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 15:24:05 -0700
Organization: A noiseless patient Spider
Lines: 53
Message-ID: <861qelki2i.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <86edimjkab.fsf@linuxsc.com> <20230925104829.275@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="52bf0d5ee5119c0cfc0137a030202cf0";
logging-data="2247392"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19K8ifEULYbO9VW2ethdJvaIYdKRUfifhA="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:0dziNuPzgYp5K13loedevtofnmg=
sha1:iJxSi0T7b+xeZqki3g1SMcWn3xQ=
 by: Tim Rentsch - Mon, 25 Sep 2023 22:24 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> On 2023-09-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> #include <stddef.h>
>>
>> #define ALIGN_MAX _Alignas (max_align_t)
>>
>> typedef struct {
>> size_t first_unused;
>> size_t total_available;
>> size_t element_size;
>> ALIGN_MAX char data[];
>> } Header;
>>
>> Given this definition for the prefix metadata, there are simple
>> definitions for META() and DATA(), as follows:
>>
>> Header *
>> META( void *elements ){
>> char *data = elements;
>> return data ? (Header*) (data - offsetof( Header, data )) : NULL;
>> }
>>
>> void *
>> DATA( Header *h ){
>> return h ? h->data : NULL;
>> }
>
> How about:
>
> typedef ALIGN_MAX struct {
> size_t first_unused;
> size_t total_available;
> size_t element_size;
> } Header;
>
> Header *
> META(void *elements) {
> return elements ? (Header *) elements - 1 : NULL;
> }
>
> void *
> DATA(Header *h) {
> return h ? h + 1 : NULL;
> }
>
> Can't we have ALIGN_MAX on the struct type, like with
> __attribute__((aligned))?

Please find something better to do than post these
useless responses. I don't want to waste any more
time on them.

Re: Dymamic arrays: memory management and naming

<20230925155845.139@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 23:14:52 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 33
Message-ID: <20230925155845.139@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com>
Injection-Date: Mon, 25 Sep 2023 23:14:52 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="84073cef60c20381e418356514cfa3d7";
logging-data="2263361"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+4nLRIR1EfKdYzqngL6YRAouI+DZyRImc="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:AL5BB2xX3gSD5FaqjMNewuoSbf4=
 by: Kaz Kylheku - Mon, 25 Sep 2023 23:14 UTC

On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
> h only makes sense if you're passing a signed argument and want it
> printed as unsigned. Say we have 16 bit short and 32 bit int.
> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
> will obtain the desired 65535.

There is something I had neglected to mention: the use of h for portability

If we have "%u" paired with an unsigned short, argument we have
a mismatch on a large number of platforms, where unsigned short promotes
to int. The %u specifier wants unsigned int.

%hu will take care of it somehow. It knows which way the unsigned
short argument has promoted, and recover the value.

The h modifier makes the conversion specifier compatible with either
a short or unsigned short argument. That means that on platforms where
they promote the same way, it expects an actual type of int.
On platforms where they don't it has to do something like, oh,
in the case of the unsigned specifier, assume that the argument
was converted from an unsigned short, and treat the bits accordingly.

If we want specifically %u for decimal output, and we know that
unsigned short promotes to int, we can get away with %d.

That's not correct if unsigned short isn't promoting to int, and
not applicable if we want %x/%X, %o or %b.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<20230925161504.994@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 23:49:29 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <20230925161504.994@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<86edimjkab.fsf@linuxsc.com> <20230925104829.275@kylheku.com>
<861qelki2i.fsf@linuxsc.com>
Injection-Date: Mon, 25 Sep 2023 23:49:29 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="84073cef60c20381e418356514cfa3d7";
logging-data="2273559"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+B+N1LYRBMvArbmz04ou471gatSJhfGc0="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:F/UjP84KnrZBZr+EBr03w+T33Ds=
 by: Kaz Kylheku - Mon, 25 Sep 2023 23:49 UTC

On 2023-09-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>> Can't we have ALIGN_MAX on the struct type, like with
>> __attribute__((aligned))?
>
> Please find something better to do than post these
> useless responses. I don't want to waste any more
^^^^^^^

Thanks for the hint; that informs me there is something wrong with it.

Indeed, _Alignas cannot be used that way, due to a constraint:

"The _Alignas specifier shall not be used in conjunction with either of
the storage class specifiers typedef or register." [N3096 6.7.5]

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<20230925170025.873@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Tue, 26 Sep 2023 00:07:02 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <20230925170025.873@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<86edimjkab.fsf@linuxsc.com> <20230925104829.275@kylheku.com>
<861qelki2i.fsf@linuxsc.com>
Injection-Date: Tue, 26 Sep 2023 00:07:02 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="84073cef60c20381e418356514cfa3d7";
logging-data="2277106"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19ZpxVNIoSZKYjSnahy+6J46pnFb2Xr1TY="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:U3icInSqeKb60EWn8N8ik05nYA8=
 by: Kaz Kylheku - Tue, 26 Sep 2023 00:07 UTC

On 2023-09-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>> Can't we have ALIGN_MAX on the struct type, like with
>> __attribute__((aligned))?
>
> Please find something better to do than post these
> useless responses. I don't want to waste any more
> time on them.

Aha! I got it: let's take all the members of Header and wrap them into an
anonymous struct member. Then, put the _Alignas onto that member.

#include <stddef.h>

#define ALIGN_MAX _Alignas (max_align_t)

typedef struct {
ALIGN_MAX struct {
size_t first_unused;
size_t total_available;
size_t element_size;
};
} Header;

Header *
META(void *elements) {
return elements ? (Header *) elements - 1 : NULL;
}

void *
DATA(Header *h) {
return h ? h + 1 : NULL;
}

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

<86wmwdiu9w.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 18:43:23 -0700
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <86wmwdiu9w.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com> <86a5taj96a.fsf@linuxsc.com> <20230925134735.419@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="52bf0d5ee5119c0cfc0137a030202cf0";
logging-data="2430595"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19V6Z+VHxMOmKvqnJnYb6E4d2/BAXpYkzw="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:+LaHfKRrey532z4Y4U5vzV+phS0=
sha1:B5GUCWL+M5bZXkyqTQF+fd3QhGc=
 by: Tim Rentsch - Tue, 26 Sep 2023 01:43 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> On 2023-09-25, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>
>>> People reviewing C code should know the utter basics, like that short
>>> promotes to int. This is not an unreasonable expectation.
>>
>> Amusing, in light of what follows.
>>
>>> [...]
>>>
>>> h only makes sense if you're passing a signed argument and want it
>>> printed as unsigned. Say we have 16 bit short and 32 bit int.
>>> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
>>> will obtain the desired 65535.
>>
>> Both of those have undefined behavior.
>
> Not both; the second one is okay.

That is right. The wording in C11 is not completely clear on
the question; however, wording in the C23 draft clarifies the
issue, and establishes that the behavior in this particular
case is well-defined.

It's still a bad idea IMO to use %hu with an argument that
has (before promotion) a signed type, but that's a style
issue, and doesn't change the definedness of this case.

Re: Dymamic arrays: memory management and naming

<86sf71itgt.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 19:00:50 -0700
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <86sf71itgt.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com> <20230925155845.139@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="52bf0d5ee5119c0cfc0137a030202cf0";
logging-data="2436956"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18mWa8MJT47Wu/BkFnmgJnTwsUWGSWYkbU="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:VKulYtjDJlPlMO1TKsg/reUKhQM=
sha1:t7jZfm0Vc0nys3DF6ZmDU2EIzAk=
 by: Tim Rentsch - Tue, 26 Sep 2023 02:00 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>
>> h only makes sense if you're passing a signed argument and want it
>> printed as unsigned. Say we have 16 bit short and 32 bit int.
>> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
>> will obtain the desired 65535.
>
> There is something I had neglected to mention: the use of h for portability
>
> If we have "%u" paired with an unsigned short, argument we have
> a mismatch on a large number of platforms, where unsigned short promotes
> to int. The %u specifier wants unsigned int.

A %u conversion specification is always okay for an unsigned
short argument, regardless of whether the argument promotes to
int or to unsigned int. Reading an integer-typed variadic
argument with the corresponding type of the other signedness
is allowed when the argument value is representable in both
types, which it always will be in this case even if unsigned
short promotes to int.

Re: Dymamic arrays: memory management and naming

<20230925190228.732@kylheku.com>

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Tue, 26 Sep 2023 02:38:19 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <20230925190228.732@kylheku.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com>
<20230925155845.139@kylheku.com> <86sf71itgt.fsf@linuxsc.com>
Injection-Date: Tue, 26 Sep 2023 02:38:19 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="84073cef60c20381e418356514cfa3d7";
logging-data="2448309"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19hsCAcwJXF3XOgypS12vBeMlWYRW+yltk="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:uBGyiTU/RsemlLYqNVoCXXEECC0=
 by: Kaz Kylheku - Tue, 26 Sep 2023 02:38 UTC

On 2023-09-26, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>
>> On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>
>>> h only makes sense if you're passing a signed argument and want it
>>> printed as unsigned. Say we have 16 bit short and 32 bit int.
>>> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
>>> will obtain the desired 65535.
>>
>> There is something I had neglected to mention: the use of h for portability
>>
>> If we have "%u" paired with an unsigned short, argument we have
>> a mismatch on a large number of platforms, where unsigned short promotes
>> to int. The %u specifier wants unsigned int.
>
> A %u conversion specification is always okay for an unsigned
> short argument, regardless of whether the argument promotes to
> int or to unsigned int.

> Reading an integer-typed variadic
> argument with the corresponding type of the other signedness
> is allowed when the argument value is representable in both
> types, which it always will be in this case even if unsigned
> short promotes to int.

OK, I see how that is derived. printf is now described as if using
va_arg to extract the promoted type of the conversion specifier's type,
and then converting the promoted type to the the specifier's type (which
can hardly be done in any other way).

C99 didn't have this, only "If any argument is not the correct type for
the corresponding conversion specification, the behavior is undefined."

(Even though it already featured the signed/unsigned flexibility
measure in va_arg!)

If %u is implemented in C99 using va_arg, it will be well defined
for an unsigned short argument. For it not to be well-defined, it would
have to use some other mechanism than va_arg, which is more fragile.
But that makes next to no sense; this is a defect fix, really.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca
NOTE: If you use Google Groups, I don't see you, unless you're whitelisted.

Re: Dymamic arrays: memory management and naming

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

  copy mid

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

  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: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 19:38:39 -0700
Organization: None to speak of
Lines: 70
Message-ID: <87msx9u09c.fsf@nosuchdomain.example.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com>
<20230925155845.139@kylheku.com> <86sf71itgt.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="ec8cfd24bb03823b1c68ea0c43111c0c";
logging-data="2448389"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+x/BWKAu6I2rhzGlZ5zbAo"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:A5kSrHl1p1aiLz4EpSARPd4V8Ts=
sha1:s/vhrHPAA3C3wC9BZqE906myv/M=
 by: Keith Thompson - Tue, 26 Sep 2023 02:38 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>> On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>> h only makes sense if you're passing a signed argument and want it
>>> printed as unsigned. Say we have 16 bit short and 32 bit int.
>>> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
>>> will obtain the desired 65535.
>>
>> There is something I had neglected to mention: the use of h for portability
>>
>> If we have "%u" paired with an unsigned short, argument we have
>> a mismatch on a large number of platforms, where unsigned short promotes
>> to int. The %u specifier wants unsigned int.
>
> A %u conversion specification is always okay for an unsigned
> short argument, regardless of whether the argument promotes to
> int or to unsigned int. Reading an integer-typed variadic
> argument with the corresponding type of the other signedness
> is allowed when the argument value is representable in both
> types, which it always will be in this case even if unsigned
> short promotes to int.

Agreed, but unfortunately the standard doesn't quite say that in
normative text -- until C23, as I discovered while writing this.

In C17 and earlier, 7.21.6.1p9 (The fprintf function) says:
If any argument is not the correct type for the corresponding
conversion specification, the behavior is undefined.

(The word "the" implies that there is only one "correct type".)

6.2.5p9 (Types) says:
The range of nonnegative values of a signed integer type is a
subrange of the corresponding unsigned integer type, and the
representation of the same value in each type is the same.
with a footnote:
The same representation and alignment requirements are meant to
imply interchangeability as arguments to functions, return values
from functions, and members of unions.
but of course footnotes are non-normative.

I can imagine a (contrived) conforming implementation that passes
int arguments in one set of CPU registers and unsigned int arguments
in another set of CPU registers, so that `printf("%u\n", 42);`
would very probably *not* print "42". Such an implementation
would violate the intent of that footnote, but would not as far
as I can tell violate any normative requirement. (In real life,
I'm sure no such implementation exists.)

But C23 (N3096 draft) changes the description for fprintf:
fprintf shall behave as if it uses va_arg with a type argument
naming the type resulting from applying the default argument
promotions to the type corresponding to the conversion
specification and then converting the result of the va_arg
expansion to the type corresponding to the conversion
specification.

and the description of the va_arg() macro explicitly allows the type
of the first argument and the type given by the second argument
to be corresponding signed and unsigned types, if the value is
representable in both types. (This goes back to C99.)

Prior to C23, though the <stdarg.h> macros are the obvious way to
implement the *printf() functions, the standard didn't require it
to behave that way.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Dymamic arrays: memory management and naming

<86o7hpiggw.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 23:41:35 -0700
Organization: A noiseless patient Spider
Lines: 47
Message-ID: <86o7hpiggw.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com> <20230925155845.139@kylheku.com> <86sf71itgt.fsf@linuxsc.com> <87msx9u09c.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="52bf0d5ee5119c0cfc0137a030202cf0";
logging-data="2515389"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19BFEsSaEYfiV99WtKf67+T3R7DxtNaxDM="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:VGnzui2jBsFzDlYwk1r8GMa0Kxw=
sha1:CgTQuJ5eyXP5RrLqqceBvIPIzlw=
 by: Tim Rentsch - Tue, 26 Sep 2023 06:41 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>
>>> On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>
>>>> h only makes sense if you're passing a signed argument and want
>>>> it printed as unsigned. Say we have 16 bit short and 32 bit int.
>>>> Then "%u"/(short) -1 will print as 429496725, whereas
>>>> "%hu"/(short) -1 will obtain the desired 65535.
>>>
>>> There is something I had neglected to mention: the use of h for
>>> portability
>>>
>>> If we have "%u" paired with an unsigned short, argument we have a
>>> mismatch on a large number of platforms, where unsigned short
>>> promotes to int. The %u specifier wants unsigned int.
>>
>> A %u conversion specification is always okay for an unsigned
>> short argument, regardless of whether the argument promotes to
>> int or to unsigned int. Reading an integer-typed variadic
>> argument with the corresponding type of the other signedness
>> is allowed when the argument value is representable in both
>> types, which it always will be in this case even if unsigned
>> short promotes to int.
>
> Agreed, but unfortunately the standard doesn't quite say that in
> normative text -- until C23, as I discovered while writing this.
> [...]

My position is that this rule was intended since at least C99
(and perhaps earlier) but was not expressed at unambiguously as
one might like.

Also I believe there is support for this view if one consults
various historical documents done by the ISO C committee.

Also I think it is rather pointless to debate whether the C
standard does or does not "say that in normative text" when
the normative text that is there lends itself to different
interpretations. We all agree on what words are used; what
the words mean in this case is more a subjective reaction
than an objective fact. And is pretty much beside the point
since what was intended and what was understood is the same
for most of the people who are familiar with the C standard.

Re: Dymamic arrays: memory management and naming

<86jzsdigek.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Mon, 25 Sep 2023 23:42:59 -0700
Organization: A noiseless patient Spider
Lines: 40
Message-ID: <86jzsdigek.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com> <20230925155845.139@kylheku.com> <86sf71itgt.fsf@linuxsc.com> <20230925190228.732@kylheku.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="52bf0d5ee5119c0cfc0137a030202cf0";
logging-data="2515389"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/c5bPqcjkW/lkvNwqWsUoYfSayLqptLGc="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:75/IWqrPk/CFCa3nw1cBMX28iPs=
sha1:n4DciHFNutT9ODC8OlE61mp9q8Y=
 by: Tim Rentsch - Tue, 26 Sep 2023 06:42 UTC

Kaz Kylheku <864-117-4973@kylheku.com> writes:

> On 2023-09-26, Tim Rentsch <tr.17687@z991.linuxsc.com> wrote:
>
>> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>
>>> On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>
>>>> h only makes sense if you're passing a signed argument and want it
>>>> printed as unsigned. Say we have 16 bit short and 32 bit int.
>>>> Then "%u"/(short) -1 will print as 429496725, whereas "%hu"/(short) -1
>>>> will obtain the desired 65535.
>>>
>>> There is something I had neglected to mention: the use of h for
>>> portability
>>>
>>> If we have "%u" paired with an unsigned short, argument we have a
>>> mismatch on a large number of platforms, where unsigned short
>>> promotes to int. The %u specifier wants unsigned int.
>>
>> A %u conversion specification is always okay for an unsigned
>> short argument, regardless of whether the argument promotes to
>> int or to unsigned int.
>>
>> Reading an integer-typed variadic
>> argument with the corresponding type of the other signedness
>> is allowed when the argument value is representable in both
>> types, which it always will be in this case even if unsigned
>> short promotes to int.
>
> OK, I see how that is derived. printf is now described as if using
> va_arg to extract the promoted type of the conversion specifier's
> type, and then converting the promoted type to the the specifier's
> type (which can hardly be done in any other way).
>
> C99 didn't have this, only "If any argument is not the correct type
> for the corresponding conversion specification, the behavior is
> undefined."

Please see my response to the parallel post by Keith Thompson.

Re: Dymamic arrays: memory management and naming

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!news.neodome.net!news.mixmin.net!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Tue, 26 Sep 2023 02:42:08 -0700
Organization: None to speak of
Lines: 63
Message-ID: <87fs31tgnj.fsf@nosuchdomain.example.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com>
<20230925155845.139@kylheku.com> <86sf71itgt.fsf@linuxsc.com>
<87msx9u09c.fsf@nosuchdomain.example.com> <86o7hpiggw.fsf@linuxsc.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="ec8cfd24bb03823b1c68ea0c43111c0c";
logging-data="2576671"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/yE91qxUGnVNrYWwS3TYWq"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:STswHQqH1s8g1QXqeFrfP9x0ZrA=
sha1:CupKj8spd+h5Xlgfqyb/+BcrACw=
 by: Keith Thompson - Tue, 26 Sep 2023 09:42 UTC

Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>>> On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>>> h only makes sense if you're passing a signed argument and want
>>>>> it printed as unsigned. Say we have 16 bit short and 32 bit int.
>>>>> Then "%u"/(short) -1 will print as 429496725, whereas
>>>>> "%hu"/(short) -1 will obtain the desired 65535.
>>>>
>>>> There is something I had neglected to mention: the use of h for
>>>> portability
>>>>
>>>> If we have "%u" paired with an unsigned short, argument we have a
>>>> mismatch on a large number of platforms, where unsigned short
>>>> promotes to int. The %u specifier wants unsigned int.
>>>
>>> A %u conversion specification is always okay for an unsigned
>>> short argument, regardless of whether the argument promotes to
>>> int or to unsigned int. Reading an integer-typed variadic
>>> argument with the corresponding type of the other signedness
>>> is allowed when the argument value is representable in both
>>> types, which it always will be in this case even if unsigned
>>> short promotes to int.
>>
>> Agreed, but unfortunately the standard doesn't quite say that in
>> normative text -- until C23, as I discovered while writing this.
>> [...]
>
> My position is that this rule was intended since at least C99
> (and perhaps earlier) but was not expressed at unambiguously as
> one might like.

It was not expressed in normative text, ambiguously or otherwise.

> Also I believe there is support for this view if one consults
> various historical documents done by the ISO C committee.

You wouldn't have specific citations, would you?

In any case, the language is defined by the standard, not by
historical documents with wording that never made it into the
standard. No implementer or programmer should have to consult
"historical documents" to determine what the standard requires.

> Also I think it is rather pointless to debate whether the C
> standard does or does not "say that in normative text" when
> the normative text that is there lends itself to different
> interpretations. We all agree on what words are used; what
> the words mean in this case is more a subjective reaction
> than an objective fact. And is pretty much beside the point
> since what was intended and what was understood is the same
> for most of the people who are familiar with the C standard.

You consider it pointless. I don't. I find both the intent behind
the standard and what the standard says interesting. In this case,
they were two different things, and I consider that to be a flaw
in the standard. I'm glad the flaw is being corrected in C23.

--
Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com
Will write code for food.
void Void(void) { Void(); } /* The recursive call of the void */

Re: Dymamic arrays: memory management and naming

<877co853zo.fsf@fatphil.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: pc+use...@asdf.org (Phil Carmody)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Sat, 30 Sep 2023 13:50:51 +0300
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <877co853zo.fsf@fatphil.org>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc>
<86ediwmk8b.fsf@linuxsc.com>
<20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com>
<20230924003915.51bb21339ab1c551eba59162@gmail.moc>
<KZf6tlV2s29qC6sCU@bongo-ra.co>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: dont-email.me; posting-host="b462d095ec36d5575d1738328f849fd2";
logging-data="875702"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+UboTB3/5bk6O6heH4p4b/"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux)
Cancel-Lock: sha1:xSdAq4ACG0BOCubKIrS1/HiqEas=
sha1:/DZ6tj0O6B7K/05OpudlpzYXOQ4=
 by: Phil Carmody - Sat, 30 Sep 2023 10:50 UTC

Spiros Bousbouras <spibou@gmail.com> writes:
> On Sun, 24 Sep 2023 00:39:15 +0300
> Anton Shepelev <anton.txt@gmail.moc> wrote:
>> for( int i = 0; i < 5; i += 1 )
>> { data[i] = (char)i%2; }
>
> (char)i will be promoted to something else (most likely int) before the
> % operation takes place so the cast is pointless. Even if you had written
> (char)(i%2) , given the values involved I don't see the point.

Would you have made the same argument had the '2' been a '3' and the '5'
been an unknown value? The defensive coder in me sees almost all
constants as a possibly having alternative values.

Phil
--
We are no longer hunters and nomads. No longer awed and frightened, as we have
gained some understanding of the world in which we live. As such, we can cast
aside childish remnants from the dawn of our civilization.
-- NotSanguine on SoylentNews, after Eugen Weber in /The Western Tradition/

Re: Dymamic arrays: memory management and naming

<XwSxLptRc6CtAbJ0Z@bongo-ra.co>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: spi...@gmail.com (Spiros Bousbouras)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Sun, 1 Oct 2023 17:43:59 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <XwSxLptRc6CtAbJ0Z@bongo-ra.co>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc>
<86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <KZf6tlV2s29qC6sCU@bongo-ra.co>
<877co853zo.fsf@fatphil.org>
MIME-Version: 1.0
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: 8bit
Injection-Date: Sun, 1 Oct 2023 17:43:59 -0000 (UTC)
Injection-Info: dont-email.me; posting-host="b11860471a9b99b1f84ed44b7a68c75d";
logging-data="2549623"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19sZK/56FlQXQ1x1F5S5bim"
Cancel-Lock: sha1:9yFSYA0NdztbxLfl/+1XizAPPNw=
X-Server-Commands: nowebcancel
In-Reply-To: <877co853zo.fsf@fatphil.org>
X-Organisation: Weyland-Yutani
 by: Spiros Bousbouras - Sun, 1 Oct 2023 17:43 UTC

On Sat, 30 Sep 2023 13:50:51 +0300
Phil Carmody <pc+usenet@asdf.org> wrote:
> Spiros Bousbouras <spibou@gmail.com> writes:
> > On Sun, 24 Sep 2023 00:39:15 +0300
> > Anton Shepelev <anton.txt@gmail.moc> wrote:
> >> for( int i = 0; i < 5; i += 1 )
> >> { data[i] = (char)i%2; }
> >
> > (char)i will be promoted to something else (most likely int) before the
> > % operation takes place so the cast is pointless. Even if you had written
> > (char)(i%2) , given the values involved I don't see the point.
>
> Would you have made the same argument had the '2' been a '3' and the '5'
> been an unknown value?

The 2 being a 3 would have made no difference to my points. 5 being some
other value would depend on what restrictions we would have about the set of
possible values. One needs some restrictions to be able to programme at all ,
even knowing that the possible value is an integer is also a restriction.

> The defensive coder in me sees almost all
> constants as a possibly having alternative values.

Prudent coding is also not placing undue burden on one's mental faculties : if
no obvious generalisation suggests itself then don't try to invent one. For
a kind of extreme anti-generalisation attitude I offer the following from the
inventor of Forth :

I do not have a piece of software that will handle N pin chips, even where N
is between 40 and 84. I have three versions each is specialized. To add the
code necessary to change the number of output pads is certainly possible. But
I don't need to solve the general problem when I only have to solve these
specific cases.

Chuck Moore
http://www.ultratechnology.com/tape1-2.htm

--
vlaho.ninja/prog

Re: Dymamic arrays: memory management and naming

<86sf6sc82x.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: Dymamic arrays: memory management and naming
Date: Tue, 03 Oct 2023 03:29:42 -0700
Organization: A noiseless patient Spider
Lines: 67
Message-ID: <86sf6sc82x.fsf@linuxsc.com>
References: <20230909132336.99cdb303ad685bc1e40f92df@gmail.moc> <86ediwmk8b.fsf@linuxsc.com> <20230919011018.d9ed14da6e842291d47b69d7@gmail.moc> <86jzsmkqyu.fsf@linuxsc.com> <20230924003915.51bb21339ab1c551eba59162@gmail.moc> <KZf6tlV2s29qC6sCU@bongo-ra.co> <20230924081911.947@kylheku.com> <20230925155845.139@kylheku.com> <86sf71itgt.fsf@linuxsc.com> <87msx9u09c.fsf@nosuchdomain.example.com> <86o7hpiggw.fsf@linuxsc.com> <87fs31tgnj.fsf@nosuchdomain.example.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=us-ascii
Injection-Info: dont-email.me; posting-host="a7f15c7033ff7991d1a910385a293cab";
logging-data="3690250"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX196NEYc9Fh41JhtqBT2noXwzsmCh26R66E="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:yCXWCKRFe5ls2CR6pbek0N9n7yY=
sha1:NSwGsiHokxF0dhPdpJTVXELvoy8=
 by: Tim Rentsch - Tue, 3 Oct 2023 10:29 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:

> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>
>>> Tim Rentsch <tr.17687@z991.linuxsc.com> writes:
>>>
>>>> Kaz Kylheku <864-117-4973@kylheku.com> writes:
>>>>
>>>>> On 2023-09-24, Kaz Kylheku <864-117-4973@kylheku.com> wrote:
>>>>>
>>>>>> h only makes sense if you're passing a signed argument and want
>>>>>> it printed as unsigned. Say we have 16 bit short and 32 bit int.
>>>>>> Then "%u"/(short) -1 will print as 429496725, whereas
>>>>>> "%hu"/(short) -1 will obtain the desired 65535.
>>>>>
>>>>> There is something I had neglected to mention: the use of h for
>>>>> portability
>>>>>
>>>>> If we have "%u" paired with an unsigned short, argument we have a
>>>>> mismatch on a large number of platforms, where unsigned short
>>>>> promotes to int. The %u specifier wants unsigned int.
>>>>
>>>> A %u conversion specification is always okay for an unsigned
>>>> short argument, regardless of whether the argument promotes to
>>>> int or to unsigned int. Reading an integer-typed variadic
>>>> argument with the corresponding type of the other signedness
>>>> is allowed when the argument value is representable in both
>>>> types, which it always will be in this case even if unsigned
>>>> short promotes to int.
>>>
>>> Agreed, but unfortunately the standard doesn't quite say that in
>>> normative text -- until C23, as I discovered while writing this.
>>> [...]
>>
>> My position is that this rule was intended since at least C99
>> (and perhaps earlier) but was not expressed at unambiguously as
>> one might like.
>
> It was not expressed in normative text, ambiguously or otherwise.
>
>> Also I believe there is support for this view if one consults
>> various historical documents done by the ISO C committee.
>
> You wouldn't have specific citations, would you?
>
> In any case, the language is defined by the standard, not by
> historical documents with wording that never made it into the
> standard. No implementer or programmer should have to consult
> "historical documents" to determine what the standard requires.
>
>> Also I think it is rather pointless to debate whether the C
>> standard does or does not "say that in normative text" when
>> the normative text that is there lends itself to different
>> interpretations. We all agree on what words are used; what
>> the words mean in this case is more a subjective reaction
>> than an objective fact. And is pretty much beside the point
>> since what was intended and what was understood is the same
>> for most of the people who are familiar with the C standard.
>
> You consider it pointless. I don't. I find both the intent behind
> the standard and what the standard says interesting. In this case,
> they were two different things, and I consider that to be a flaw
> in the standard. I'm glad the flaw is being corrected in C23.

I repeat my previous statements.


devel / comp.lang.c / Re: Dymamic arrays: memory management and naming

Pages:123
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor