Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.c > #163826
| From | Guillaume <message@bottle.org> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: About struct fields that are assigned once and never changed again |
| Date | 2021-12-13 19:15 +0100 |
| Organization | Aioe.org NNTP Server |
| Message-ID | <sp82kn$66h$1@gioia.aioe.org> (permalink) |
| References | <56b509db-c5ba-4020-b1e4-6250d41a706cn@googlegroups.com> |
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.
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
About struct fields that are assigned once and never changed again Oğuz <oguzismailuysal@gmail.com> - 2021-12-13 02:49 -0800
Re: About struct fields that are assigned once and never changed again Oğuz <oguzismailuysal@gmail.com> - 2021-12-13 02:50 -0800
Re: About struct fields that are assigned once and never changed again Malcolm McLean <malcolm.arthur.mclean@gmail.com> - 2021-12-13 03:05 -0800
Re: About struct fields that are assigned once and never changed again David Brown <david.brown@hesbynett.no> - 2021-12-13 12:12 +0100
Re: About struct fields that are assigned once and never changed again Oğuz <oguzismailuysal@gmail.com> - 2021-12-13 09:01 -0800
Re: About struct fields that are assigned once and never changed again David Brown <david.brown@hesbynett.no> - 2021-12-13 18:37 +0100
Re: About struct fields that are assigned once and never changed again scott@slp53.sl.home (Scott Lurndal) - 2021-12-13 18:06 +0000
Re: About struct fields that are assigned once and never changed again Ben Bacarisse <ben.usenet@bsb.me.uk> - 2021-12-13 16:12 +0000
Re: About struct fields that are assigned once and never changed again James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-13 12:09 -0500
Re: About struct fields that are assigned once and never changed again Guillaume <message@bottle.org> - 2021-12-13 19:15 +0100
Re: About struct fields that are assigned once and never changed again James Kuyper <jameskuyper@alumni.caltech.edu> - 2021-12-13 13:25 -0500
Re: About struct fields that are assigned once and never changed again Tim Rentsch <tr.17687@z991.linuxsc.com> - 2021-12-15 00:25 -0800
Re: About struct fields that are assigned once and never changed again Oğuz <oguzismailuysal@gmail.com> - 2021-12-15 12:38 +0300
csiph-web