Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

24 Apr, 2024: Testing a new version of the Overboard here. If you have an issue post about it to rocksolid.nodes.help (I know. Everyone on Usenet has issues)


devel / comp.lang.c / on why declare a struct with a single array in it

SubjectAuthor
* on why declare a struct with a single array in itMeredith Montgomery
+* Re: on why declare a struct with a single array in itGuillaume
|`* Re: on why declare a struct with a single array in itBen Bacarisse
| `* Re: on why declare a struct with a single array in itDavid Brown
|  `* Re: on why declare a struct with a single array in itpozz
|   `* Re: on why declare a struct with a single array in itDavid Brown
|    `* Re: on why declare a struct with a single array in itManfred
|     `* Re: on why declare a struct with a single array in itpozz
|      +* Re: on why declare a struct with a single array in itManfred
|      |`- Re: on why declare a struct with a single array in itMeredith Montgomery
|      `* Re: on why declare a struct with a single array in itDavid Brown
|       `* Re: on why declare a struct with a single array in itGuillaume
|        `* Re: on why declare a struct with a single array in itKeith Thompson
|         `* Re: on why declare a struct with a single array in itScott Lurndal
|          +* Re: on why declare a struct with a single array in itKeith Thompson
|          |+* Re: on why declare a struct with a single array in itDavid Brown
|          ||+- Re: on why declare a struct with a single array in itBart
|          ||`* Re: on why declare a struct with a single array in itKeith Thompson
|          || +* Re: on why declare a struct with a single array in itBart
|          || |+* Re: on why declare a struct with a single array in itRichard Damon
|          || ||`* Re: on why declare a struct with a single array in itDavid Brown
|          || || +* Re: on why declare a struct with a single array in itManfred
|          || || |`* Re: on why declare a struct with a single array in itDavid Brown
|          || || | +* Re: on why declare a struct with a single array in itManfred
|          || || | |+* Re: on why declare a struct with a single array in itDavid Brown
|          || || | ||`* Re: on why declare a struct with a single array in itMalcolm McLean
|          || || | || `- Re: on why declare a struct with a single array in itDavid Brown
|          || || | |+- Re: on why declare a struct with a single array in itBart
|          || || | |`* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | +* Re: on why declare a struct with a single array in itBart
|          || || | | |+- Re: on why declare a struct with a single array in itDavid Brown
|          || || | | |`* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | | +* Re: on why declare a struct with a single array in itBart
|          || || | | | |+- Re: on why declare a struct with a single array in itBart
|          || || | | | |`* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | | | `- Re: on why declare a struct with a single array in itBart
|          || || | | | +- Re: on why declare a struct with a single array in itChris M. Thomasson
|          || || | | | `* Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |  `* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | |   `* Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |    +* Re: on why declare a struct with a single array in itDick
|          || || | | |    |+* Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |    ||`- Re: on why declare a struct with a single array in itMalcolm McLean
|          || || | | |    |+- Re: on why declare a struct with a single array in itDavid Brown
|          || || | | |    |`* Re: on why declare a struct with a single array in itRobert Latest
|          || || | | |    | `- Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |    `* Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | |     +* Re: on why declare a struct with a single array in itKenny McCormack
|          || || | | |     |`- Re: on why declare a struct with a single array in itTim Rentsch
|          || || | | |     `- Re: on why declare a struct with a single array in itKeith Thompson
|          || || | | `- Re: on why declare a struct with a single array in itKeith Thompson
|          || || | `* Re: on why declare a struct with a single array in itGuillaume
|          || || |  `* Re: on why declare a struct with a single array in itBart
|          || || |   `- Re: on why declare a struct with a single array in itKeith Thompson
|          || || `- Re: on why declare a struct with a single array in itChris M. Thomasson
|          || |+* Re: on why declare a struct with a single array in itRichard Damon
|          || ||`- Re: on why declare a struct with a single array in itBart
|          || |+* Re: on why declare a struct with a single array in itJoe Pfeiffer
|          || ||`* Re: on why declare a struct with a single array in itBart
|          || || +* Re: on why declare a struct with a single array in itJoe Pfeiffer
|          || || |`- Re: on why declare a struct with a single array in itBart
|          || || `- Re: on why declare a struct with a single array in itChris M. Thomasson
|          || |`- Re: on why declare a struct with a single array in itBart
|          || +* Re: on why declare a struct with a single array in itGuillaume
|          || |+- Re: on why declare a struct with a single array in itBart
|          || |`- Re: on why declare a struct with a single array in itChris M. Thomasson
|          || `* Re: on why declare a struct with a single array in itDavid Brown
|          ||  `- Re: on why declare a struct with a single array in itChris M. Thomasson
|          |`- Re: on why declare a struct with a single array in itScott Lurndal
|          `- Re: on why declare a struct with a single array in itKenny McCormack
+* Re: on why declare a struct with a single array in itJorgen Grahn
|`- Re: on why declare a struct with a single array in itGuillaume
+- Re: on why declare a struct with a single array in itKaz Kylheku
`* Re: on why declare a struct with a single array in itAnton Shepelev
 `- Re: on why declare a struct with a single array in itMeredith Montgomery

Pages:123
on why declare a struct with a single array in it

<86tughjjm7.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!CjRTGRADGs778RZmUQuqAw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: on why declare a struct with a single array in it
Date: Fri, 12 Nov 2021 15:48:16 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86tughjjm7.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="51710"; posting-host="CjRTGRADGs778RZmUQuqAw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:V3AGnaTmcLnqyiANzi0DYqNY3CQ=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Fri, 12 Nov 2021 18:48 UTC

Why would someone put a lonely array inside a structure?

struct ip_address { unsigned char d[4]; };

Would it be because they want to (in the future) add more members to
this structure? Because it's not like they can hide this array in the
structure; it's still referenced as

struct ip_address ip;
ip.d[0] = ...
ip.d[1] = ...

But it does look neat, so I guess neatness is the reason? It's also
easier to read because we're constantly reminded it is an ip address?

Re: on why declare a struct with a single array in it

<smmdcc$1n62$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!5Eb5Kjev2xuaeX7DMmY97A.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 12 Nov 2021 19:56:11 +0100
Organization: Aioe.org NNTP Server
Message-ID: <smmdcc$1n62$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="56514"; posting-host="5Eb5Kjev2xuaeX7DMmY97A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Fri, 12 Nov 2021 18:56 UTC

Le 12/11/2021 à 19:48, Meredith Montgomery a écrit :
> Why would someone put a lonely array inside a structure?
>
> struct ip_address { unsigned char d[4]; };
>
> Would it be because they want to (in the future) add more members to
> this structure?

That can be a good reason. Anticipating makes for much more maintainable
code.

But another reason here is that it's a way to make it possible to copy
arrays. In C, you can't copy arrays.

unsigned char d1[4], d2[4];

d2 = d1; // invalid C

You have to either copy item by item in a loop, or use memcpy() or
something similar.

If you encapsulate an array in a struct, now you can do it more elegantly:

struct ip_address ip1, ip2;

ip2 = ip1; // perfectly valid C, and will copy the whole array

Of course, if you add members to the struct, it still works and will
copy all members.

Re: on why declare a struct with a single array in it

<87pmr5qih6.fsf@bsb.me.uk>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: ben.use...@bsb.me.uk (Ben Bacarisse)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 12 Nov 2021 19:31:01 +0000
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <87pmr5qih6.fsf@bsb.me.uk>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="ded9b6065004ab925554d8d4df700871";
logging-data="24168"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+d2Y4SKiDpKyxaruzqxWLsP3BNaQLvsXk="
Cancel-Lock: sha1:m31ciipvNwi0KpHrfprcRR5Zgrs=
sha1:n2pxkZd8CjLo/MvBmOMow5zMPWo=
X-BSB-Auth: 1.af2735d95e04e539ee2b.20211112193101GMT.87pmr5qih6.fsf@bsb.me.uk
 by: Ben Bacarisse - Fri, 12 Nov 2021 19:31 UTC

Guillaume <message@bottle.org> writes:

> Le 12/11/2021 à 19:48, Meredith Montgomery a écrit :
>> Why would someone put a lonely array inside a structure?
>> struct ip_address { unsigned char d[4]; };
>> Would it be because they want to (in the future) add more members to
>> this structure?
>
> That can be a good reason. Anticipating makes for much more maintainable code.
>
> But another reason here is that it's a way to make it possible to copy
> arrays. In C, you can't copy arrays.
>
> unsigned char d1[4], d2[4];
>
> d2 = d1; // invalid C
>
> You have to either copy item by item in a loop, or use memcpy() or
> something similar.
>
> If you encapsulate an array in a struct, now you can do it more elegantly:
>
> struct ip_address ip1, ip2;
>
> ip2 = ip1; // perfectly valid C, and will copy the whole array

And a special case of copying is passing values to a function. That
might even be the /main/ reason for putting an array into a struct,
though only the code's author really knows.

--
Ben.

Re: on why declare a struct with a single array in it

<smmhhg$bfh$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 12 Nov 2021 21:07:12 +0100
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <smmhhg$bfh$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Fri, 12 Nov 2021 20:07:12 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="41f6da1ec58c2d62b4ed23d2a8f0d64f";
logging-data="11761"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+7E+2hY0k84PELgwqWMAHY/D9gSyE01q4="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:gyXGc4Eva/SHpKbxEvkMPqBgwrM=
In-Reply-To: <87pmr5qih6.fsf@bsb.me.uk>
Content-Language: en-GB
 by: David Brown - Fri, 12 Nov 2021 20:07 UTC

On 12/11/2021 20:31, Ben Bacarisse wrote:
> Guillaume <message@bottle.org> writes:
>
>> Le 12/11/2021 à 19:48, Meredith Montgomery a écrit :
>>> Why would someone put a lonely array inside a structure?
>>> struct ip_address { unsigned char d[4]; };
>>> Would it be because they want to (in the future) add more members to
>>> this structure?
>>
>> That can be a good reason. Anticipating makes for much more maintainable code.
>>
>> But another reason here is that it's a way to make it possible to copy
>> arrays. In C, you can't copy arrays.
>>
>> unsigned char d1[4], d2[4];
>>
>> d2 = d1; // invalid C
>>
>> You have to either copy item by item in a loop, or use memcpy() or
>> something similar.
>>
>> If you encapsulate an array in a struct, now you can do it more elegantly:
>>
>> struct ip_address ip1, ip2;
>>
>> ip2 = ip1; // perfectly valid C, and will copy the whole array
>
> And a special case of copying is passing values to a function. That
> might even be the /main/ reason for putting an array into a struct,
> though only the code's author really knows.
>

That is certainly one reason why I do it. Basically, it means you have
a "value" type that does not decay to a pointer. It also means the size
is fixed and available via "sizeof" as it is passed around, and perhaps
most importantly it is a specific type. The "struct ip_address" here is
a named type that cannot be confused with any other array of unsigned
char, nor a pointer to an unsigned char.

Re: on why declare a struct with a single array in it

<slrnsouusf.1rfm.grahn+nntp@frailea.sa.invalid>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: grahn+n...@snipabacken.se (Jorgen Grahn)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: 13 Nov 2021 08:47:11 GMT
Lines: 54
Message-ID: <slrnsouusf.1rfm.grahn+nntp@frailea.sa.invalid>
References: <86tughjjm7.fsf@levado.to>
X-Trace: individual.net Ild5vQOq4DhI/CQxhIAJWwIP1y9FtxoX4m1LgHuuEV33M/wEET
Cancel-Lock: sha1:JxNzLiD64BWqkIJFM0uWd3i+vDE=
User-Agent: slrn/1.0.3 (OpenBSD)
 by: Jorgen Grahn - Sat, 13 Nov 2021 08:47 UTC

On Fri, 2021-11-12, Meredith Montgomery wrote:
> Why would someone put a lonely array inside a structure?
>
> struct ip_address { unsigned char d[4]; };

Or the official Unix IPv6 address:

struct in6_addr {
unsigned char s6_addr[16]; /* IPv6 address */
};

> Would it be because they want to (in the future) add more members to
> this structure?

Not in this case -- noone will ever change the definition of "an IPv4
address". I hope.

> Because it's not like they can hide this array in the
> structure; it's still referenced as
>
> struct ip_address ip;
> ip.d[0] = ...
> ip.d[1] = ...
>
> But it does look neat, so I guess neatness is the reason? It's also
> easier to read because we're constantly reminded it is an ip address?

Others have already replied; I will give a similar answer.

I use this pattern all the time, whenever I can, i.e. when it's ok that
all arrays are the same length[0]. The benefits are:

- I get a real, distinct type with a name, which doesn't convert or
decay to other types. struct in6_addr isn't just /any/ array of 16
unsigned chars: it's an IPv6 address.

- I get objects which work like any other objects in assignments,
function calls and so on.

There is no information hiding, but that's not a big problem IMO. Or
perhaps I have gotten used to it.

/Jorgen

[0] And when the length is variable you can sometimes do

struct foo {
struct bar v[31];
unsigned count;
};

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .

Re: on why declare a struct with a single array in it

<smosat$1qgm$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!5Eb5Kjev2xuaeX7DMmY97A.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sat, 13 Nov 2021 18:23:38 +0100
Organization: Aioe.org NNTP Server
Message-ID: <smosat$1qgm$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to>
<slrnsouusf.1rfm.grahn+nntp@frailea.sa.invalid>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="59926"; posting-host="5Eb5Kjev2xuaeX7DMmY97A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Sat, 13 Nov 2021 17:23 UTC

Le 13/11/2021 à 09:47, Jorgen Grahn a écrit :
> On Fri, 2021-11-12, Meredith Montgomery wrote:
>> Because it's not like they can hide this array in the
>> structure; it's still referenced as
(...)
> There is no information hiding, but that's not a big problem IMO. Or
> perhaps I have gotten used to it.

Actually, I didn't mention it, but encapsulating data in a struct does
allow some form of "information hiding" - if we all mean the same thing
here.

You can define "opaque" types using structs - more precisely, using
pointers to structs. It's commonly seen in standard libraries, POSIX
stuff, etc.

Of course, if you have access to the struct definition, there is no
hiding per se, but you can absolutely distribute a library with only
opaque data types for the user of the library.

'FILE *' is a common example. Do you ever care what is behind a FILE
type? It often depends on the platform anyway. You only use it through a
pointer.

What allows to hide what's inside a struct in C using pointers is the
fact you can absolutely define a pointer to a struct without defining
the struct itself:

typedef struct Foo * Foo_t;

As long as you don't need to directly access the underlying struct
members, that works plenty fine - typical use is through function calls.

Re: on why declare a struct with a single array in it

<20211113094911.939@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: 480-992-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sat, 13 Nov 2021 17:55:09 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 22
Message-ID: <20211113094911.939@kylheku.com>
References: <86tughjjm7.fsf@levado.to>
Injection-Date: Sat, 13 Nov 2021 17:55:09 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="00e5a16c2bf64635f76f9d7d9b817b74";
logging-data="12826"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18UyIkuc2qN2hYjxLWwyKVaNMAHat5eY38="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:wpg+hv7eAr1IcD1ElA/ng1IwRoQ=
 by: Kaz Kylheku - Sat, 13 Nov 2021 17:55 UTC

On 2021-11-12, Meredith Montgomery <mmontgomery@levado.to> wrote:
> Why would someone put a lonely array inside a structure?
>
> struct ip_address { unsigned char d[4]; };

The main reason is because structs provide more abstraction than
arrays in C.

You can assign them, pass them into functions and return them.

If you have

typedef struct ip_address ip_address_t;

this ip_address_t type will not do weird things, like if it were an
array typedef; it will not turn into a pointer when you use it to
declare a function parameter, or produce object definitions that
cannot be assigned.

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal

Re: on why declare a struct with a single array in it

<sn5pcl$pru$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Thu, 18 Nov 2021 15:53:09 +0100
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <sn5pcl$pru$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 18 Nov 2021 14:53:09 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3d829fb3e17ff69060ac30b30f2cf54e";
logging-data="26494"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1++8vND9JRdONIszO0I6CivT6yTOnASlfE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:JQz8eLWw3lfejnRU0WfNjiux97w=
In-Reply-To: <smmhhg$bfh$1@dont-email.me>
 by: pozz - Thu, 18 Nov 2021 14:53 UTC

Il 12/11/2021 21:07, David Brown ha scritto:
> On 12/11/2021 20:31, Ben Bacarisse wrote:
>> Guillaume <message@bottle.org> writes:
>>
>>> Le 12/11/2021 à 19:48, Meredith Montgomery a écrit :
>>>> Why would someone put a lonely array inside a structure?
>>>> struct ip_address { unsigned char d[4]; };
>>>> Would it be because they want to (in the future) add more members to
>>>> this structure?
>>>
>>> That can be a good reason. Anticipating makes for much more maintainable code.
>>>
>>> But another reason here is that it's a way to make it possible to copy
>>> arrays. In C, you can't copy arrays.
>>>
>>> unsigned char d1[4], d2[4];
>>>
>>> d2 = d1; // invalid C
>>>
>>> You have to either copy item by item in a loop, or use memcpy() or
>>> something similar.
>>>
>>> If you encapsulate an array in a struct, now you can do it more elegantly:
>>>
>>> struct ip_address ip1, ip2;
>>>
>>> ip2 = ip1; // perfectly valid C, and will copy the whole array
>>
>> And a special case of copying is passing values to a function. That
>> might even be the /main/ reason for putting an array into a struct,
>> though only the code's author really knows.
>>
>
> That is certainly one reason why I do it. Basically, it means you have
> a "value" type that does not decay to a pointer. It also means the size
> is fixed and available via "sizeof" as it is passed around, and perhaps
> most importantly it is a specific type. The "struct ip_address" here is
> a named type that cannot be confused with any other array of unsigned
> char, nor a pointer to an unsigned char.
>

Regarding your two last sentences, the same can be achieved with:

typedef unsigned char ip_address[4];

So I don't think those two last arguments are the real reason to embed a
fixed-sized array into a struct.

Re: on why declare a struct with a single array in it

<sn5qji$3ri$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Thu, 18 Nov 2021 16:13:53 +0100
Organization: A noiseless patient Spider
Lines: 70
Message-ID: <sn5qji$3ri$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 18 Nov 2021 15:13:54 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="eacba1fa919d2d9944baf4e35984ee78";
logging-data="3954"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19QKVAvrf7RQ3QEsJBJXeemVZmm1qP17mI="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:D4KKsgBLMOR1HsxcB+k9CzcLsMc=
In-Reply-To: <sn5pcl$pru$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Thu, 18 Nov 2021 15:13 UTC

On 18/11/2021 15:53, pozz wrote:
> Il 12/11/2021 21:07, David Brown ha scritto:
>> On 12/11/2021 20:31, Ben Bacarisse wrote:
>>> Guillaume <message@bottle.org> writes:
>>>
>>>> Le 12/11/2021 à 19:48, Meredith Montgomery a écrit :
>>>>> Why would someone put a lonely array inside a structure?
>>>>>     struct ip_address { unsigned char d[4]; };
>>>>> Would it be because they want to (in the future) add more members to
>>>>> this structure?
>>>>
>>>> That can be a good reason. Anticipating makes for much more
>>>> maintainable code.
>>>>
>>>> But another reason here is that it's a way to make it possible to copy
>>>> arrays. In C, you can't copy arrays.
>>>>
>>>> unsigned char d1[4], d2[4];
>>>>
>>>> d2 = d1; // invalid C
>>>>
>>>> You have to either copy item by item in a loop, or use memcpy() or
>>>> something similar.
>>>>
>>>> If you encapsulate an array in a struct, now you can do it more
>>>> elegantly:
>>>>
>>>> struct ip_address ip1, ip2;
>>>>
>>>> ip2 = ip1; // perfectly valid C, and will copy the whole array
>>>
>>> And a special case of copying is passing values to a function.  That
>>> might even be the /main/ reason for putting an array into a struct,
>>> though only the code's author really knows.
>>>
>>
>> That is certainly one reason why I do it.  Basically, it means you have
>> a "value" type that does not decay to a pointer.  It also means the size
>> is fixed and available via "sizeof" as it is passed around, and perhaps
>> most importantly it is a specific type.  The "struct ip_address" here is
>> a named type that cannot be confused with any other array of unsigned
>> char, nor a pointer to an unsigned char.
>>
>
> Regarding your two last sentences, the same can be achieved with:
>
>   typedef unsigned char ip_address[4];
>
> So I don't think those two last arguments are the real reason to embed a
> fixed-sized array into a struct.
>

No, it is not the same thing at all.

Try compiling:

typedef unsigned char ip_address[4];
ip_address a;
unsigned char * p = a;

Since "a" is just an array of unsigned char, most uses of it in an
expression decay to a pointer to the first element. "typedef", despite
the name, does not define a type - it gives an alias to an existing
type. If you want to make a new type so that pointers to it are
incompatible with other pointer types, you need to put it in a struct
(or a union, but that might seem a little odd). And wrapping it in a
struct (or union) is the only way to stop this decay, so that the type
retains its value and size even when passed into and out of functions.

Re: on why declare a struct with a single array in it

<sn5rc0$1i81$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Thu, 18 Nov 2021 16:26:53 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sn5rc0$1i81$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="51457"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Content-Language: en-US
X-Notice: Filtered by postfilter v. 0.9.2
 by: Manfred - Thu, 18 Nov 2021 15:26 UTC

On 11/18/2021 4:13 PM, David Brown wrote:
> On 18/11/2021 15:53, pozz wrote:
>> Il 12/11/2021 21:07, David Brown ha scritto:
>>> On 12/11/2021 20:31, Ben Bacarisse wrote:
>>>> Guillaume <message@bottle.org> writes:
>>>>
>>>>> Le 12/11/2021 à 19:48, Meredith Montgomery a écrit :
>>>>>> Why would someone put a lonely array inside a structure?
>>>>>>     struct ip_address { unsigned char d[4]; };
>>>>>> Would it be because they want to (in the future) add more members to
>>>>>> this structure?
>>>>>
>>>>> That can be a good reason. Anticipating makes for much more
>>>>> maintainable code.
>>>>>
>>>>> But another reason here is that it's a way to make it possible to copy
>>>>> arrays. In C, you can't copy arrays.
>>>>>
>>>>> unsigned char d1[4], d2[4];
>>>>>
>>>>> d2 = d1; // invalid C
>>>>>
>>>>> You have to either copy item by item in a loop, or use memcpy() or
>>>>> something similar.
>>>>>
>>>>> If you encapsulate an array in a struct, now you can do it more
>>>>> elegantly:
>>>>>
>>>>> struct ip_address ip1, ip2;
>>>>>
>>>>> ip2 = ip1; // perfectly valid C, and will copy the whole array
>>>>
>>>> And a special case of copying is passing values to a function.  That
>>>> might even be the /main/ reason for putting an array into a struct,
>>>> though only the code's author really knows.
>>>>
>>>
>>> That is certainly one reason why I do it.  Basically, it means you have
>>> a "value" type that does not decay to a pointer.  It also means the size
>>> is fixed and available via "sizeof" as it is passed around, and perhaps
>>> most importantly it is a specific type.  The "struct ip_address" here is
>>> a named type that cannot be confused with any other array of unsigned
>>> char, nor a pointer to an unsigned char.
>>>
>>
>> Regarding your two last sentences, the same can be achieved with:
>>
>>   typedef unsigned char ip_address[4];
>>
>> So I don't think those two last arguments are the real reason to embed a
>> fixed-sized array into a struct.
>>
>
> No, it is not the same thing at all.
>
>
> Try compiling:
>
> typedef unsigned char ip_address[4];
> ip_address a;
> unsigned char * p = a;
>
> Since "a" is just an array of unsigned char, most uses of it in an
> expression decay to a pointer to the first element. "typedef", despite
> the name, does not define a type - it gives an alias to an existing
> type. If you want to make a new type so that pointers to it are
> incompatible with other pointer types, you need to put it in a struct
> (or a union, but that might seem a little odd). And wrapping it in a
> struct (or union) is the only way to stop this decay, so that the type
> retains its value and size even when passed into and out of functions.
>

Even more:

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

typedef unsigned char ip_address[4];

void foo(ip_address a)
{ printf("sizeof(a) = %zu\n", sizeof(a)); // sizeof(a) returns
sizeof(unsigned char*)
}

int main(void)
{ unsigned char a[] = { 1, 2, 3, 4 };

foo(a); // unsigned char[4] is valid here
}

$ gcc -std=c11 -O2 -Wall -Wextra ip_address_array_size.c && ./a.out
ip_address_array_size.c: In function ‘foo’:
ip_address_array_size.c:7:37: warning: ‘sizeof’ on array function
parameter ‘a’ will return size of ‘unsigned char *’
[-Wsizeof-array-argument]
7 | printf("sizeof(a) = %zu\n", sizeof(a)); // sizeof(a) returns
sizeof(unsigned char*)
| ^
ip_address_array_size.c:5:21: note: declared here
5 | void foo(ip_address a)
| ~~~~~~~~~~~^
sizeof(a) = 8

Re: on why declare a struct with a single array in it

<sn5ukn$2ir$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: pozzu...@gmail.com (pozz)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Thu, 18 Nov 2021 17:22:47 +0100
Organization: A noiseless patient Spider
Lines: 113
Message-ID: <sn5ukn$2ir$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 18 Nov 2021 16:22:47 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="3d829fb3e17ff69060ac30b30f2cf54e";
logging-data="2651"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX195YgiuGAqLevbB3bOTk4hyLvM5HGFo/sg="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:HTrCxVLJUEHiL/AviejgQtpnGXk=
In-Reply-To: <sn5rc0$1i81$1@gioia.aioe.org>
 by: pozz - Thu, 18 Nov 2021 16:22 UTC

Il 18/11/2021 16:26, Manfred ha scritto:
> On 11/18/2021 4:13 PM, David Brown wrote:
>> On 18/11/2021 15:53, pozz wrote:
>>> Il 12/11/2021 21:07, David Brown ha scritto:
>>>> On 12/11/2021 20:31, Ben Bacarisse wrote:
>>>>> Guillaume <message@bottle.org> writes:
>>>>>
>>>>>> Le 12/11/2021 à 19:48, Meredith Montgomery a écrit :
>>>>>>> Why would someone put a lonely array inside a structure?
>>>>>>>      struct ip_address { unsigned char d[4]; };
>>>>>>> Would it be because they want to (in the future) add more members to
>>>>>>> this structure?
>>>>>>
>>>>>> That can be a good reason. Anticipating makes for much more
>>>>>> maintainable code.
>>>>>>
>>>>>> But another reason here is that it's a way to make it possible to
>>>>>> copy
>>>>>> arrays. In C, you can't copy arrays.
>>>>>>
>>>>>> unsigned char d1[4], d2[4];
>>>>>>
>>>>>> d2 = d1; // invalid C
>>>>>>
>>>>>> You have to either copy item by item in a loop, or use memcpy() or
>>>>>> something similar.
>>>>>>
>>>>>> If you encapsulate an array in a struct, now you can do it more
>>>>>> elegantly:
>>>>>>
>>>>>> struct ip_address ip1, ip2;
>>>>>>
>>>>>> ip2 = ip1; // perfectly valid C, and will copy the whole array
>>>>>
>>>>> And a special case of copying is passing values to a function.  That
>>>>> might even be the /main/ reason for putting an array into a struct,
>>>>> though only the code's author really knows.
>>>>>
>>>>
>>>> That is certainly one reason why I do it.  Basically, it means you have
>>>> a "value" type that does not decay to a pointer.  It also means the
>>>> size
>>>> is fixed and available via "sizeof" as it is passed around, and perhaps
>>>> most importantly it is a specific type.  The "struct ip_address"
>>>> here is
>>>> a named type that cannot be confused with any other array of unsigned
>>>> char, nor a pointer to an unsigned char.
>>>>
>>>
>>> Regarding your two last sentences, the same can be achieved with:
>>>
>>>    typedef unsigned char ip_address[4];
>>>
>>> So I don't think those two last arguments are the real reason to embed a
>>> fixed-sized array into a struct.
>>>
>>
>> No, it is not the same thing at all.
>>
>>
>> Try compiling:
>>
>>     typedef unsigned char ip_address[4];
>>     ip_address a;
>>     unsigned char * p = a;
>>
>> Since "a" is just an array of unsigned char, most uses of it in an
>> expression decay to a pointer to the first element.  "typedef", despite
>> the name, does not define a type - it gives an alias to an existing
>> type.  If you want to make a new type so that pointers to it are
>> incompatible with other pointer types, you need to put it in a struct
>> (or a union, but that might seem a little odd).  And wrapping it in a
>> struct (or union) is the only way to stop this decay, so that the type
>> retains its value and size even when passed into and out of functions.
>>
>
> Even more:
>
>
> $ cat ip_address_array_size.c
> #include <stdio.h>
>
> typedef unsigned char ip_address[4];
>
> void foo(ip_address a)
> {
>   printf("sizeof(a) = %zu\n", sizeof(a)); // sizeof(a) returns
> sizeof(unsigned char*)
> }
>
> int main(void)
> {
>   unsigned char a[] = { 1, 2, 3, 4 };
>
>   foo(a); // unsigned char[4] is valid here
> }
>
> $ gcc -std=c11 -O2 -Wall -Wextra ip_address_array_size.c && ./a.out
> ip_address_array_size.c: In function ‘foo’:
> ip_address_array_size.c:7:37: warning: ‘sizeof’ on array function
> parameter ‘a’ will return size of ‘unsigned char *’
> [-Wsizeof-array-argument]
>     7 |   printf("sizeof(a) = %zu\n", sizeof(a)); // sizeof(a) returns
> sizeof(unsigned char*)
>       |                                     ^
> ip_address_array_size.c:5:21: note: declared here
>     5 | void foo(ip_address a)
>       |          ~~~~~~~~~~~^
> sizeof(a) = 8

Ok, my shame, you're right.

Next time I will think twice before repling.

Re: on why declare a struct with a single array in it

<sn6134$i63$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!Puiiztk9lHEEQC0y3uUjRA.user.46.165.242.75.POSTED!not-for-mail
From: non...@add.invalid (Manfred)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Thu, 18 Nov 2021 18:04:35 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sn6134$i63$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="18627"; posting-host="Puiiztk9lHEEQC0y3uUjRA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: en-US
 by: Manfred - Thu, 18 Nov 2021 17:04 UTC

On 11/18/2021 5:22 PM, pozz wrote:
> Il 18/11/2021 16:26, Manfred ha scritto:
>> On 11/18/2021 4:13 PM, David Brown wrote:
>>> On 18/11/2021 15:53, pozz wrote:
>>>> Il 12/11/2021 21:07, David Brown ha scritto:
>>>>> On 12/11/2021 20:31, Ben Bacarisse wrote:
>>>>>> Guillaume <message@bottle.org> writes:
>>>>>>
>>>>>>> Le 12/11/2021 à 19:48, Meredith Montgomery a écrit :
>>>>>>>> Why would someone put a lonely array inside a structure?
>>>>>>>>      struct ip_address { unsigned char d[4]; };
>>>>>>>> Would it be because they want to (in the future) add more
>>>>>>>> members to
>>>>>>>> this structure?
>>>>>>>
>>>>>>> That can be a good reason. Anticipating makes for much more
>>>>>>> maintainable code.
>>>>>>>
>>>>>>> But another reason here is that it's a way to make it possible to
>>>>>>> copy
>>>>>>> arrays. In C, you can't copy arrays.
>>>>>>>
>>>>>>> unsigned char d1[4], d2[4];
>>>>>>>
>>>>>>> d2 = d1; // invalid C
>>>>>>>
>>>>>>> You have to either copy item by item in a loop, or use memcpy() or
>>>>>>> something similar.
>>>>>>>
>>>>>>> If you encapsulate an array in a struct, now you can do it more
>>>>>>> elegantly:
>>>>>>>
>>>>>>> struct ip_address ip1, ip2;
>>>>>>>
>>>>>>> ip2 = ip1; // perfectly valid C, and will copy the whole array
>>>>>>
>>>>>> And a special case of copying is passing values to a function.  That
>>>>>> might even be the /main/ reason for putting an array into a struct,
>>>>>> though only the code's author really knows.
>>>>>>
>>>>>
>>>>> That is certainly one reason why I do it.  Basically, it means you
>>>>> have
>>>>> a "value" type that does not decay to a pointer.  It also means the
>>>>> size
>>>>> is fixed and available via "sizeof" as it is passed around, and
>>>>> perhaps
>>>>> most importantly it is a specific type.  The "struct ip_address"
>>>>> here is
>>>>> a named type that cannot be confused with any other array of unsigned
>>>>> char, nor a pointer to an unsigned char.
>>>>>
>>>>
>>>> Regarding your two last sentences, the same can be achieved with:
>>>>
>>>>    typedef unsigned char ip_address[4];
>>>>
>>>> So I don't think those two last arguments are the real reason to
>>>> embed a
>>>> fixed-sized array into a struct.
>>>>
>>>
>>> No, it is not the same thing at all.
>>>
>>>
>>> Try compiling:
>>>
>>>     typedef unsigned char ip_address[4];
>>>     ip_address a;
>>>     unsigned char * p = a;
>>>
>>> Since "a" is just an array of unsigned char, most uses of it in an
>>> expression decay to a pointer to the first element.  "typedef", despite
>>> the name, does not define a type - it gives an alias to an existing
>>> type.  If you want to make a new type so that pointers to it are
>>> incompatible with other pointer types, you need to put it in a struct
>>> (or a union, but that might seem a little odd).  And wrapping it in a
>>> struct (or union) is the only way to stop this decay, so that the type
>>> retains its value and size even when passed into and out of functions.
>>>
>>
>> Even more:
>>
>>
>> $ cat ip_address_array_size.c
>> #include <stdio.h>
>>
>> typedef unsigned char ip_address[4];
>>
>> void foo(ip_address a)
>> {
>>    printf("sizeof(a) = %zu\n", sizeof(a)); // sizeof(a) returns
>> sizeof(unsigned char*)
>> }
>>
>> int main(void)
>> {
>>    unsigned char a[] = { 1, 2, 3, 4 };
>>
>>    foo(a); // unsigned char[4] is valid here
>> }
>>
>> $ gcc -std=c11 -O2 -Wall -Wextra ip_address_array_size.c && ./a.out
>> ip_address_array_size.c: In function ‘foo’:
>> ip_address_array_size.c:7:37: warning: ‘sizeof’ on array function
>> parameter ‘a’ will return size of ‘unsigned char *’
>> [-Wsizeof-array-argument]
>>      7 |   printf("sizeof(a) = %zu\n", sizeof(a)); // sizeof(a)
>> returns sizeof(unsigned char*)
>>        |                                     ^
>> ip_address_array_size.c:5:21: note: declared here
>>      5 | void foo(ip_address a)
>>        |          ~~~~~~~~~~~^
>> sizeof(a) = 8
>
> Ok, my shame, you're right.
>
> Next time I will think twice before repling.

No shame, no problem.
Posts like this give a chance to clarify doubts to other readers as well.

Re: on why declare a struct with a single array in it

<sn6iam$n5t$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Thu, 18 Nov 2021 22:58:45 +0100
Organization: A noiseless patient Spider
Lines: 17
Message-ID: <sn6iam$n5t$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Thu, 18 Nov 2021 21:58:46 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="e45cbfcab8c637c3e723a35e858862c4";
logging-data="23741"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+bbwSvX4x+7Y9gp+L+cW0CTteraC4QlFw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:HZk2gOW0NijjRXy8/GsgHRG1xUs=
In-Reply-To: <sn5ukn$2ir$1@dont-email.me>
Content-Language: en-GB
 by: David Brown - Thu, 18 Nov 2021 21:58 UTC

On 18/11/2021 17:22, pozz wrote:
>
> Ok, my shame, you're right.
>
> Next time I will think twice before repling.

It is a well-known fact that the best way to get an answer on the
internet is not to post a question, but to post the wrong answer!

Arrays and typedefs can be subtle things in C, and are easily
misunderstood - they can seem to work the way you want to start with,
but other things happen when they get passed around via pointers and
function parameters. It is good to question it like you did - that's
how you learn, and can also aid others in learning. There is never a
shame in asking a (topical :-) ) question here, nor in making a mistake
and getting corrected. There's only a shame in not asking when you are
not sure, or not accepting it when a mistake is corrected.

Re: on why declare a struct with a single array in it

<20211119131443.1ff9e003959ec0d0e5db4cc7@g{oogle}mail.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: anton....@g{oogle}mail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 19 Nov 2021 13:14:43 +0300
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <20211119131443.1ff9e003959ec0d0e5db4cc7@g{oogle}mail.com>
References: <86tughjjm7.fsf@levado.to>
Mime-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader02.eternal-september.org; posting-host="a43323d89f99ea21a2a40a4dba423bb5";
logging-data="903"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18lINRFHUDUOFOkpqY8Q0AZ+thDOREzNM4="
Cancel-Lock: sha1:FdeCuy0OiNiWFKfvVKz0oniQrz8=
X-Newsreader: Sylpheed 3.5.0 (GTK+ 2.24.23; i686-pc-mingw32)
 by: Anton Shepelev - Fri, 19 Nov 2021 10:14 UTC

Meredith Montgomery:

> Why would someone put a lonely array inside a structure?
>
> struct ip_address { unsigned char d[4]; };

Here is a comment on that matter from the maintainer of the
Netpbm project. This is from a private e-mail, but I hope he
will not object to publishing of this quotation:

And one somewhat more substantial thing: t_float3
(Float3) should be a struct containing a double[] rather
than just a double[]. That way you don't have confusing
degeneration to a pointer when you pass it to a function
and it becomes obvious what the outputs of a function are
(because they're explicit pointers to the struct).

--
() ascii ribbon campaign - against html e-mail
/\ http://preview.tinyurl.com/qcy6mjc [archived]

Re: on why declare a struct with a single array in it

<sn8kq9$tep$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!5Eb5Kjev2xuaeX7DMmY97A.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 19 Nov 2021 17:53:22 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sn8kq9$tep$1@gioia.aioe.org>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="30169"; posting-host="5Eb5Kjev2xuaeX7DMmY97A.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Guillaume - Fri, 19 Nov 2021 16:53 UTC

Le 18/11/2021 à 22:58, David Brown a écrit :
> It is a well-known fact that the best way to get an answer on the
> internet is not to post a question, but to post the wrong answer!

Very true! :D

> Arrays and typedefs can be subtle things in C, and are easily
> misunderstood - they can seem to work the way you want to start with,
> but other things happen when they get passed around via pointers and
> function parameters.

Well arrays in C are not so subtle, but they can be surprising,
especially for beginners.

You raise two points here:

1. Arrays are compatible with pointers of the same base type, but since
C pointers are dumb things that are nothing like "fat" pointers, they
can't possibly hold any other attribute than a memory location. Thus,
obviously a pointer can't hold the 'size' of the pointed location. So
this point should not be particularly confusing, even to beginners. In
particular, sizeof of a pointer is nothing else than the size of the
pointer itself. Always. What you assign to the pointer doesn's matter,
even if it's an array.

Now there are kind of exceptions to that, but that are purely
"hand-implemented" and have nothing to do with the language itself: for
instance, the typical malloc() behavior. Behind the scenes, pointers
returned by malloc() are "fat pointers", and you can also implement your
own fat pointers if you so wish. But the language doesn't care. It has
no provision for that, except you can do pointer arithmetics to your
heart's content.

2. The second point is less well known and admittedly a lot more
confusing. A function parameter can be an array type, but in this
particular context, sizeof can't get the size either. That part IS
definitely confusing, and doesn't make much sense IMHO, but it has
legacy reasons. Fortunately, decent compilers warn about it, but that
helps only those who care to read warnings. Which anyone should anyway.

For this second point, I'm not sure it's strictly a problem about arrays
in C rather than a "problem" with function arguments. Now that
distinction is subtle indeed.

Re: on why declare a struct with a single array in it

<86k0h4km1j.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!A5viUP7ZdBSpM8+So1ywOw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 19 Nov 2021 16:00:56 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86k0h4km1j.fsf@levado.to>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6134$i63$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="20415"; posting-host="A5viUP7ZdBSpM8+So1ywOw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:oXLSpmxN2MsGLPLOlkQWMuMtmM8=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Fri, 19 Nov 2021 19:00 UTC

Manfred <noname@add.invalid> writes:

[...]

>> Ok, my shame, you're right.
>> Next time I will think twice before repling.
>
> No shame, no problem.
> Posts like this give a chance to clarify doubts to other readers as well.

So true!

Re: on why declare a struct with a single array in it

<86ee7cklpt.fsf@levado.to>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!rocksolid2!i2pn.org!aioe.org!A5viUP7ZdBSpM8+So1ywOw.user.46.165.242.75.POSTED!not-for-mail
From: mmontgom...@levado.to (Meredith Montgomery)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 19 Nov 2021 16:07:58 -0300
Organization: Aioe.org NNTP Server
Message-ID: <86ee7cklpt.fsf@levado.to>
References: <86tughjjm7.fsf@levado.to>
<20211119131443.1ff9e003959ec0d0e5db4cc7@g{oogle}mail.com>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: gioia.aioe.org; logging-data="26434"; posting-host="A5viUP7ZdBSpM8+So1ywOw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
Cancel-Lock: sha1:5u9PWRTwZRzEhA6/lh3ibgplLug=
X-Notice: Filtered by postfilter v. 0.9.2
 by: Meredith Montgomery - Fri, 19 Nov 2021 19:07 UTC

Anton Shepelev <anton.txt@g{oogle}mail.com> writes:

> Meredith Montgomery:
>
>> Why would someone put a lonely array inside a structure?
>>
>> struct ip_address { unsigned char d[4]; };
>
> Here is a comment on that matter from the maintainer of the
> Netpbm project. This is from a private e-mail, but I hope he
> will not object to publishing of this quotation:
>
> And one somewhat more substantial thing: t_float3
> (Float3) should be a struct containing a double[] rather
> than just a double[]. That way you don't have confusing
> degeneration to a pointer when you pass it to a function
> and it becomes obvious what the outputs of a function are
> (because they're explicit pointers to the struct).

Interesting. That seems point 2 written in Message-ID

<sn8kq9$tep$1@gioia.aioe.org>

Thanks for sharing.

Re: on why declare a struct with a single array in it

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 19 Nov 2021 11:27:25 -0800
Organization: None to speak of
Lines: 100
Message-ID: <87czmwosiq.fsf@nosuchdomain.example.com>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="1ce4a9086bdec89a73c6da8f53f5390c";
logging-data="32520"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19IdGdEeQBPgNS6r5o8j1IQ"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:OAHLjaWCH4GsP31JHeLfQZKAG0I=
sha1:/YiW2tf30iwPLuZib02HpWiZ5Ww=
 by: Keith Thompson - Fri, 19 Nov 2021 19:27 UTC

Guillaume <message@bottle.org> writes:
> Le 18/11/2021 à 22:58, David Brown a écrit :
>> It is a well-known fact that the best way to get an answer on the
>> internet is not to post a question, but to post the wrong answer!
>
> Very true! :D
>
>> Arrays and typedefs can be subtle things in C, and are easily
>> misunderstood - they can seem to work the way you want to start with,
>> but other things happen when they get passed around via pointers and
>> function parameters.
>
> Well arrays in C are not so subtle, but they can be surprising,
> especially for beginners.

The best explanation I've seen of the often confusing relationship
between arrays and pointers in C is section 6 of the comp.lang.c FAQ:
http://www.c-faq.com/

> You raise two points here:
>
> 1. Arrays are compatible with pointers of the same base type, but
> since C pointers are dumb things that are nothing like "fat" pointers,
> they can't possibly hold any other attribute than a memory
> location. Thus, obviously a pointer can't hold the 'size' of the
> pointed location. So this point should not be particularly confusing,
> even to beginners. In particular, sizeof of a pointer is nothing else
> than the size of the pointer itself. Always. What you assign to the
> pointer doesn's matter, even if it's an array.

Arrays are not "compatible" with pointers. C has a well defined concept
of type compatibility. It doesn't refer to types that can be assigned
to each other.

Every pointer value refers to a memory address *and* (other than void*)
specifies the type of the object it points to . That type information
includes the size of the target type. The size information isn't
contained in the pointer object, any more than an int object contains
the value of sizeof (int). A pointer to an *element* of an array object
doesn't tell us the size of the array object.

> Now there are kind of exceptions to that, but that are purely
> "hand-implemented" and have nothing to do with the language itself:
> for instance, the typical malloc() behavior. Behind the scenes,
> pointers returned by malloc() are "fat pointers", and you can also
> implement your own fat pointers if you so wish. But the language
> doesn't care. It has no provision for that, except you can do pointer
> arithmetics to your heart's content.

I'm not sure what you mean about malloc() returning "fat pointers".
malloc() returns a void* with no information about the size or type of
what it points to. Some implementations might provide a way to query
the size of the allocated space, but that's not standard.

> 2. The second point is less well known and admittedly a lot more
> confusing. A function parameter can be an array type, but in this
> particular context, sizeof can't get the size either. That part IS
> definitely confusing, and doesn't make much sense IMHO, but it has
> legacy reasons. Fortunately, decent compilers warn about it, but that
> helps only those who care to read warnings. Which anyone should
> anyway.
>
> For this second point, I'm not sure it's strictly a problem about
> arrays in C rather than a "problem" with function arguments. Now that
> distinction is subtle indeed.

There are two basic rules to remember.

First, an expression of array type is, in most contexts, "converted" to
a pointer expression whose value points to the initial element of the
array object. (This is a compile-time adjustment, not a run-time
conversion). The exceptions are the argument of sizeof, the argument of
unary "&", and a string literal in an initializer used to initialize an
array object; in those contexts, array expressions remain as array
expressions.

Second, a parameter defined with an array type is adjusted to a pointer
type. This:
void func(int param[42]);
is *exactly* equivalent to:
void func(int *param);

All the weird interactions between arrays and pointers, including the
behavior of multidimensional arrays (which are simply arrays of arrays),
follow from those two rules.

One consequence of these rules is that we usually manipulate array
objects via pointers to their elements, because most operations that
would act on array objects as a whole don't exist.

Something else I've found useful is to treat the words "array" and
"pointer" as adjectives, not nouns. Informally we can say "this is an
array", but it can be ambiguous. Instead, we can have an array type, an
array object, an array expression, an array value, etc. (where "an array
FOO" is "a FOO of array type" for FOO!=type).

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

Re: on why declare a struct with a single array in it

<Y2UlJ.79170$ya3.75666@fx38.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!news.swapon.de!news.uzoreto.com!feeder1.feed.usenet.farm!feed.usenet.farm!peer03.ams4!peer.am4.highwinds-media.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx38.iad.POSTED!not-for-mail
X-newsreader: xrn 9.03-beta-14-64bit
Sender: scott@dragon.sl.home (Scott Lurndal)
From: sco...@slp53.sl.home (Scott Lurndal)
Reply-To: slp53@pacbell.net
Subject: Re: on why declare a struct with a single array in it
Newsgroups: comp.lang.c
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org> <87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me> <sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me> <sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me> <sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org> <87czmwosiq.fsf@nosuchdomain.example.com>
Lines: 22
Message-ID: <Y2UlJ.79170$ya3.75666@fx38.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Fri, 19 Nov 2021 20:46:48 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Fri, 19 Nov 2021 20:46:48 GMT
X-Received-Bytes: 2071
 by: Scott Lurndal - Fri, 19 Nov 2021 20:46 UTC

Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>Guillaume <message@bottle.org> writes:
>> Le 18/11/2021 à 22:58, David Brown a écrit :

>> Now there are kind of exceptions to that, but that are purely
>> "hand-implemented" and have nothing to do with the language itself:
>> for instance, the typical malloc() behavior. Behind the scenes,
>> pointers returned by malloc() are "fat pointers", and you can also
>> implement your own fat pointers if you so wish. But the language
>> doesn't care. It has no provision for that, except you can do pointer
>> arithmetics to your heart's content.
>
>I'm not sure what you mean about malloc() returning "fat pointers".
>malloc() returns a void* with no information about the size or type of
>what it points to. Some implementations might provide a way to query
>the size of the allocated space, but that's not standard.

Many malloc implementations reserve additional space, either before
or after the allocated space for bookkeeping and alignment purposes.

Perhaps that is the source of Guillaume's "fat pointer" reference.

Re: on why declare a struct with a single array in it

<874k87q35r.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Fri, 19 Nov 2021 12:52:16 -0800
Organization: None to speak of
Lines: 34
Message-ID: <874k87q35r.fsf@nosuchdomain.example.com>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com>
<Y2UlJ.79170$ya3.75666@fx38.iad>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="1ce4a9086bdec89a73c6da8f53f5390c";
logging-data="4021"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19aCqnTTAi/sOQwLUKaaUIX"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:HX8MFftH2knhSCzyZdwkfQorG/E=
sha1:e75iQmTSgeA7L1hVe7hM7kGmCpo=
 by: Keith Thompson - Fri, 19 Nov 2021 20:52 UTC

scott@slp53.sl.home (Scott Lurndal) writes:
> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>Guillaume <message@bottle.org> writes:
>>> Le 18/11/2021 à 22:58, David Brown a écrit :
>
>>> Now there are kind of exceptions to that, but that are purely
>>> "hand-implemented" and have nothing to do with the language itself:
>>> for instance, the typical malloc() behavior. Behind the scenes,
>>> pointers returned by malloc() are "fat pointers", and you can also
>>> implement your own fat pointers if you so wish. But the language
>>> doesn't care. It has no provision for that, except you can do pointer
>>> arithmetics to your heart's content.
>>
>>I'm not sure what you mean about malloc() returning "fat pointers".
>>malloc() returns a void* with no information about the size or type of
>>what it points to. Some implementations might provide a way to query
>>the size of the allocated space, but that's not standard.
>
> Many malloc implementations reserve additional space, either before
> or after the allocated space for bookkeeping and alignment purposes.
>
> Perhaps that is the source of Guillaume's "fat pointer" reference.

Perhaps -- but typically that information is not stored in the pointer
itself. If it were, all void* values would have to store that extra
information.

(I think it's easier to store bookkeeping data before the allocated
space. If you store it after the space, it's hard to know where it is.)

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

Re: on why declare a struct with a single array in it

<snak7r$rj09$2@news.xmission.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!xmission!nnrp.xmission!.POSTED.shell.xmission.com!not-for-mail
From: gaze...@shell.xmission.com (Kenny McCormack)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sat, 20 Nov 2021 10:55:55 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <snak7r$rj09$2@news.xmission.com>
References: <86tughjjm7.fsf@levado.to> <sn8kq9$tep$1@gioia.aioe.org> <87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
Injection-Date: Sat, 20 Nov 2021 10:55:55 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="904201"; mail-complaints-to="abuse@xmission.com"
X-Newsreader: trn 4.0-test77 (Sep 1, 2010)
Originator: gazelle@shell.xmission.com (Kenny McCormack)
 by: Kenny McCormack - Sat, 20 Nov 2021 10:55 UTC

In article <Y2UlJ.79170$ya3.75666@fx38.iad>,
Scott Lurndal <slp53@pacbell.net> wrote:
....
>>I'm not sure what you mean about malloc() returning "fat pointers".
>>malloc() returns a void* with no information about the size or type of
>>what it points to. Some implementations might provide a way to query
>>the size of the allocated space, but that's not standard.
>
>Many malloc implementations reserve additional space, either before
>or after the allocated space for bookkeeping and alignment purposes.
>
>Perhaps that is the source of Guillaume's "fat pointer" reference.

That's the way I took it. That, although not in the standard, and thus
completely outside of Keith's field of vision, that on most implementations
you can "reverse engineer" how your particular implementation of malloc
(and friends) works, and figure out how to use the data contained around
the pointer value returned by malloc to figure out things like how big the
malloc'd region is.

Totally non-portable, of course, but necessary in certain situations.

--
It's possible that leasing office space to a Starbucks is a greater liability
in today's GOP than is hitting your mother on the head with a hammer.

Re: on why declare a struct with a single array in it

<snaq53$voh$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sat, 20 Nov 2021 13:36:50 +0100
Organization: A noiseless patient Spider
Lines: 38
Message-ID: <snaq53$voh$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 20 Nov 2021 12:36:51 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="97de0f5820112279033bb40994fc41ca";
logging-data="32529"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/PVJjrL93zwzBdrDaFYUMMkTxGtXryYkQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:sVQAcuBwUMs0JCiWigDJIHeT+Bw=
In-Reply-To: <874k87q35r.fsf@nosuchdomain.example.com>
Content-Language: en-GB
 by: David Brown - Sat, 20 Nov 2021 12:36 UTC

On 19/11/2021 21:52, Keith Thompson wrote:
> scott@slp53.sl.home (Scott Lurndal) writes:
>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>> Guillaume <message@bottle.org> writes:
>>>> Le 18/11/2021 à 22:58, David Brown a écrit :
>>
>>>> Now there are kind of exceptions to that, but that are purely
>>>> "hand-implemented" and have nothing to do with the language itself:
>>>> for instance, the typical malloc() behavior. Behind the scenes,
>>>> pointers returned by malloc() are "fat pointers", and you can also
>>>> implement your own fat pointers if you so wish. But the language
>>>> doesn't care. It has no provision for that, except you can do pointer
>>>> arithmetics to your heart's content.
>>>
>>> I'm not sure what you mean about malloc() returning "fat pointers".
>>> malloc() returns a void* with no information about the size or type of
>>> what it points to. Some implementations might provide a way to query
>>> the size of the allocated space, but that's not standard.
>>
>> Many malloc implementations reserve additional space, either before
>> or after the allocated space for bookkeeping and alignment purposes.
>>
>> Perhaps that is the source of Guillaume's "fat pointer" reference.
>
> Perhaps -- but typically that information is not stored in the pointer
> itself. If it were, all void* values would have to store that extra
> information.
>
> (I think it's easier to store bookkeeping data before the allocated
> space. If you store it after the space, it's hard to know where it is.)
>

That's the most common way for a simple malloc/free system.

It has always struck me as a mistake that C did things that way - in my
opinion, "free" should have taken an additional parameter of the size
for the deallocation.

Re: on why declare a struct with a single array in it

<snb1u4$ppq$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sat, 20 Nov 2021 14:49:40 +0000
Organization: A noiseless patient Spider
Lines: 61
Message-ID: <snb1u4$ppq$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Date: Sat, 20 Nov 2021 14:49:40 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1153000d80528f48a6c31e844e153496";
logging-data="26426"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19dnSKLiFlJNtuBh7SS5iKBoCPN1hJobM8="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:zahse4gkTodJPuLR3ExrrH3aePM=
In-Reply-To: <snaq53$voh$1@dont-email.me>
 by: Bart - Sat, 20 Nov 2021 14:49 UTC

On 20/11/2021 12:36, David Brown wrote:
> On 19/11/2021 21:52, Keith Thompson wrote:
>> scott@slp53.sl.home (Scott Lurndal) writes:
>>> Keith Thompson <Keith.S.Thompson+u@gmail.com> writes:
>>>> Guillaume <message@bottle.org> writes:
>>>>> Le 18/11/2021 à 22:58, David Brown a écrit :
>>>
>>>>> Now there are kind of exceptions to that, but that are purely
>>>>> "hand-implemented" and have nothing to do with the language itself:
>>>>> for instance, the typical malloc() behavior. Behind the scenes,
>>>>> pointers returned by malloc() are "fat pointers", and you can also
>>>>> implement your own fat pointers if you so wish. But the language
>>>>> doesn't care. It has no provision for that, except you can do pointer
>>>>> arithmetics to your heart's content.
>>>>
>>>> I'm not sure what you mean about malloc() returning "fat pointers".
>>>> malloc() returns a void* with no information about the size or type of
>>>> what it points to. Some implementations might provide a way to query
>>>> the size of the allocated space, but that's not standard.
>>>
>>> Many malloc implementations reserve additional space, either before
>>> or after the allocated space for bookkeeping and alignment purposes.
>>>
>>> Perhaps that is the source of Guillaume's "fat pointer" reference.
>>
>> Perhaps -- but typically that information is not stored in the pointer
>> itself. If it were, all void* values would have to store that extra
>> information.
>>
>> (I think it's easier to store bookkeeping data before the allocated
>> space. If you store it after the space, it's hard to know where it is.)
>>
>
> That's the most common way for a simple malloc/free system.
>
> It has always struck me as a mistake that C did things that way - in my
> opinion, "free" should have taken an additional parameter of the size
> for the deallocation.

Yeah, that's how I always implemented deallocations in the days when I
had to code everthing.

I was suprised when I first came across C's free which magically didn't
need that info!

Rather an indulgence for a lower level systems language.

When I go to some pains to allocate a block of exactly 4096 bytes, I
expect the next 4096-byte block to follow immediately, not be offset
from it by 4, 8 or 28 bytes or however it decides to do it.

Worse if my block is only 8 or 16 bytes:

for (int i=0; i<10; ++i) {
p=malloc(8);
printf("%d\n",(int)p);
}

The first three compilers I tried (I think all using msvcrt), used 32
bytes per allocation. 300% overhead!

Re: on why declare a struct with a single array in it

<875ysmjsfz.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sat, 20 Nov 2021 15:52:32 -0800
Organization: None to speak of
Lines: 29
Message-ID: <875ysmjsfz.fsf@nosuchdomain.example.com>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com>
<Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="a1aa0dcd5f82470090faf86702e2798a";
logging-data="27669"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+ZshqFW/nJETRCNPhJ9VFx"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:gaCqA5tUXFM98TsMfUUx+shwrd8=
sha1:hfhfTKYRIMqLX62NCm4VNMaxme0=
 by: Keith Thompson - Sat, 20 Nov 2021 23:52 UTC

David Brown <david.brown@hesbynett.no> writes:
> On 19/11/2021 21:52, Keith Thompson wrote:
[...]
>> Perhaps -- but typically that information is not stored in the pointer
>> itself. If it were, all void* values would have to store that extra
>> information.
>>
>> (I think it's easier to store bookkeeping data before the allocated
>> space. If you store it after the space, it's hard to know where it is.)
>
> That's the most common way for a simple malloc/free system.
>
> It has always struck me as a mistake that C did things that way - in my
> opinion, "free" should have taken an additional parameter of the size
> for the deallocation.

Meh. That would have introduced another rich source of programming
errors (passing the wrong size). Probably most implementations would
have kept track of the size anyway, so they could detect errors. As
long as they're keeping track, why make the user do so as well?

Perhaps a lower-level allocator might be more efficient if user code is
required to keep track, but I don't think it would be all that useful
for most applications.

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

Re: on why declare a struct with a single array in it

<snc3a0$6gs$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: on why declare a struct with a single array in it
Date: Sun, 21 Nov 2021 00:19:13 +0000
Organization: A noiseless patient Spider
Lines: 44
Message-ID: <snc3a0$6gs$1@dont-email.me>
References: <86tughjjm7.fsf@levado.to> <smmdcc$1n62$1@gioia.aioe.org>
<87pmr5qih6.fsf@bsb.me.uk> <smmhhg$bfh$1@dont-email.me>
<sn5pcl$pru$1@dont-email.me> <sn5qji$3ri$1@dont-email.me>
<sn5rc0$1i81$1@gioia.aioe.org> <sn5ukn$2ir$1@dont-email.me>
<sn6iam$n5t$1@dont-email.me> <sn8kq9$tep$1@gioia.aioe.org>
<87czmwosiq.fsf@nosuchdomain.example.com> <Y2UlJ.79170$ya3.75666@fx38.iad>
<874k87q35r.fsf@nosuchdomain.example.com> <snaq53$voh$1@dont-email.me>
<875ysmjsfz.fsf@nosuchdomain.example.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sun, 21 Nov 2021 00:19:12 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="2dd6f070aa435e5fade94a2f2235e0fb";
logging-data="6684"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+LSl+UDkIW+EM/r9/YvjQNX0fq4UUAZoo="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:91.0) Gecko/20100101
Thunderbird/91.3.1
Cancel-Lock: sha1:t1Wo2W/K6eYggP3glHTosliugok=
In-Reply-To: <875ysmjsfz.fsf@nosuchdomain.example.com>
 by: Bart - Sun, 21 Nov 2021 00:19 UTC

On 20/11/2021 23:52, Keith Thompson wrote:
> David Brown <david.brown@hesbynett.no> writes:
>> On 19/11/2021 21:52, Keith Thompson wrote:
> [...]
>>> Perhaps -- but typically that information is not stored in the pointer
>>> itself. If it were, all void* values would have to store that extra
>>> information.
>>>
>>> (I think it's easier to store bookkeeping data before the allocated
>>> space. If you store it after the space, it's hard to know where it is.)
>>
>> That's the most common way for a simple malloc/free system.
>>
>> It has always struck me as a mistake that C did things that way - in my
>> opinion, "free" should have taken an additional parameter of the size
>> for the deallocation.
>
> Meh. That would have introduced another rich source of programming
> errors (passing the wrong size).

You could also pass the wrong size to malloc, or the wrong pointer to free.

> Probably most implementations would
> have kept track of the size anyway, so they could detect errors.

I doubt that, unless it's for debugging mode.

If an application needs to allocate 100 million 32-byte nodes, which
would occupy 3.2GB by themselves, how much extra would be needed for the
size, 'just for checking'?

On my machine that seems to be 16 bytes per node, so 50% more. 1.6GB
extra just so someone can write:

free(p);

instead of:

free(p, sizeof(treenode));

possibly in just one place in the program.

It doesn't make sense.

Pages:123
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor