Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

Wernher von Braun settled for a V-2 when he coulda had a V-8.


devel / comp.lang.c / structs passed by copy

SubjectAuthor
* structs passed by copyThiago Adams
+* Re: structs passed by copyKenny McCormack
|+* Re: structs passed by copyAnton Shepelev
||+* Re: structs passed by copyKenny McCormack
|||+- Re: structs passed by copyKaz Kylheku
|||`* Re: structs passed by copyJoe Pfeiffer
||| +- Re: structs passed by copyKenny McCormack
||| `* Re: structs passed by copyKeith Thompson
|||  `* Re: structs passed by copyJames Kuyper
|||   +- Re: structs passed by copyChris M. Thomasson
|||   `* Re: structs passed by copyKeith Thompson
|||    +* Re: structs passed by copyChris M. Thomasson
|||    |`- Re: structs passed by copyChris M. Thomasson
|||    `* Re: structs passed by copyJames Kuyper
|||     `* Re: structs passed by copyKeith Thompson
|||      +- Re: structs passed by copyRichard Damon
|||      +* Re: structs passed by copyDavid Brown
|||      |`* Re: structs passed by copyKeith Thompson
|||      | `* Re: structs passed by copyDavid Brown
|||      |  `* Re: structs passed by copyMichael S
|||      |   `* Re: structs passed by copyBen Bacarisse
|||      |    `* Re: structs passed by copyMichael S
|||      |     +* Re: structs passed by copyfir
|||      |     |`* Re: structs passed by copyfir
|||      |     | `* Re: structs passed by copyfir
|||      |     |  `* Re: structs passed by copyfir
|||      |     |   `* Re: structs passed by copyfir
|||      |     |    `* Re: structs passed by copyfir
|||      |     |     +- Re: structs passed by copyfir
|||      |     |     +- Re: structs passed by copyfir
|||      |     |     `- Re: structs passed by copyfir
|||      |     `* Re: structs passed by copyBen Bacarisse
|||      |      `- Re: structs passed by copyMichael S
|||      `* Re: structs passed by copyTim Rentsch
|||       `* Re: structs passed by copyKeith Thompson
|||        +* Re: structs passed by copyJames Kuyper
|||        |`- Re: structs passed by copyKeith Thompson
|||        `* Re: structs passed by copyTim Rentsch
|||         `* Re: structs passed by copyKeith Thompson
|||          `* Re: structs passed by copyKeith Thompson
|||           `* Re: structs passed by copyPhil Carmody
|||            `* Re: structs passed by copyKeith Thompson
|||             `* Re: structs passed by copyTim Rentsch
|||              +* Re: structs passed by copyKeith Thompson
|||              |`* Re: structs passed by copyTim Rentsch
|||              | `- Re: structs passed by copyKeith Thompson
|||              `* Re: structs passed by copyJohn Forkosh
|||               +- Re: structs passed by copyKeith Thompson
|||               +- Re: structs passed by copyMichael S
|||               `- Re: structs passed by copyTim Rentsch
||`* Re: structs passed by copyJames Kuyper
|| `* Re: structs passed by copyAnton Shepelev
||  `- Re: structs passed by copyRichard Damon
|+- Re: structs passed by copyThiago Adams
|`- Re: structs passed by copyantispam
+* Re: structs passed by copyBlue-Maned_Hawk
|`- Re: structs passed by copyKeith Thompson
+- Re: structs passed by copyKeith Thompson
+* Re: structs passed by copyBart
|`* Re: structs passed by copyThiago Adams
| +- Re: structs passed by copyKeith Thompson
| `* Re: structs passed by copyBart
|  `- Re: structs passed by copyfir
+- Re: structs passed by copyRichard Damon
+* Re: structs passed by copyChris M. Thomasson
|`- Re: structs passed by copyChris M. Thomasson
+- Re: structs passed by copyDavid Brown
+- Re: structs passed by copyOpus
+* Re: structs passed by copyMichael S
|`* Re: structs passed by copyThiago Adams
| `* Re: structs passed by copyBen Bacarisse
|  +* Re: structs passed by copyfir
|  |`* Re: structs passed by copyfir
|  | `* Re: structs passed by copyfir
|  |  `* Re: structs passed by copyfir
|  |   `* Re: structs passed by copyfir
|  |    +- Re: structs passed by copyfir
|  |    `- Re: structs passed by copyfir
|  +* Re: structs passed by copyChris M. Thomasson
|  |`* Re: structs passed by copyBen Bacarisse
|  | `* Re: structs passed by copyChris M. Thomasson
|  |  `* Re: structs passed by copyBen Bacarisse
|  |   +- Re: structs passed by copyChris M. Thomasson
|  |   `* Re: structs passed by copyThiago Adams
|  |    `- Re: structs passed by copyDavid Brown
|  `* Re: structs passed by copyThiago Adams
|   `- Re: structs passed by copyBen Bacarisse
+* Re: structs passed by copyJorgen Grahn
|+* Re: structs passed by copyKeith Thompson
||+* Re: structs passed by copyÖö Tiib
|||`* Re: structs passed by copyKeith Thompson
||| +* Re: structs passed by copyJames Kuyper
||| |+* Re: structs passed by copyDavid Brown
||| ||`* Re: structs passed by copyVir Campestris
||| || `* Re: structs passed by copyDavid Brown
||| ||  `* Re: structs passed by copyThiago Adams
||| ||   `* Re: structs passed by copyThiago Adams
||| ||    `* Re: structs passed by copyDavid Brown
||| ||     `* Re: structs passed by copyThiago Adams
||| ||      `* Re: structs passed by copyDavid Brown
||| ||       `* Re: structs passed by copybart c
||| |`- Re: structs passed by copyKeith Thompson
||| +- Re: structs passed by copyÖö Tiib
||| `- Re: structs passed by copyAndrey Tarasevich
||+- Re: structs passed by copyChris M. Thomasson
||+- Grammar nitpick (Was: structs passed by copy)Kenny McCormack
||`* Re: structs passed by copyTim Rentsch
|`* Re: structs passed by copyTim Rentsch
+* Re: structs passed by copyMaciej Zelma
`* Re: structs passed by copyBonita Montero

Pages:123456
structs passed by copy

<76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:620a:134e:b0:706:49fb:8049 with SMTP id c14-20020a05620a134e00b0070649fb8049mr387322qkl.36.1674240069220;
Fri, 20 Jan 2023 10:41:09 -0800 (PST)
X-Received: by 2002:a05:6902:34f:b0:6f9:7bf9:8fc7 with SMTP id
e15-20020a056902034f00b006f97bf98fc7mr1733711ybs.279.1674240068975; Fri, 20
Jan 2023 10:41:08 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 20 Jan 2023 10:41:08 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=45.174.239.81; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 45.174.239.81
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
Subject: structs passed by copy
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Fri, 20 Jan 2023 18:41:09 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 1804
 by: Thiago Adams - Fri, 20 Jan 2023 18:41 UTC

At first edition of "The C programming language" we can see
that structs could not be passed directly as function arguments.
They also could not be returned from functions and assigned.

Soon after [1], the language changed to allow assignment and make
structs arguments passed by copy by default.

Today, I have 0% of structs being passed by copy in my code.
So for me this is a very bad default.

If structs where passed by reference this would not be the only
exception on the type system because arrays are already passed
as pointers.

Do you have structs passed by copy in your code?
Could it be replaced by const * ?
Considering your answer would you agree with me that this is a bad default?

Returning function by copy is common in my code.

[1]
https://www.bell-labs.com/usr/dmr/www/cchanges.pdf

Re: structs passed by copy

<tqeoet$2r8gf$1@news.xmission.com>

  copy mid

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

  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: structs passed by copy
Date: Fri, 20 Jan 2023 19:01:49 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tqeoet$2r8gf$1@news.xmission.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
Injection-Date: Fri, 20 Jan 2023 19:01:49 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2990607"; 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 - Fri, 20 Jan 2023 19:01 UTC

In article <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>,
Thiago Adams <thiago.adams@gmail.com> wrote:
>At first edition of "The C programming language" we can see
>that structs could not be passed directly as function arguments.
>They also could not be returned from functions and assigned.
>
>Soon after [1], the language changed to allow assignment and make
>structs arguments passed by copy by default.
>
>Today, I have 0% of structs being passed by copy in my code.
>So for me this is a very bad default.

Two comments:
1) I never really understood *why* they changed the language to allow
structs to be passed as actual objects. Anyone care to comment?
2) The usual method for passing structs around is to pass a pointer to
the struct. That works just fine in all cases and, thus, as above,
I don't understand why the change.

Like you, I've never needed (or used) this ability in my code, so have not
ever used it. I wonder if it came about because they made some other
changes (enhancements) in what you could legally do with a struct, and this
ended up coming for free.

P.S. I actually do kinda like the idea that you can pass an array "by
value" (should you want/need to do that) by wrapping the array inside a
struct and passing the struct. A cute hack that could prove useful some
sunny day...

--
"We should always be disposed to believe that which appears to us to be
white is really black, if the hierarchy of the church so decides."

- Saint Ignatius Loyola (1491-1556) Founder of the Jesuit Order -

Re: structs passed by copy

<tqeosg$26gtj$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: bluemane...@gmail.com (Blue-Maned_Hawk)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Fri, 20 Jan 2023 14:09:04 -0500
Organization: A noiseless patient Spider
Lines: 43
Message-ID: <tqeosg$26gtj$1@dont-email.me>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
MIME-Version: 1.0
Content-Type: multipart/signed; micalg=pgp-sha256;
protocol="application/pgp-signature";
boundary="------------kNXklUnkxIS74xEzr9zY0ma5"
Injection-Date: Fri, 20 Jan 2023 19:09:05 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="ed9547b48bdcd8df51cb1feb3f074530";
logging-data="2311091"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/wMi0AVw+7r1BmlKorEren93RW5lQSvSM="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Cancel-Lock: sha1:gNrIg8zipRUPn+bA6+yC+bkteHU=
In-Reply-To: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
Content-Language: en-US
 by: Blue-Maned_Hawk - Fri, 20 Jan 2023 19:09 UTC
Attachments: "OpenPGP_signature" (application/pgp-signature)

On 1/20/23 13:41, Thiago Adams wrote:
> <snip/>
>
> If structs where passed by reference this would not be the only
> exception on the type system because arrays are already passed
> as pointers.
>
> <snip/>
​The only reason arrays are the exception is because arrays aren't a
first-class construct, and are just a syntactic wrapper around pointers
to contiguous storage.
--
⚗︎ | /blu.mɛin.dʰak/ | shortens to "Hawk" | he/him/his/himself/Mr.
bluemanedhawk.github.io
Apologies if this message got encoded into octoctal. Blame Thunderbird
for not behaving correctly with Unicode and PGP.

Attachments: "OpenPGP_signature" (application/pgp-signature)
Re: structs passed by copy

<873585j9k2.fsf@nosuchdomain.example.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Fri, 20 Jan 2023 11:22:05 -0800
Organization: None to speak of
Lines: 36
Message-ID: <873585j9k2.fsf@nosuchdomain.example.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="356714a9dd42e22ebdc83653058250ea";
logging-data="2315865"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ctO7XLbq5fiig96TfyFk4"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:UYt0fEGGg/G5/h7X03wwLKOXZF8=
sha1:wxFopiMyC5SVasAXXM3VcQav4MA=
 by: Keith Thompson - Fri, 20 Jan 2023 19:22 UTC

Thiago Adams <thiago.adams@gmail.com> writes:

> At first edition of "The C programming language" we can see
> that structs could not be passed directly as function arguments.
> They also could not be returned from functions and assigned.
>
> Soon after [1], the language changed to allow assignment and make
> structs arguments passed by copy by default.
>
> Today, I have 0% of structs being passed by copy in my code.
> So for me this is a very bad default.
>
> If structs where passed by reference this would not be the only
> exception on the type system because arrays are already passed
> as pointers.
>
> Do you have structs passed by copy in your code?
> Could it be replaced by const * ?
> Considering your answer would you agree with me that this is a bad default?
>
> Returning function by copy is common in my code.
>
> [1]
> https://www.bell-labs.com/usr/dmr/www/cchanges.pdf

All function arguments are passed by copy. Arrays are not an exception
to that rule because they're passed by pointer. They're an exception
because there are no arguments of array type. There is some syntactic
sugar that can make it look like arrays are being passed by reference,
but that's not what's happening.

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

Re: structs passed by copy

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Fri, 20 Jan 2023 11:26:47 -0800
Organization: None to speak of
Lines: 32
Message-ID: <87y1pxhurs.fsf@nosuchdomain.example.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqeosg$26gtj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader01.eternal-september.org; posting-host="356714a9dd42e22ebdc83653058250ea";
logging-data="2315865"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1878LlXB/BoPiZwQ7xb7i2k"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:7xeNnlUqQW7YoNTuFCJcc42TLRc=
sha1:slQFZXHUmB4e493RD08+1M44eP0=
 by: Keith Thompson - Fri, 20 Jan 2023 19:26 UTC

Blue-Maned_Hawk <bluemanedhawk@gmail.com> writes:
> On 1/20/23 13:41, Thiago Adams wrote:
>> <snip/>
>> If structs where passed by reference this would not be the only
>> exception on the type system because arrays are already passed
>> as pointers.
>> <snip/>
>
> ​The only reason arrays are the exception is because arrays aren't a
> first-class construct, and are just a syntactic wrapper around
> pointers to contiguous storage.

That's incorrect.

Array objects and array types are entirely distinct from pointer objects
and pointer types.

An array *expressions* is, in most but not all contexts, implicitly
"converted" to a pointer to the array object's 0th element. And a
function parameter of array type is "adjusted" to a parameter of pointer
type. Together, these rules can make it seem like array objects are
being passed by reference, but that's not what's really happening.

I find it useful to treat the words "array" and "pointer" as adjectives,
not nounds: "array object", not just "array".

See section 6 of the comp.lang.c FAQ, <https://www.c-faq.com/>.

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

Re: structs passed by copy

<20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Fri, 20 Jan 2023 23:01:38 +0300
Organization: A noiseless patient Spider
Lines: 15
Message-ID: <20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqeoet$2r8gf$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader01.eternal-september.org; posting-host="a09a571e0f651c2e89ce86fbc4b6f250";
logging-data="2326669"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19OqJKOdlms5oM2n+58s9s/aGDZE5YCxBg="
Cancel-Lock: sha1:pkWHmiLgojEwCIAWO25ZDGPjrI4=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Fri, 20 Jan 2023 20:01 UTC

Kenny McCormack:

> I never really understood *why* they changed the language
> to allow structs to be passed as actual objects. Anyone
> care to comment?

Integers, floats, and doubles can be passed by value. If I
were to implement complex numbers, I should do it as a
struct with a .re (real) and .im (imaginary) fields. I
should want to pass it by value whenever I pass real types
by value.

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: structs passed by copy

<tqesrc$i48$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!uabYU4OOdxBKlV2hpj27FQ.user.46.165.242.75.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Fri, 20 Jan 2023 20:16:44 +0000
Organization: Aioe.org NNTP Server
Message-ID: <tqesrc$i48$1@gioia.aioe.org>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="18568"; posting-host="uabYU4OOdxBKlV2hpj27FQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
X-Notice: Filtered by postfilter v. 0.9.2
 by: Bart - Fri, 20 Jan 2023 20:16 UTC

On 20/01/2023 18:41, Thiago Adams wrote:
> At first edition of "The C programming language" we can see
> that structs could not be passed directly as function arguments.
> They also could not be returned from functions and assigned.
>
> Soon after [1], the language changed to allow assignment and make
> structs arguments passed by copy by default.
>
> Today, I have 0% of structs being passed by copy in my code.
> So for me this is a very bad default.
>
> If structs where passed by reference this would not be the only
> exception on the type system because arrays are already passed
> as pointers.

If S is a struct instance, and A is an array instance, then here:

S; // is the struct itself; not a reference;
A; // is a pointer to A[0]; not the array itself

So the default is already part of the language, and exists in nearly
every place where you use S and A within an expression.

Including expressions like f(S) and S=g().

So to me it makes sense.

Bear in mind that on modern ABIs, most structs tend to be passed by
reference behind the scenes anyway. Although it may still require some
copying to protect the caller's original data if that is at risk.

>
> Do you have structs passed by copy in your code?
> Could it be replaced by const * ?
> Considering your answer would you agree with me that this is a bad default?
>
> Returning function by copy is common in my code.

If you use libraries like Raylib, then pretty much every struct is
passed and return by-value, or notionally by-value.

Like you I prefer by-reference, but I also prefer that that was explicit
in the language.

(My own language manipulates arrays by-value too. For some decades I had
structs and arrays passed by actual copying, but I NEVER used that
feature. That is still the case, but now behind the scenes it uses
pointers. I still use explicit pointer-to or by-reference passing modes.)

Re: structs passed by copy

<tqf121$2re3i$1@news.xmission.com>

  copy mid

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

  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: structs passed by copy
Date: Fri, 20 Jan 2023 21:28:33 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tqf121$2re3i$1@news.xmission.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com> <tqeoet$2r8gf$1@news.xmission.com> <20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
Injection-Date: Fri, 20 Jan 2023 21:28:33 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="2996338"; 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 - Fri, 20 Jan 2023 21:28 UTC

In article <20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>,
Anton Shepelev <anton.txt@gmail.com> wrote:
>Kenny McCormack:
>
>> I never really understood *why* they changed the language
>> to allow structs to be passed as actual objects. Anyone
>> care to comment?
>
>Integers, floats, and doubles can be passed by value. If I
>were to implement complex numbers, I should do it as a
>struct with a .re (real) and .im (imaginary) fields. I
>should want to pass it by value whenever I pass real types
>by value.

I get that, for symmetry's sake and all, but wouldn't it be just as easy to
pass the address of your struct? Like we do with most of the structs that
get passed to Unix API calls (e.g., stat(2)).

By the way, didn't they already add complex numbers to the standard,
somewhere along the way? Having never used them, I can't remember, off
hand, how they are implemented.

--
Joni Ernst (2014): Obama should be impeached because 2 people have died of Ebola.
Joni Ernst (2020): Trump is doing great things, because only 65,000 times as many people have died of COVID-19.

Josef Stalin (1947): When one person dies, it is a tragedy; when a million die, it is merely statistics.

Re: structs passed by copy

<6787515f-559e-48ff-b363-4ed61e4ed51fn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:50c1:0:b0:534:6c47:849b with SMTP id e1-20020ad450c1000000b005346c47849bmr501643qvq.60.1674267122726;
Fri, 20 Jan 2023 18:12:02 -0800 (PST)
X-Received: by 2002:a81:7047:0:b0:4bc:e7dc:16bc with SMTP id
l68-20020a817047000000b004bce7dc16bcmr1962585ywc.368.1674267122429; Fri, 20
Jan 2023 18:12:02 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 20 Jan 2023 18:12:02 -0800 (PST)
In-Reply-To: <tqeoet$2r8gf$1@news.xmission.com>
Injection-Info: google-groups.googlegroups.com; posting-host=45.174.239.81; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 45.174.239.81
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com> <tqeoet$2r8gf$1@news.xmission.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <6787515f-559e-48ff-b363-4ed61e4ed51fn@googlegroups.com>
Subject: Re: structs passed by copy
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Sat, 21 Jan 2023 02:12:02 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2707
 by: Thiago Adams - Sat, 21 Jan 2023 02:12 UTC

On Friday, January 20, 2023 at 4:02:59 PM UTC-3, Kenny McCormack wrote:
> In article <76ec6d87-0f5d-4671...@googlegroups.com>,
> Thiago Adams <thiago...@gmail.com> wrote:
> >At first edition of "The C programming language" we can see
> >that structs could not be passed directly as function arguments.
> >They also could not be returned from functions and assigned.
> >
> >Soon after [1], the language changed to allow assignment and make
> >structs arguments passed by copy by default.
> >
> >Today, I have 0% of structs being passed by copy in my code.
> >So for me this is a very bad default.
> Two comments:
> 1) I never really understood *why* they changed the language to allow
> structs to be passed as actual objects. Anyone care to comment?

I guess is to make structs work as other basic types.

But they didn't realise the common case is passing without copy.
const also was added latter, making references safer.

At 2 edition of c programming language, page 128, they added a "struct point"
sample, passing and returning point by copy.
This is a very specific cases where using copy may make sense.

This sample was not present at first edition. So it kind of justifying the feature.

At pag 168 of Byte magazine 1983 [2] has a interesting comment that some compilers
use to pass address of struct even without operator &.

[2] pag 168
https://ia800309.us.archive.org/26/items/byte-magazine-1983-08/1983_08_BYTE_08-08_The_C_Language.pdf

This shows me that this behaviour was common sense, at least for some compilers.

Re: structs passed by copy

<tqfijf$9u7$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!NZ87pNe1TKxNDknVl4tZhw.user.46.165.242.91.POSTED!not-for-mail
From: antis...@math.uni.wroc.pl
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 02:28:00 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <tqfijf$9u7$1@gioia.aioe.org>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com> <tqeoet$2r8gf$1@news.xmission.com>
Injection-Info: gioia.aioe.org; logging-data="10183"; posting-host="NZ87pNe1TKxNDknVl4tZhw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: tin/2.4.5-20201224 ("Glen Albyn") (Linux/5.10.0-9-amd64 (x86_64))
Cancel-Lock: sha1:Kb0eo3ZyvTie3bX57jpGaU3XJxo=
X-Notice: Filtered by postfilter v. 0.9.2
 by: antis...@math.uni.wroc.pl - Sat, 21 Jan 2023 02:28 UTC

Kenny McCormack <gazelle@shell.xmission.com> wrote:
> In article <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>,
> Thiago Adams <thiago.adams@gmail.com> wrote:
> >At first edition of "The C programming language" we can see
> >that structs could not be passed directly as function arguments.
> >They also could not be returned from functions and assigned.
> >
> >Soon after [1], the language changed to allow assignment and make
> >structs arguments passed by copy by default.
> >
> >Today, I have 0% of structs being passed by copy in my code.
> >So for me this is a very bad default.
>
> Two comments:
> 1) I never really understood *why* they changed the language to allow
> structs to be passed as actual objects. Anyone care to comment?

Why not? Argument passing in C is releated to assignment. Once you
allow structure assignment it is natural to allow passing of
structs. If compiler passes arguments on the stack, then
implementation is allmost for free, just generate code as for
assignment to fill stack slot. If some arguments are passed in
registers, then implementation gets more complicated. OTOH
small structures that fit into one or two registers are more
efficient to pass by value, so it makes sense to spend some
effort to pass them in registers.

> 2) The usual method for passing structs around is to pass a pointer to
> the struct. That works just fine in all cases and, thus, as above,
> I don't understand why the change.

If you pass address of the struct, than there is no change. The
rest is just making struct into "first class objects".
> Like you, I've never needed (or used) this ability in my code, so have not
> ever used it. I wonder if it came about because they made some other
> changes (enhancements) in what you could legally do with a struct, and this
> ended up coming for free.

Once you have structurs assignment passing them as arguments is only
logical thing.

Few times are used structures with one member to get stronger type
checking (free of default C rules for argument compatibility). I did
appropriate change temporarly, using macros and for this use it was
essential that structures were passed by value. Few times I used
small structures and passed them by value. It made code slightly
simpler and probably more efficient than passing address.

--
Waldek Hebisch

Re: structs passed by copy

<MbIyL.391692$iS99.388714@fx16.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx16.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0)
Gecko/20100101 Thunderbird/102.6.1
Subject: Re: structs passed by copy
Content-Language: en-US
Newsgroups: comp.lang.c
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 46
Message-ID: <MbIyL.391692$iS99.388714@fx16.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Fri, 20 Jan 2023 21:35:24 -0500
X-Received-Bytes: 2932
 by: Richard Damon - Sat, 21 Jan 2023 02:35 UTC

On 1/20/23 1:41 PM, Thiago Adams wrote:
> At first edition of "The C programming language" we can see
> that structs could not be passed directly as function arguments.
> They also could not be returned from functions and assigned.
>
> Soon after [1], the language changed to allow assignment and make
> structs arguments passed by copy by default.
>
> Today, I have 0% of structs being passed by copy in my code.
> So for me this is a very bad default.
>
> If structs where passed by reference this would not be the only
> exception on the type system because arrays are already passed
> as pointers.
>
> Do you have structs passed by copy in your code?
> Could it be replaced by const * ?
> Considering your answer would you agree with me that this is a bad default?
>
> Returning function by copy is common in my code.
>
> [1]
> https://www.bell-labs.com/usr/dmr/www/cchanges.pdf
>

My impression is that the biggest reason structs are passed by value and
not automatically by address is that pass by value is useful in some
cases, and allows "pass by address" easily with just adding a character,
while the opposite makes things complicated and inefficient.

This did not work for arrays for several reasons. First, the Array name
to pointer transformation is a fairly fundamental part of the definition
of how arrays work.

Second, I suspect enough early code depended on the ability to "pass a
character array" to a function and it fill in the answer existed that
would make changing that behavior hard. (Note, the early version didn't
convert struct variables to addresses automatically, you just needed a
add the &/* yourself.

Third, there is likely enough code that plays fast and loose with the
knowledge that an parameter declaired as an array isn't actually an
array, and the point it is passed might not point to an array of that
exact size anyway, so coming up with a good definition of how to handle
this might be hard, and will have a number of special cases that act
"surprisingly" different.

Re: structs passed by copy

<tqfkit$2e0mf$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: chris.m....@gmail.com (Chris M. Thomasson)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Fri, 20 Jan 2023 19:01:48 -0800
Organization: A noiseless patient Spider
Lines: 48
Message-ID: <tqfkit$2e0mf$2@dont-email.me>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 21 Jan 2023 03:01:49 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="11c30eab5f36f858bcf8ef8589f0f908";
logging-data="2556623"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/FqSvDzYWqNq26sT03IHq/FFTM1mV37yE="
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
Cancel-Lock: sha1:u3aVYL/tJpagKkYOHCbKrGADeCs=
In-Reply-To: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
Content-Language: en-US
 by: Chris M. Thomasson - Sat, 21 Jan 2023 03:01 UTC

On 1/20/2023 10:41 AM, Thiago Adams wrote:
> At first edition of "The C programming language" we can see
> that structs could not be passed directly as function arguments.
> They also could not be returned from functions and assigned.
>
> Soon after [1], the language changed to allow assignment and make
> structs arguments passed by copy by default.
>
> Today, I have 0% of structs being passed by copy in my code.
> So for me this is a very bad default.
>
> If structs where passed by reference this would not be the only
> exception on the type system because arrays are already passed
> as pointers.
>
> Do you have structs passed by copy in your code?
> Could it be replaced by const * ?
> Considering your answer would you agree with me that this is a bad default?
>
> Returning function by copy is common in my code.
>
> [1]
> https://www.bell-labs.com/usr/dmr/www/cchanges.pdf
>
Sorry for any typos:

struct bar
{ int baz;
};

struct foo
{ struct bar bar[42];
};

struct foo
foobarbaz()
{ struct foo foo = { { 0 } };
foo.bar[0] = 4;
foo.bar[1] = 2;
return foo;
}

Nice! Wrapping up an array in a struct is fine with me... :^)

Re: structs passed by copy

<93789350-1b98-4acd-a345-81e0197cd4fen@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a37:b584:0:b0:706:9268:9e7b with SMTP id e126-20020a37b584000000b0070692689e7bmr575827qkf.459.1674271010924;
Fri, 20 Jan 2023 19:16:50 -0800 (PST)
X-Received: by 2002:a81:4ac1:0:b0:4ec:933c:6c99 with SMTP id
x184-20020a814ac1000000b004ec933c6c99mr1978418ywa.61.1674271010640; Fri, 20
Jan 2023 19:16:50 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer03.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Fri, 20 Jan 2023 19:16:50 -0800 (PST)
In-Reply-To: <tqesrc$i48$1@gioia.aioe.org>
Injection-Info: google-groups.googlegroups.com; posting-host=45.174.239.81; posting-account=xFcAQAoAAAAoWlfpQ6Hz2n-MU9fthxbY
NNTP-Posting-Host: 45.174.239.81
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com> <tqesrc$i48$1@gioia.aioe.org>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <93789350-1b98-4acd-a345-81e0197cd4fen@googlegroups.com>
Subject: Re: structs passed by copy
From: thiago.a...@gmail.com (Thiago Adams)
Injection-Date: Sat, 21 Jan 2023 03:16:50 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2226
 by: Thiago Adams - Sat, 21 Jan 2023 03:16 UTC

On Friday, January 20, 2023 at 5:16:58 PM UTC-3, Bart wrote:
> On 20/01/2023 18:41, Thiago Adams wrote:
> > At first edition of "The C programming language" we can see
> > that structs could not be passed directly as function arguments.
> > They also could not be returned from functions and assigned.
> >
> > Soon after [1], the language changed to allow assignment and make
> > structs arguments passed by copy by default.
> >
> > Today, I have 0% of structs being passed by copy in my code.
> > So for me this is a very bad default.
> >
> > If structs where passed by reference this would not be the only
> > exception on the type system because arrays are already passed
> > as pointers.
> If S is a struct instance, and A is an array instance, then here:
>
> S; // is the struct itself; not a reference;
> A; // is a pointer to A[0]; not the array itself

If you use sizeof of an array (not function argument) then
you get the correct size.

int a[2];
sizeof(a); // will not give the sizeof a[0]

So, in some cases it is not equivalent of first element.

Re: structs passed by copy

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Fri, 20 Jan 2023 21:08:10 -0800
Organization: None to speak of
Lines: 42
Message-ID: <87lelwiif9.fsf@nosuchdomain.example.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqesrc$i48$1@gioia.aioe.org>
<93789350-1b98-4acd-a345-81e0197cd4fen@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="6267dfc088a2417e999e077df7c21f53";
logging-data="2591363"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/eWhopIRLcyYyS/xiFSUby"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:/DCUUVNHe823N/2TjeFIN3xqieg=
sha1:IYjZDUFqHW0Fgvk/OhNjpZY3zHg=
 by: Keith Thompson - Sat, 21 Jan 2023 05:08 UTC

Thiago Adams <thiago.adams@gmail.com> writes:
> On Friday, January 20, 2023 at 5:16:58 PM UTC-3, Bart wrote:
>> On 20/01/2023 18:41, Thiago Adams wrote:
>> > At first edition of "The C programming language" we can see
>> > that structs could not be passed directly as function arguments.
>> > They also could not be returned from functions and assigned.
>> >
>> > Soon after [1], the language changed to allow assignment and make
>> > structs arguments passed by copy by default.
>> >
>> > Today, I have 0% of structs being passed by copy in my code.
>> > So for me this is a very bad default.
>> >
>> > If structs where passed by reference this would not be the only
>> > exception on the type system because arrays are already passed
>> > as pointers.
>> If S is a struct instance, and A is an array instance, then here:
>>
>> S; // is the struct itself; not a reference;
>> A; // is a pointer to A[0]; not the array itself
>
> If you use sizeof of an array (not function argument) then
> you get the correct size.

Of course.

> int a[2];
> sizeof(a); // will not give the sizeof a[0]
>
> So, in some cases it is not equivalent of first element.

It's never equivalent to the first element. It's sometimes equivalent
to the *address* of the first element.

But he specifically referred to `A;`, an expression statement where the
expression is the name of an array object. In that context, the
expression decays to a pointer to A[0] (and the value is discarded).

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

Re: structs passed by copy

<20230121003516.362@kylheku.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: 864-117-...@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 09:14:56 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 139
Message-ID: <20230121003516.362@kylheku.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqeoet$2r8gf$1@news.xmission.com>
<20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
<tqf121$2re3i$1@news.xmission.com>
Injection-Date: Sat, 21 Jan 2023 09:14:56 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="1ef615aa6043aaf8fc3b1a68c2965024";
logging-data="2674444"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18060Oqw8LDK/hYw3ODI3dgOYOIEzpBZ9U="
User-Agent: slrn/1.0.3 (Linux)
Cancel-Lock: sha1:TTHjcQzmpisdkK2ivABdrIPzu+M=
 by: Kaz Kylheku - Sat, 21 Jan 2023 09:14 UTC

On 2023-01-20, Kenny McCormack <gazelle@shell.xmission.com> wrote:
> I get that, for symmetry's sake and all, but wouldn't it be just as easy to
> pass the address of your struct? Like we do with most of the structs that
> get passed to Unix API calls (e.g., stat(2)).

In fact, passing a structure by value can be implemented by a hidden
pointer. Compilers do that at their discretion; it may be based on how
large the structure is and other factors.

The important thing is the semantics. In C, you can modify an argument
without affecting the caller; the understanding is that the object is
a local copy.

One big reason for passing a structure by value is that you want to
be able to mutate the copy, and to do that conveniently without
a dance like this:

void fun(struct obj *ptr)
{
struct obj local = *ptr;
// work with local
local.counter++;
// ...
}

It just just becomes

void fun(struct obj local)
{
local.counter++;
// ...
}

Even if you don't need the copy, there is less syntax. No pointer
declarator in the parameter, and just dot referencing instead of ->.

Moreover, you don't have to use the & operator to prepare the
argument;

void fun(struct obj local)
{
do_this(local.foo, local.bar);
}

//

struct obj x;
//
fun(x);

versus

void fun(struct obj *local) // noise here
{
do_this(local->foo, local->bar); // more noise here
}

//

struct obj x;
//
fun(&x); // noise here

There is more freedom to generate code for the by-value situation in
different ways. The pointer version basically specifies what the
implementation is, whereas the by-value version could use a pointer,
whereby callee copies the struct if necessary, or have the caller make
the copy, passing a pointer to the callee, or have the caller make an
actual stack copy that the callee finds at a fixed offset in the
parameter space. One strategy could be used for large structures,
another for small structures.

Thus, when you write the noise-free, clean version with by-value
passing, it can easily have the same efficiency as the explicit pointer
version, with as little actual data copying.

For small structures, you could actually be making the program less
efficient by passing a pointer. If you have a complex number
represented as a structure holding two doubles, when that is passed
by value, it could acctually be passed using the same mechanism
as if it were two arguments of type double. E.g. two registers.

And when the callee accesses "arg.real" and "arg.imag", that could
actually be compiled to just referring to those registers.

Whereas code that does arg->real might not have that pointer
optimized away at all.

Another consideration is that the recipent of a by-value struct
argument (no matter how the passage is implemented) has the
guarantee that it's a local. If the callee doesn't lift the
address of that local with the & operator, then it is guaranteed
that nothing else in the program has the address of the object;
and thus no function which it uses needs to be suspected of
modifying the object.

Pointer version:

void fun(const struct complex_number *pcplx)
{
double dr = other_fun_1(pcplx->real);
double di = other_fun_2(pcplx->real + pcplx->imag);
}

Here although the structure is marked "const", we have no idea
where the pointer came form and who else has that pointer.
When the other_fun_1 is called, and is an external function about which
we know nothing, it has to be assumed that it has access to that object
and can modify it. Thus the second evaluation of pcplx->real has to
re-load that member through the pointer. It cannot just re-use thue
previously loaded pcplx->real value.

There is no such threat under by-value:

void fun(const struct complex_number cplx)
{
double dr = other_fun_1(cplx.real);
double di = other_fun_2(cplx.real + cplx.imag);
}

The call to other_fun_1 cannot possibly change the value of
cplx.real; the compiler can put that into a register,
which doesn't have to be spilled and reloaded across
calls to external functions.

The pointer version could be optimized similarly if we
could convince the compiler that the object cannot
be modified. E.g. the members themselves could
be const-qualified types:

struct complex_number { const double real, imag; };

That's going to create hassles.

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

Re: structs passed by copy

<tqgd4p$2hs2e$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 05:00:57 -0500
Organization: A noiseless patient Spider
Lines: 20
Message-ID: <tqgd4p$2hs2e$1@dont-email.me>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqeoet$2r8gf$1@news.xmission.com>
<20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 21 Jan 2023 10:00:57 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="5ff9c038becceac0306b3924785a349a";
logging-data="2682958"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18F1z4qrfiOPORyyXgLlygLW0JlxZlH71E="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.4.2
Cancel-Lock: sha1:431rJCOucNox8n+w/3Ggwbv+PUs=
Content-Language: en-US
In-Reply-To: <20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
 by: James Kuyper - Sat, 21 Jan 2023 10:00 UTC

On 1/20/23 15:01, Anton Shepelev wrote:
> Kenny McCormack:
>
>> I never really understood *why* they changed the language
>> to allow structs to be passed as actual objects. Anyone
>> care to comment?
>
> Integers, floats, and doubles can be passed by value. If I
> were to implement complex numbers, I should do it as a
> struct with a .re (real) and .im (imaginary) fields. I
> should want to pass it by value whenever I pass real types
> by value.

Note that this argument can be applied to any struct which can be seen
as implementing a type that is, in some way, an extension of the scalar
types, because the scalar types are also passed by value. I could, and
have, had reason to use this approach for things like quaternions,
vectors or tensors with a small fixed dimensions (<= 4), and smart
pointers. It's much easier to do such things in C++, where I can create
corresponding operator overloads.

Re: structs passed by copy

<tqgped$1gti$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!uabYU4OOdxBKlV2hpj27FQ.user.46.165.242.75.POSTED!not-for-mail
From: bc...@freeuk.com (Bart)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 13:30:54 +0000
Organization: Aioe.org NNTP Server
Message-ID: <tqgped$1gti$1@gioia.aioe.org>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqesrc$i48$1@gioia.aioe.org>
<93789350-1b98-4acd-a345-81e0197cd4fen@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Info: gioia.aioe.org; logging-data="50098"; posting-host="uabYU4OOdxBKlV2hpj27FQ.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.6.1
X-Notice: Filtered by postfilter v. 0.9.2
 by: Bart - Sat, 21 Jan 2023 13:30 UTC

On 21/01/2023 03:16, Thiago Adams wrote:
> On Friday, January 20, 2023 at 5:16:58 PM UTC-3, Bart wrote:
>> On 20/01/2023 18:41, Thiago Adams wrote:
>>> At first edition of "The C programming language" we can see
>>> that structs could not be passed directly as function arguments.
>>> They also could not be returned from functions and assigned.
>>>
>>> Soon after [1], the language changed to allow assignment and make
>>> structs arguments passed by copy by default.
>>>
>>> Today, I have 0% of structs being passed by copy in my code.
>>> So for me this is a very bad default.
>>>
>>> If structs where passed by reference this would not be the only
>>> exception on the type system because arrays are already passed
>>> as pointers.
>> If S is a struct instance, and A is an array instance, then here:
>>
>> S; // is the struct itself; not a reference;
>> A; // is a pointer to A[0]; not the array itself
>
> If you use sizeof of an array (not function argument) then
> you get the correct size.
>
> int a[2];
> sizeof(a); // will not give the sizeof a[0]
>
> So, in some cases it is not equivalent of first element.

You snipped this bit:

>> So the default is already part of the language, and exists in nearly
>> every place where you use S and A within an expression.

Re: structs passed by copy

<tqgr4f$2k7io$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: david.br...@hesbynett.no (David Brown)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 14:59:43 +0100
Organization: A noiseless patient Spider
Lines: 31
Message-ID: <tqgr4f$2k7io$1@dont-email.me>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Sat, 21 Jan 2023 13:59:43 -0000 (UTC)
Injection-Info: reader01.eternal-september.org; posting-host="32bd68efd534e778738b021541f4943c";
logging-data="2760280"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/0UJl+oICxtNFxnvAKzeRVYtR0kya5bDQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101
Thunderbird/102.2.2
Cancel-Lock: sha1:8zeV0P4D7X0RUm6az/4MH/MlKZY=
Content-Language: en-GB
In-Reply-To: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
 by: David Brown - Sat, 21 Jan 2023 13:59 UTC

On 20/01/2023 19:41, Thiago Adams wrote:

> Do you have structs passed by copy in your code?
> Could it be replaced by const * ?
> Considering your answer would you agree with me that this is a bad default?
>
> Returning function by copy is common in my code.
>

In C, struct (and union) is the only way to make a new type. So if you
have a gui library with handles that are implemented as ints, you can write:

typedef struct { int contents; } handle;

Then you can assign, pass around and return instances of type "handle"
as conveniently and efficiently as an "int", but in a typesafe manner -
the compiler won't let you mix it up with an "int", or mismatch pointers
to handle with pointers to int.

If you have a fixed size buffer, you can have :

typedef struct { char data[bufSize]; } buffer;

and pass that around. sizeof works "correctly", and the ABI and
compiler conspire to make it all efficient using hidden pointers.

I wouldn't use pointers here unless I actually wanted pointers - passing
or returning structs by values is fine.

Re: structs passed by copy

<1btu0jojlm.fsf@pfeifferfamily.net>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: pfeif...@cs.nmsu.edu (Joe Pfeiffer)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 10:58:29 -0700
Organization: A noiseless patient Spider
Lines: 10
Message-ID: <1btu0jojlm.fsf@pfeifferfamily.net>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqeoet$2r8gf$1@news.xmission.com>
<20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
<tqf121$2re3i$1@news.xmission.com>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="b85803cca465b11271861e3c9cc7083f";
logging-data="2838371"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/kIwdMcCtBvjHaaV4o45hC1sZG9dAEnmU="
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:dwBie3EX0OhHXU1TAFRug6wtvF8=
sha1:zyjEN6DL8dP15TGR8HnY5XtrYos=
 by: Joe Pfeiffer - Sat, 21 Jan 2023 17:58 UTC

gazelle@shell.xmission.com (Kenny McCormack) writes:
>
> By the way, didn't they already add complex numbers to the standard,
> somewhere along the way? Having never used them, I can't remember, off
> hand, how they are implemented.

Yes, though that doesn't really affect the example.

I first learned about complex numbers in C when I typoed something like
5i when I meant 5+i and was very, very confused for a while.

Re: structs passed by copy

<tqh98p$2shnk$1@news.xmission.com>

  copy mid

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

  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: structs passed by copy
Date: Sat, 21 Jan 2023 18:00:57 -0000 (UTC)
Organization: The official candy of the new Millennium
Message-ID: <tqh98p$2shnk$1@news.xmission.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com> <20230120230138.1225d36e5ad8e5f07401c67a@gmail.com> <tqf121$2re3i$1@news.xmission.com> <1btu0jojlm.fsf@pfeifferfamily.net>
Injection-Date: Sat, 21 Jan 2023 18:00:57 -0000 (UTC)
Injection-Info: news.xmission.com; posting-host="shell.xmission.com:166.70.8.4";
logging-data="3032820"; 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, 21 Jan 2023 18:00 UTC

In article <1btu0jojlm.fsf@pfeifferfamily.net>,
Joe Pfeiffer <pfeiffer@cs.nmsu.edu> wrote:
>gazelle@shell.xmission.com (Kenny McCormack) writes:
>>
>> By the way, didn't they already add complex numbers to the standard,
>> somewhere along the way? Having never used them, I can't remember, off
>> hand, how they are implemented.
>
>Yes, though that doesn't really affect the example.

Agreed.

>I first learned about complex numbers in C when I typoed something like
>5i when I meant 5+i and was very, very confused for a while.

Interesting.

--
I'll give him credit for one thing: He is (& will be) the most quotable President
ever. Books have been written about (GW) Bushisms, but Dubya's got nothing on Trump.

Tremendously wet - from the standpoint of water.

Re: structs passed by copy

<tqhcmm$lpp$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!4Hoj1xNXwFYGL+DljOtGgA.user.46.165.242.75.POSTED!not-for-mail
From: ifo...@youknew.org (Opus)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 19:59:29 +0100
Organization: Aioe.org NNTP Server
Message-ID: <tqhcmm$lpp$1@gioia.aioe.org>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 8bit
Injection-Info: gioia.aioe.org; logging-data="22329"; posting-host="4Hoj1xNXwFYGL+DljOtGgA.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:102.0) Gecko/20100101
Thunderbird/102.7.0
X-Notice: Filtered by postfilter v. 0.9.2
Content-Language: fr
 by: Opus - Sat, 21 Jan 2023 18:59 UTC

Le 20/01/2023 à 19:41, Thiago Adams a écrit :
> At first edition of "The C programming language" we can see
> that structs could not be passed directly as function arguments.
> They also could not be returned from functions and assigned.

Yes, that was a very long time ago. I personally don't remember having
ever been exposed to a C compiler that didn't support it.

> Soon after [1], the language changed to allow assignment and make
> structs arguments passed by copy by default.

Indeed.

> Do you have structs passed by copy in your code?

Yes, sometimes. As with any other programming topic, the key is just to
use your brain. Usually better than cargo cult programming.

- Passing and returning structs by value gets you some kind of
"functional" style.
- Avoids any pointer error.
- Should usually be avoided if the structure is too large. Common sense.
For two reasons: first it has to be copied (takes some time), and second
it will eat up space in the stack (may or may not be an issue depending
on your context.)
- Is thus inefficient if the struct is large, especially if you are only
going to access one or just a few members. In this case, it's obvious
that accessing it by reference (through a pointer) makes more sense.
- But if the struct is reasonably small, it can be not just elegant, but
very efficient. Most ABIs these days pass arguments in registers up to a
certain size/number of arguments. If say you define a 'complex' type
struct (two 'double'), then passing them by value rather than reference
works great and is less tedious than using pointers.
- Returning structs can be useful to combine, for instance, a returned
error code and a returned value.

If you want to get an impression of how that all compiles on various
targets and make comparisons, I suggest https://godbolt.org/ which is
very handy.

Re: structs passed by copy

<20230121230054.29622c25db348474c66a254e@gmail.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: anton....@gmail.com (Anton Shepelev)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 23:00:54 +0300
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <20230121230054.29622c25db348474c66a254e@gmail.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqeoet$2r8gf$1@news.xmission.com>
<20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
<tqgd4p$2hs2e$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=US-ASCII
Content-Transfer-Encoding: 7bit
Injection-Info: reader01.eternal-september.org; posting-host="d76d9ca7c4b86e14ed8689b95c24bf23";
logging-data="2875259"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX180rcBKBf4dlbgmGiL0oYVSxnAYb6csyDg="
Cancel-Lock: sha1:mbQllEax/Ws+IdurAEKe3eU6GBw=
X-Newsreader: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
 by: Anton Shepelev - Sat, 21 Jan 2023 20:00 UTC

James Kuyper to Anton Shepelev:

> > Integers, floats, and doubles can be passed by value.
> > If I were to implement complex numbers, I should do it
> > as a struct with a .re (real) and .im (imaginary)
> > fields. I should want to pass it by value whenever I
> > pass real types by value.
>
> Note that this argument can be applied to any struct which
> can be seen as implementing a type that is, in some way,
> an extension of the scalar types, because the scalar types
> are also passed by value. I could, and have, had reason to
> use this approach for things like quaternions, vectors or
> tensors with a small fixed dimensions (<= 4), and smart
> pointers. It's much easier to do such things in C++, where
> I can create corresponding operator overloads.

Indeed. My specific example demostrates a general deficiency
of C and other similar languages: implementing one's own
"scalar-like" types is impossibe or cumbersome, whereas
those types are often very useful. All right, modern C has
built-in complex type, but that only shows that it cannot
be implemented by the programmer *in* C!

--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments

Re: structs passed by copy

<tpZyL.764075$GNG9.196142@fx18.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer02.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx18.iad.POSTED!not-for-mail
MIME-Version: 1.0
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0)
Gecko/20100101 Thunderbird/102.6.1
Subject: Re: structs passed by copy
Content-Language: en-US
Newsgroups: comp.lang.c
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqeoet$2r8gf$1@news.xmission.com>
<20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
<tqgd4p$2hs2e$1@dont-email.me>
<20230121230054.29622c25db348474c66a254e@gmail.com>
From: Rich...@Damon-Family.org (Richard Damon)
In-Reply-To: <20230121230054.29622c25db348474c66a254e@gmail.com>
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
Lines: 39
Message-ID: <tpZyL.764075$GNG9.196142@fx18.iad>
X-Complaints-To: abuse@easynews.com
Organization: Forte - www.forteinc.com
X-Complaints-Info: Please be sure to forward a copy of ALL headers otherwise we will be unable to process your complaint properly.
Date: Sat, 21 Jan 2023 17:10:33 -0500
X-Received-Bytes: 2731
 by: Richard Damon - Sat, 21 Jan 2023 22:10 UTC

On 1/21/23 3:00 PM, Anton Shepelev wrote:
> James Kuyper to Anton Shepelev:
>
>>> Integers, floats, and doubles can be passed by value.
>>> If I were to implement complex numbers, I should do it
>>> as a struct with a .re (real) and .im (imaginary)
>>> fields. I should want to pass it by value whenever I
>>> pass real types by value.
>>
>> Note that this argument can be applied to any struct which
>> can be seen as implementing a type that is, in some way,
>> an extension of the scalar types, because the scalar types
>> are also passed by value. I could, and have, had reason to
>> use this approach for things like quaternions, vectors or
>> tensors with a small fixed dimensions (<= 4), and smart
>> pointers. It's much easier to do such things in C++, where
>> I can create corresponding operator overloads.
>
> Indeed. My specific example demostrates a general deficiency
> of C and other similar languages: implementing one's own
> "scalar-like" types is impossibe or cumbersome, whereas
> those types are often very useful. All right, modern C has
> built-in complex type, but that only shows that it cannot
> be implemented by the programmer *in* C!
>

No, they CAN be implemented, they just don't completely work like the
built in types because the operators don't work on them.

Yes, to make a completely "act like a native" type, you need to be able
to overload the operators, and then you need to be able to define
'conversion' functions to convert one type to another, and so on and you
get to the beginnings of something like C++.

Once you let that "Nosse of the Camel" in, you tend to get a lot of the
base language of C++ following.

Re: structs passed by copy

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

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail
From: Keith.S....@gmail.com (Keith Thompson)
Newsgroups: comp.lang.c
Subject: Re: structs passed by copy
Date: Sat, 21 Jan 2023 16:14:12 -0800
Organization: None to speak of
Lines: 16
Message-ID: <87edrnifxn.fsf@nosuchdomain.example.com>
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
<tqeoet$2r8gf$1@news.xmission.com>
<20230120230138.1225d36e5ad8e5f07401c67a@gmail.com>
<tqf121$2re3i$1@news.xmission.com> <1btu0jojlm.fsf@pfeifferfamily.net>
MIME-Version: 1.0
Content-Type: text/plain
Injection-Info: reader01.eternal-september.org; posting-host="a96db803e771599752dfc0089ea20ee1";
logging-data="2958427"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/UPOnCTa1oC3hBV1KbScsD"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.2 (gnu/linux)
Cancel-Lock: sha1:KArHj3tDNBAxTAI/boc0nu5G2gI=
sha1:64hVjGH8ud3a+EoxL4O7HsY4epk=
 by: Keith Thompson - Sun, 22 Jan 2023 00:14 UTC

Joe Pfeiffer <pfeiffer@cs.nmsu.edu> writes:
[...]
> I first learned about complex numbers in C when I typoed something like
> 5i when I meant 5+i and was very, very confused for a while.

5i is an imaginary constant, a gcc-specific extension. It's a syntax
error in standard C.

gcc imaginary constants are of complex type. The standard defines
optional support for imaginary types in Annex G, but I don't know
of any compiler that supports them (or how they'd be useful).

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

Re: structs passed by copy

<4707042e-7997-4082-a44a-6357d5b80f6en@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ae9:eb11:0:b0:706:1cd6:24a4 with SMTP id b17-20020ae9eb11000000b007061cd624a4mr648399qkg.213.1674348902173;
Sat, 21 Jan 2023 16:55:02 -0800 (PST)
X-Received: by 2002:a81:7047:0:b0:4bc:e7dc:16bc with SMTP id
l68-20020a817047000000b004bce7dc16bcmr2330061ywc.368.1674348901942; Sat, 21
Jan 2023 16:55:01 -0800 (PST)
Path: i2pn2.org!i2pn.org!usenet.blueworldhosting.com!feed1.usenet.blueworldhosting.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Sat, 21 Jan 2023 16:55:01 -0800 (PST)
In-Reply-To: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a0d:6fc2:55b0:ca00:18a5:d365:1d2b:77b8;
posting-account=ow8VOgoAAAAfiGNvoH__Y4ADRwQF1hZW
NNTP-Posting-Host: 2a0d:6fc2:55b0:ca00:18a5:d365:1d2b:77b8
References: <76ec6d87-0f5d-4671-b4c8-7023e3da787an@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <4707042e-7997-4082-a44a-6357d5b80f6en@googlegroups.com>
Subject: Re: structs passed by copy
From: already5...@yahoo.com (Michael S)
Injection-Date: Sun, 22 Jan 2023 00:55:02 +0000
Content-Type: text/plain; charset="UTF-8"
X-Received-Bytes: 2367
 by: Michael S - Sun, 22 Jan 2023 00:55 UTC

On Friday, January 20, 2023 at 8:41:16 PM UTC+2, Thiago Adams wrote:
> At first edition of "The C programming language" we can see
> that structs could not be passed directly as function arguments.
> They also could not be returned from functions and assigned.
>
> Soon after [1], the language changed to allow assignment and make
> structs arguments passed by copy by default.
>
> Today, I have 0% of structs being passed by copy in my code.
> So for me this is a very bad default.
>
> If structs where passed by reference this would not be the only
> exception on the type system because arrays are already passed
> as pointers.
>
> Do you have structs passed by copy in your code?

Yes, I do.

> Could it be replaced by const * ?

Not 100%

> Considering your answer would you agree with me that this is a bad default?

No, it is the only sane option.
The early C that didn't have it was inconsistent.
And I don't understand why do you call it "default'.

>
> Returning function by copy is common in my code.
>

Then I even less understand your objection to passing by value.

> [1]
> https://www.bell-labs.com/usr/dmr/www/cchanges.pdf

Pages:123456
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor