Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

I must have slipped a disk -- my pack hurts!


devel / comp.lang.c / Re: About struct fields that are assigned once and never changed again

SubjectAuthor
* About struct fields that are assigned once and never changed againOğuz
+- Re: About struct fields that are assigned once and never changed againOğuz
+- Re: About struct fields that are assigned once and never changed againMalcolm McLean
+* Re: About struct fields that are assigned once and never changedDavid Brown
|`* Re: About struct fields that are assigned once and never changed againOğuz
| +- Re: About struct fields that are assigned once and never changedDavid Brown
| `- Re: About struct fields that are assigned once and never changed againScott Lurndal
+- Re: About struct fields that are assigned once and never changed againBen Bacarisse
+- Re: About struct fields that are assigned once and never changedJames Kuyper
+* Re: About struct fields that are assigned once and never changedGuillaume
|`- Re: About struct fields that are assigned once and never changedJames Kuyper
`* Re: About struct fields that are assigned once and never changed againTim Rentsch
 `- Re: About struct fields that are assigned once and never changedOğuz

1
About struct fields that are assigned once and never changed again

<56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:622a:1112:: with SMTP id e18mr44229685qty.226.1639392555090;
Mon, 13 Dec 2021 02:49:15 -0800 (PST)
X-Received: by 2002:a05:620a:2995:: with SMTP id r21mr32558939qkp.100.1639392554919;
Mon, 13 Dec 2021 02:49:14 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 13 Dec 2021 02:49:14 -0800 (PST)
Injection-Info: google-groups.googlegroups.com; posting-host=85.104.46.109; posting-account=RbOzpwoAAACSDI6OO1wVarfPakNstxUl
NNTP-Posting-Host: 85.104.46.109
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Subject: About struct fields that are assigned once and never changed again
From: oguzisma...@gmail.com (Oğuz)
Injection-Date: Mon, 13 Dec 2021 10:49:15 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 12
 by: Oğuz - Mon, 13 Dec 2021 10:49 UTC

I have an array of structs as follows:

struct {
struct {
int value;
bool in_use;
} values[MAX_INPUTS_SIZE];
size_t size;
} inputs;

and a function that initializes it with user-supplied data. After the function is run, nothing in the program alters `inputs.size' or `inputs.values[N].value'. Now I have two questions about this that I can't answer myself:
i) Is there a way to treat size and value fields as `const' outside that function?
ii) If there is, would it make any difference performance-wise? Would it allow the compiler to optimize more?

Re: About struct fields that are assigned once and never changed again

<f6e5e404-1336-40b6-a38a-a63bc7548fbdn@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:ad4:5969:: with SMTP id eq9mr42171663qvb.4.1639392644165;
Mon, 13 Dec 2021 02:50:44 -0800 (PST)
X-Received: by 2002:ad4:4ee4:: with SMTP id dv4mr40851962qvb.59.1639392644050;
Mon, 13 Dec 2021 02:50:44 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 13 Dec 2021 02:50:43 -0800 (PST)
In-Reply-To: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=85.104.46.109; posting-account=RbOzpwoAAACSDI6OO1wVarfPakNstxUl
NNTP-Posting-Host: 85.104.46.109
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <f6e5e404-1336-40b6-a38a-a63bc7548fbdn@googlegroups.com>
Subject: Re: About struct fields that are assigned once and never changed again
From: oguzisma...@gmail.com (Oğuz)
Injection-Date: Mon, 13 Dec 2021 10:50:44 +0000
Content-Type: text/plain; charset="UTF-8"
Content-Transfer-Encoding: quoted-printable
Lines: 3
 by: Oğuz - Mon, 13 Dec 2021 10:50 UTC

On Monday, December 13, 2021 at 1:49:20 PM UTC+3, Oğuz wrote:
> I have an array of structs as follows:

Sorry, I meant "a struct", not "an array of structs".

Re: About struct fields that are assigned once and never changed again

<95e8b1c9-b1a4-4743-afb5-478f18b93ea2n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a0c:8031:: with SMTP id 46mr41772294qva.126.1639393511040;
Mon, 13 Dec 2021 03:05:11 -0800 (PST)
X-Received: by 2002:a05:6214:1d05:: with SMTP id e5mr41967647qvd.3.1639393510921;
Mon, 13 Dec 2021 03:05:10 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 13 Dec 2021 03:05:10 -0800 (PST)
In-Reply-To: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Injection-Info: google-groups.googlegroups.com; posting-host=2a00:23a8:400a:5601:2c0a:8f93:f815:d5e;
posting-account=Dz2zqgkAAADlK5MFu78bw3ab-BRFV4Qn
NNTP-Posting-Host: 2a00:23a8:400a:5601:2c0a:8f93:f815:d5e
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <95e8b1c9-b1a4-4743-afb5-478f18b93ea2n@googlegroups.com>
Subject: Re: About struct fields that are assigned once and never changed again
From: malcolm....@gmail.com (Malcolm McLean)
Injection-Date: Mon, 13 Dec 2021 11:05:11 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 19
 by: Malcolm McLean - Mon, 13 Dec 2021 11:05 UTC

On Monday, 13 December 2021 at 10:49:20 UTC, oguzism...@gmail.com wrote:
> I have an array of structs as follows:
>
> struct {
> struct {
> int value;
> bool in_use;
> } values[MAX_INPUTS_SIZE];
> size_t size;
> } inputs;
>
> and a function that initializes it with user-supplied data. After the function is run, nothing in the program alters `inputs.size' or `inputs.values[N].value'.
> Now I have two questions about this that I can't answer myself:
> i) Is there a way to treat size and value fields as `const' outside that function?
> ii) If there is, would it make any difference performance-wise? Would it allow the compiler to optimize more?
>
i) C doesn't allow that. It doesn't have a concept of a "constructor".
ii) It's unlikely to make any difference to performance. If a "variable" is known at compile time,
then the compiler can propagate it, and often do some significant optimisations. But if it is
read in from a parameter list, then this isn't possible.

Re: About struct fields that are assigned once and never changed again

<sp79qr$uop$1@dont-email.me>

  copy mid

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

  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: About struct fields that are assigned once and never changed
again
Date: Mon, 13 Dec 2021 12:12:26 +0100
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <sp79qr$uop$1@dont-email.me>
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 13 Dec 2021 11:12:27 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="eeeec74601fe8fc9b3a109e1e804c591";
logging-data="31513"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/nVW4PRcqsJ6tDiCOURWOgqn2q8J5kz24="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:EfrPUJutkgSAZrEOLGo0fc5/hOo=
In-Reply-To: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Mon, 13 Dec 2021 11:12 UTC

On 13/12/2021 11:49, Oğuz wrote:
> I have an array of structs as follows:
>
> struct {
> struct {
> int value;
> bool in_use;
> } values[MAX_INPUTS_SIZE];
> size_t size;
> } inputs;
>

(Google's broken interface messes up formatting, especially indentation.
If you are going to use Usenet a lot then I strongly recommend getting
a proper newsreader client and server. They are freely available, and
work much better.)

> and a function that initializes it with user-supplied data. After the function is run, nothing in the program alters `inputs.size' or `inputs.values[N].value'. Now I have two questions about this that I can't answer myself:
> i) Is there a way to treat size and value fields as `const' outside that function?
> ii) If there is, would it make any difference performance-wise? Would it allow the compiler to optimize more?
>

Your friend here is the online compiler at <https://godbolt.org>. You
can pick your language, your compiler, your flag set (for experimenting
with gcc, I'd recommend something like "-std=c11 -Wall -Wextra
-Wpedantic .O2"). Then you put in your test code and see the effect and
the error messages immediately. It's great for trying out different things.

In C, you can think of two kinds of "const". One is objects that are
defined as "const" - these must be given initial values, and you are not
allowed to change them later. That is not suitable here, as you don't
want to write out an initialiser for the whole inputs, you want to fill
it with a loop.

The other is to use a const-qualified lvalue (such as a "pointer to
const T") to access the data. This will not let the compiler do much
extra in the way of optimisation, but it can reduce the risk of mistakes
in your code (or rather, it can increase the likelihood of certain
mistakes being caught by the compiler.

As for performance, this all depends on what you are doing with the
data. If you hare using it heavily and might be doing so from different
cpu cores, then it can make sense to separate the fixed data from the
the variable data in different arrays. This will make it possible to
have shared read-only cache lines for the read-only data, rather than
more costly exclusive read-write cache lines. But don't worry about
that unless you are actually measuring performance and see that it is
slower than you want.

Re: About struct fields that are assigned once and never changed again

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

  copy mid

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

  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: About struct fields that are assigned once and never changed again
Date: Mon, 13 Dec 2021 16:12:37 +0000
Organization: A noiseless patient Spider
Lines: 36
Message-ID: <87fsqw32mi.fsf@bsb.me.uk>
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="462bd188a26106dd15fe29c93f2c378f";
logging-data="7702"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Ip7sjhMEFwIu2ZqwnVGhklekMdKOEj5k="
Cancel-Lock: sha1:hMjvR6xMJ0P7xFBR5Ig4iSBbsCg=
sha1:0K5TkZAmLFJOcO5m+YWSvBW8wlI=
X-BSB-Auth: 1.c39fc79828617da14653.20211213161237GMT.87fsqw32mi.fsf@bsb.me.uk
 by: Ben Bacarisse - Mon, 13 Dec 2021 16:12 UTC

Oğuz <oguzismailuysal@gmail.com> writes:

> I have a struct as follows:
(corrected)
>
> struct {
> struct {
> int value;
> bool in_use;
> } values[MAX_INPUTS_SIZE];
> size_t size;
> } inputs;
>
> and a function that initializes it with user-supplied data. After the
> function is run, nothing in the program alters `inputs.size' or
> `inputs.values[N].value'. Now I have two questions about this that I
> can't answer myself:
>
> i) Is there a way to treat size and value fields as `const' outside
> that function?

Hmm... You could have two versions, a public one and a private one (the
pre-processor is your friend here). The public one has const on value
and size, the private one, visible to the function that sets up the
data, does not.

I have a feeling this is not 100% permitted, but I don't have time to
check.

> ii) If there is, would it make any difference performance-wise? Would
> it allow the compiler to optimize more?

Pass.

--
Ben.

Re: About struct fields that are assigned once and never changed again

<221ac97e-0500-45dc-867a-b469f3816712n@googlegroups.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
X-Received: by 2002:a05:6214:4007:: with SMTP id kd7mr44123493qvb.52.1639414903462;
Mon, 13 Dec 2021 09:01:43 -0800 (PST)
X-Received: by 2002:a05:620a:440d:: with SMTP id v13mr32722985qkp.597.1639414903197;
Mon, 13 Dec 2021 09:01:43 -0800 (PST)
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!border1.nntp.dca1.giganews.com!nntp.giganews.com!news-out.google.com!nntp.google.com!postnews.google.com!google-groups.googlegroups.com!not-for-mail
Newsgroups: comp.lang.c
Date: Mon, 13 Dec 2021 09:01:43 -0800 (PST)
In-Reply-To: <sp79qr$uop$1@dont-email.me>
Injection-Info: google-groups.googlegroups.com; posting-host=85.104.46.109; posting-account=RbOzpwoAAACSDI6OO1wVarfPakNstxUl
NNTP-Posting-Host: 85.104.46.109
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com> <sp79qr$uop$1@dont-email.me>
User-Agent: G2/1.0
MIME-Version: 1.0
Message-ID: <221ac97e-0500-45dc-867a-b469f3816712n@googlegroups.com>
Subject: Re: About struct fields that are assigned once and never changed again
From: oguzisma...@gmail.com (Oğuz)
Injection-Date: Mon, 13 Dec 2021 17:01:43 +0000
Content-Type: text/plain; charset="UTF-8"
Lines: 6
 by: Oğuz - Mon, 13 Dec 2021 17:01 UTC

On Monday, December 13, 2021 at 2:12:39 PM UTC+3, David Brown wrote:
> (Google's broken interface messes up formatting, especially indentation.
> If you are going to use Usenet a lot then I strongly recommend getting
> a proper newsreader client and server. They are freely available, and
> work much better.)

Sorry. I tried Pan before but didn't like it. Which one do you use?

Re: About struct fields that are assigned once and never changed again

<sp7uo8$spv$2@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: About struct fields that are assigned once and never changed
again
Date: Mon, 13 Dec 2021 12:09:28 -0500
Organization: A noiseless patient Spider
Lines: 104
Message-ID: <sp7uo8$spv$2@dont-email.me>
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 13 Dec 2021 17:09:28 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1cadc526bfb859daa4447c158fee80fe";
logging-data="29503"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/ZhJ/2NT+/jg3e8Sdsgq50I+AfCmhKDZQ="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:/0kS9wa+Bqy1V2uXnCswIpxTfVU=
In-Reply-To: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Content-Language: en-US
 by: James Kuyper - Mon, 13 Dec 2021 17:09 UTC

On 12/13/21 5:49 AM, Oğuz wrote:
> I have an array of structs as follows:
>
> struct {
> struct {
> int value;
> bool in_use;
> } values[MAX_INPUTS_SIZE];
> size_t size;
> } inputs;
>
> and a function that initializes it with user-supplied data. After the function is run, nothing in the program alters `inputs.size' or `inputs.values[N].value'. Now I have two questions about this that I can't answer myself:
> i) Is there a way to treat size and value fields as `const' outside that function?

Sort-of. What you can do is use access functions to give a const
interface to the data, with the file containing the initialization
routine and access functions being the only place which has non-const
access to the data. The key to making this work is to declaring an
opaque struct type for use outside of that, with those functions being
the only ones that know the actual struct layout:

mystruct.h:
#ifndef H_MYSTRUCT
#define H_MYSTRUCT

struct mystruct;

int get_value(const struct mystruct*, size_t);
size_t get_size(const struct mystruct*);
const struct mystruct *initialize_mystruct
(size_t size, int[static size]);
#endif

mystruct.c:
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include "mystruct.h"

// Since this approach requires that mystruct objects be dynamically
// allocated, I decided to save some space by moving size to the
// beginning, allowing values to be a flexible array member. As a result
// you don't have to impose any arbitrary upper limit on the size;
// you're limited only by the amount of availale memory. You don't have
// to do this.

struct mystruct {
size_t size;
struct {
int value;
bool in_use;
} values[];
};

int get_value(const struct mystruct *inputs, size_t index)
{ // I'm not sure how you want to use in_use, but it should probably
// have some role to play in this function.

if(inputs == NULL || index >= inputs->size)
{
// Error handling
return 0;
}
return inputs->values[index].value;
}

size_t get_size(const struct mystruct *inputs)
{ if(inputs == NULL)
{
// Error handling
return 0;
}
return inputs->size;
}

const struct mystruct *initialize_mystruct(size_t size, int values[size])
{ struct mystruct *inputs = malloc(sizeof(*inputs) +
size * sizeof(inputs->values[0]));
if(inputs == NULL)
{
// Error handling
return NULL;
}
inputs->size = size;
memcpy(inputs->values, values, size*sizeof(values[0]));
for(size_t val=0; val<size; val++)
inputs->values[val].in_use = false;
return inputs;
}

The above code compiles on my desktop without error messages, but I have
not tested it. My apologies for any errors I might have made.

Note that the members of mystruct are completely unknown outside of
mystruct.c, and code outside of mystruct.c never sees anything other
than const-qualified pointers to struct mystruct.

> ii) If there is, would it make any difference performance-wise? Would it allow the compiler to optimize more?

I wouldn't expect major benefits from this approach other than making
the contents of struct mystruct inaccessible.

Re: About struct fields that are assigned once and never changed again

<sp80dm$v96$1@dont-email.me>

  copy mid

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

  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: About struct fields that are assigned once and never changed
again
Date: Mon, 13 Dec 2021 18:37:58 +0100
Organization: A noiseless patient Spider
Lines: 21
Message-ID: <sp80dm$v96$1@dont-email.me>
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
<sp79qr$uop$1@dont-email.me>
<221ac97e-0500-45dc-867a-b469f3816712n@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Date: Mon, 13 Dec 2021 17:37:58 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="eeeec74601fe8fc9b3a109e1e804c591";
logging-data="32038"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+AfugstzN0Iy6NRwDsDGE/WBcdGsCHtdw="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.11.0
Cancel-Lock: sha1:/OgFs/IAkbdIhMk9UMRWCsa/rAo=
In-Reply-To: <221ac97e-0500-45dc-867a-b469f3816712n@googlegroups.com>
Content-Language: en-GB
 by: David Brown - Mon, 13 Dec 2021 17:37 UTC

On 13/12/2021 18:01, Oğuz wrote:
> On Monday, December 13, 2021 at 2:12:39 PM UTC+3, David Brown wrote:
>> (Google's broken interface messes up formatting, especially indentation.
>> If you are going to use Usenet a lot then I strongly recommend getting
>> a proper newsreader client and server. They are freely available, and
>> work much better.)
>
> Sorry. I tried Pan before but didn't like it. Which one do you use?
>

I don't want to start yet another thread or branch about newsreaders -
if my answer here doesn't suit you, there are many previous ones in this
group and others. (And if there is one thing google groups is good for,
it is searching old posts.) My email is also valid.

I use Thunderbird for news (and email, and calender), on Windows and
Linux. I don't know if it is the "best" Usenet client - that will
depend on personal preferences. But it has been fine for me since about
version 1. I use news.eternal-semptember.org as the server - it's free,
and has all the newsgroups I need. (I also use news.gmane.org as an
alternative interface to a number of mailing lists.)

Re: About struct fields that are assigned once and never changed again

<wYLtJ.26162$G996.16069@fx31.iad>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!weretis.net!feeder8.news.weretis.net!newsreader4.netcologne.de!news.netcologne.de!peer01.ams1!peer.ams1.xlned.com!news.xlned.com!peer01.iad!feed-me.highwinds-media.com!news.highwinds-media.com!fx31.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: About struct fields that are assigned once and never changed again
Newsgroups: comp.lang.c
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com> <sp79qr$uop$1@dont-email.me> <221ac97e-0500-45dc-867a-b469f3816712n@googlegroups.com>
Lines: 12
Message-ID: <wYLtJ.26162$G996.16069@fx31.iad>
X-Complaints-To: abuse@usenetserver.com
NNTP-Posting-Date: Mon, 13 Dec 2021 18:06:20 UTC
Organization: UsenetServer - www.usenetserver.com
Date: Mon, 13 Dec 2021 18:06:20 GMT
X-Received-Bytes: 1336
 by: Scott Lurndal - Mon, 13 Dec 2021 18:06 UTC

=?UTF-8?B?T8SfdXo=?= <oguzismailuysal@gmail.com> writes:
>On Monday, December 13, 2021 at 2:12:39 PM UTC+3, David Brown wrote:
>> (Google's broken interface messes up formatting, especially indentation.
>> If you are going to use Usenet a lot then I strongly recommend getting
>> a proper newsreader client and server. They are freely available, and
>> work much better.)
>
>Sorry. I tried Pan before but didn't like it. Which one do you use?

What's not to like about PAN, particularly when compared with GG?

I use 'xrn'.

Re: About struct fields that are assigned once and never changed again

<sp82kn$66h$1@gioia.aioe.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!aioe.org!pIAqxqP81gf5uzcAHv4HwQ.user.46.165.242.75.POSTED!not-for-mail
From: mess...@bottle.org (Guillaume)
Newsgroups: comp.lang.c
Subject: Re: About struct fields that are assigned once and never changed
again
Date: Mon, 13 Dec 2021 19:15:39 +0100
Organization: Aioe.org NNTP Server
Message-ID: <sp82kn$66h$1@gioia.aioe.org>
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@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="6353"; posting-host="pIAqxqP81gf5uzcAHv4HwQ.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.4.0
Content-Language: fr
X-Notice: Filtered by postfilter v. 0.9.2
 by: Guillaume - Mon, 13 Dec 2021 18:15 UTC

Le 13/12/2021 à 11:49, Oğuz a écrit :
> I have an array of structs as follows:
>
> struct {
> struct {
> int value;
> bool in_use;
> } values[MAX_INPUTS_SIZE];
> size_t size;
> } inputs;
>
> and a function that initializes it with user-supplied data. After the function is run, nothing in the program alters `inputs.size' or `inputs.values[N].value'. Now I have two questions about this that I can't answer myself:
> i) Is there a way to treat size and value fields as `const' outside that function?

There is an 'ugly' way of doing it.

You can qualify the struct itself (if it's going to be entirely const),
or any member of it 'const'.

Now, to initialize members at run-time, you would use pointers and
casts. For instance:

*(size_t *)(&inputs.size) = 10;

This will "override" the const qualifier. Clunky way of accessing struct
members, but it "works".

Note that this is likely undefined behavior, strictly speaking (I'll
have to check in the standard.) Depending on implementation, anything
qualified "const" may not even be put in a writable memory area. (I
think it's more likely to happen if the whole struct is qualified
"const" rather than individual members, but those are just assumptions...)

> ii) If there is, would it make any difference performance-wise? Would it allow the compiler to optimize more?

I doubt it. Maybe in some specific cases.

But anyway, the only right way of initializing "const" objects is to
initialize them at compile time (meaning, with initializers.)

If you want to "protect" struct members outside of specific functions,
nothing strictly conforming in C will work. You may try the above
approach, which is kind of messy and not guaranteed to be correct except
if you're lucky.

Re: About struct fields that are assigned once and never changed again

<sp836b$m7i$1@dont-email.me>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: jameskuy...@alumni.caltech.edu (James Kuyper)
Newsgroups: comp.lang.c
Subject: Re: About struct fields that are assigned once and never changed
again
Date: Mon, 13 Dec 2021 13:25:14 -0500
Organization: A noiseless patient Spider
Lines: 29
Message-ID: <sp836b$m7i$1@dont-email.me>
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
<sp82kn$66h$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 7bit
Injection-Date: Mon, 13 Dec 2021 18:25:15 -0000 (UTC)
Injection-Info: reader02.eternal-september.org; posting-host="1cadc526bfb859daa4447c158fee80fe";
logging-data="22770"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/sISnx4JG8DP4OD2baUq9BRLTTyA8GTas="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:oulVq69HneyZ2cqJggiDJh5md+I=
In-Reply-To: <sp82kn$66h$1@gioia.aioe.org>
Content-Language: en-US
 by: James Kuyper - Mon, 13 Dec 2021 18:25 UTC

On 12/13/21 1:15 PM, Guillaume wrote:
....
> You can qualify the struct itself (if it's going to be entirely const),
> or any member of it 'const'.
>
> Now, to initialize members at run-time, you would use pointers and
> casts. For instance:
>
> *(size_t *)(&inputs.size) = 10;
>
> This will "override" the const qualifier. Clunky way of accessing struct
> members, but it "works".
>
> Note that this is likely undefined behavior, strictly speaking (I'll
> have to check in the standard.)

Correct:
"If an attempt is made to modify an object defined with a const
qualified type through use of an lvalue with non-const-qualified type,
the behavior is undefined." (6.7.3p6).

*(size_t*)(&inputs.size) creates an lvalue of type "size_t", which is
not const-qualified. Also note that attempting to modify an object
through an lvalues that IS const-qualified is a constraint violation.

Depending on implementation, anything
> qualified "const" may not even be put in a writable memory area. (I
> think it's more likely to happen if the whole struct is qualified
> "const" rather than individual members, but those are just assumptions...)

Re: About struct fields that are assigned once and never changed again

<864k7a5l6q.fsf@linuxsc.com>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: tr.17...@z991.linuxsc.com (Tim Rentsch)
Newsgroups: comp.lang.c
Subject: Re: About struct fields that are assigned once and never changed again
Date: Wed, 15 Dec 2021 00:25:33 -0800
Organization: A noiseless patient Spider
Lines: 106
Message-ID: <864k7a5l6q.fsf@linuxsc.com>
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Injection-Info: reader02.eternal-september.org; posting-host="87e29f815fda23c6ea6c6dd22211e8ad";
logging-data="12712"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19yczn64wGykgv2vaCuB9Mgy0jKlVOKJT4="
User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.4 (gnu/linux)
Cancel-Lock: sha1:zudLzYqvzDRotLqhM5zjoNJFAEA=
sha1:barRn4gJppeuvELIue4mFoFbI5s=
 by: Tim Rentsch - Wed, 15 Dec 2021 08:25 UTC

Oğuz <oguzismailuysal@gmail.com> writes:

> I have an array of structs as follows:
>
> struct {
> struct {
> int value;
> bool in_use;
> } values[MAX_INPUTS_SIZE];
> size_t size;
> } inputs;
>
> and a function that initializes it with user-supplied data. After
> the function is run, nothing in the program alters `inputs.size'
> or `inputs.values[N].value'. Now I have two questions about this
> that I can't answer myself:
>
> i) Is there a way to treat size and value fields as `const'
> outside that function?

Here is one well-known technique. Step one: write a .h file
that defines the struct type and declares an extern pointer
variable and a function to do the initializing. Note that the
type of the pointer variable says it points to a 'const' struct:

/* file provide-struct.h */

#ifndef H_PROVIDE_STRUCT_H
#define H_PROVIDE_STRUCT_H

#include <stddef.h>

enum { MAX_INPUTS_SIZE = 10000 };

typedef struct {
struct {
int value;
_Bool in_use;
} values[ MAX_INPUTS_SIZE ];
size_t size;
} Values_Array_Struct;

extern const Values_Array_Struct *const inputs;

extern void initialize_inputs_struct( const char * );

#endif/*H_PROVIDE_STRUCT_H*/

Step two: write a .c file that defines a static instance of the
struct, along with a definition for the pointer variable and the
initializing function. Because the static struct instance is
declared (and defined) without using 'const', the initialization
function can write its values with no problem:

/* file provide-struct.c */

#include "provide-struct.h"

static Values_Array_Struct inputs_w;

const Values_Array_Struct *const inputs = &inputs_w;

void
initialize_inputs_struct( const char *data ){
inputs_w.size = 3;
inputs_w.values[0].value = 3;
inputs_w.values[2].value = 4;
inputs_w.values[0].in_use = 1;
inputs_w.values[2].in_use = 1;
/* ... etc ... */
}

Step three: write client code in a different .c file, which can
initialize the struct by calling the initialization function, and
access the struct using the declared pointer variable. Because
the pointer variable's type says it points to a 'const' struct,
the struct cannot be modified by client code:

/* file provide-struct-client.c */

#include "provide-struct.h"

#define inputs (*inputs)

unsigned
example_client( const char *data ){
unsigned i;
unsigned total = 0;

initialize_inputs_struct( data );

/* Any attempt to modify a field of 'inputs' will
be flagged by the compiler. */

for( i = 0; i < inputs.size; i++ ){
if( inputs.values[i].in_use ){
total += inputs.values[i].value;
}
}
return total;
}

Incidentally, notice the #define in this file. This macro
definition uses an idiom so the struct can be accessed using '.'
rather than '->'. Doing that lets the code look a little nicer
but isn't an essential element of the basic idea.

Re: About struct fields that are assigned once and never changed again

<spcd24$5i3$1@oguzismail.eternal-september.org>

  copy mid

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

  copy link   Newsgroups: comp.lang.c
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!oguzismail.eternal-september.org!.POSTED!not-for-mail
From: oguzisma...@gmail.com (Oğuz)
Newsgroups: comp.lang.c
Subject: Re: About struct fields that are assigned once and never changed
again
Date: Wed, 15 Dec 2021 12:38:01 +0300
Organization: A noiseless patient Spider
Lines: 93
Message-ID: <spcd24$5i3$1@oguzismail.eternal-september.org>
References: <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com>
<864k7a5l6q.fsf@linuxsc.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=utf-8; format=flowed
Content-Transfer-Encoding: 7bit
Injection-Date: Wed, 15 Dec 2021 09:38:12 -0000 (UTC)
Injection-Info: oguzismail.eternal-september.org; posting-host="5bb27b6ad7b2399311cb422d36ef5d83";
logging-data="5699"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19U8/wzce+xkdBKKdw7lpockCsKpveDv/k="
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101
Thunderbird/78.14.0
Cancel-Lock: sha1:Uvj44z3ldmk2u26l+FOZl3gYz5o=
In-Reply-To: <864k7a5l6q.fsf@linuxsc.com>
Content-Language: en-US
 by: Oğuz - Wed, 15 Dec 2021 09:38 UTC

On 12/15/21 11:25 AM, Tim Rentsch wrote:
> Here is one well-known technique. Step one: write a .h file
> that defines the struct type and declares an extern pointer
> variable and a function to do the initializing. Note that the
> type of the pointer variable says it points to a 'const' struct:
>
> /* file provide-struct.h */
>
> #ifndef H_PROVIDE_STRUCT_H
> #define H_PROVIDE_STRUCT_H
>
> #include <stddef.h>
>
> enum { MAX_INPUTS_SIZE = 10000 };
>
> typedef struct {
> struct {
> int value;
> _Bool in_use;
> } values[ MAX_INPUTS_SIZE ];
> size_t size;
> } Values_Array_Struct;
>
> extern const Values_Array_Struct *const inputs;
>
> extern void initialize_inputs_struct( const char * );
>
> #endif/*H_PROVIDE_STRUCT_H*/
>
> Step two: write a .c file that defines a static instance of the
> struct, along with a definition for the pointer variable and the
> initializing function. Because the static struct instance is
> declared (and defined) without using 'const', the initialization
> function can write its values with no problem:
>
> /* file provide-struct.c */
>
> #include "provide-struct.h"
>
> static Values_Array_Struct inputs_w;
>
> const Values_Array_Struct *const inputs = &inputs_w;
>
> void
> initialize_inputs_struct( const char *data ){
> inputs_w.size = 3;
> inputs_w.values[0].value = 3;
> inputs_w.values[2].value = 4;
> inputs_w.values[0].in_use = 1;
> inputs_w.values[2].in_use = 1;
> /* ... etc ... */
> }
>
> Step three: write client code in a different .c file, which can
> initialize the struct by calling the initialization function, and
> access the struct using the declared pointer variable. Because
> the pointer variable's type says it points to a 'const' struct,
> the struct cannot be modified by client code:
>
> /* file provide-struct-client.c */
>
> #include "provide-struct.h"
>
> #define inputs (*inputs)
>
> unsigned
> example_client( const char *data ){
> unsigned i;
> unsigned total = 0;
>
> initialize_inputs_struct( data );
>
> /* Any attempt to modify a field of 'inputs' will
> be flagged by the compiler. */
>
> for( i = 0; i < inputs.size; i++ ){
> if( inputs.values[i].in_use ){
> total += inputs.values[i].value;
> }
> }
> return total;
> }
>
> Incidentally, notice the #define in this file. This macro
> definition uses an idiom so the struct can be accessed using '.'
> rather than '->'. Doing that lets the code look a little nicer
> but isn't an essential element of the basic idea.
>

This and others all worked fine and I learned a lot from this thread,
thank you all. And for the record, neither making those fields const nor
separating `inputs' into two arrays made the program any faster than it
already was :/

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor