Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.c > #30556

Re: Initializing a malloc'ed struct whose fields are const with run-time values

From Shao Miller <sha0.miller@gmail.com>
Newsgroups comp.lang.c
Subject Re: Initializing a malloc'ed struct whose fields are const with run-time values
Date 2013-01-22 23:47 -0500
Organization A noiseless patient Spider
Message-ID <kdnq18$vc6$1@dont-email.me> (permalink)
References <kdmbl7$4lc$1@dont-email.me> <slrn3vfskftksd.ddo.ike@sverige.freeshell.org> <kdmo4v$vm5$1@dont-email.me> <50fefdb9$0$16499$426a34cc@news.free.fr>

Show all headers | View raw


On 1/22/2013 15:59, Noob wrote:
> Shao Miller wrote:
>> On 1/22/2013 12:59, Ike Naar wrote:
>>> On 2013-01-22, Noob wrote:
>>>> #include <stdlib.h>
>>>> #include <string.h>
>>>> struct toto *foo(int i, float f, void *p)
>>>> {
>>>>     struct toto s = { i, f, p };
>>>>     struct toto *res = malloc(sizeof *res);
>>>>     if (res != NULL) memcpy(res, &s, sizeof s);
>>>
>>> Why memcpy instead of an assignment?
>>>       if (res != NULL) *res = s;
>>
>> Because they amount to the same thing, behind the scenes, on a few
>> implementations? ;)  Lots of people still do:
>>
>>     struct tag s;
>>     memset(&s, 0, sizeof s);
>>
>> instead of:
>>
>>     struct tag s = { 0 };
>>
>> But the latter seems better (when possible).
>>
>>>>     return res;
>>>> }
>>>
>>> And how about returning by value?
>>>
>>>     struct toto foo(int i, float f, void *p)
>>>     {
>>>       struct toto res = {i, f, p};
>>>       return res;
>>>     }
>>>
>>
>> This has the same problem with the initializer list, though.
>>
>> I would guess that 'malloc' was being used for lifetime considerations,
>> but obviously only Noob knows.
>
> As I often do, my simplification went a bit too far, so I'll just
> describe the actual use-case.
> (This involves two threads on a POSIX-compliant platform.)
>
> I have a foo_start function which malloc's space for a "context"
> struct, populates the struct according to the function's parameters,
> then spawns a new thread which is passed this context. (This is why
> dynamic allocation must be used.)
>
> To make matters more complex, I have a flexible array at the end of
> the struct.
>
> Basically, struct ctx is defined this way:
>
> struct ctx {
>    int file_idx;
>    const char *buf;
>    sem_t sem;
>    char path[];
> };
>
> void *foo_run(void *arg) {
>    struct ctx *ctx = arg;
>    do stuff in an infinite loop, according to ctx
> }
>
> void foo_start(int param1, int param2, ...)
> {
>    struct ctx *ctx = malloc(sizeof *ctx + paramx);
>    populate the fields of ctx;
>    spawn(foo_run, ctx);
> }
>
> In my current version, I don't have any const qualifiers in my code.
> I like to look at the assembly code generated by the compiler, and I
> noticed that every time I need some field from ctx, the compiler has
> to reload it, instead of caching the value in a register.
>
> As far as I understand, this is expected: the address of the struct
> could be stored anywhere, and any function in a different translation
> unit could "pull the rug" from under me. Except that *I* wrote the
> code, and I *know* (by design) that most fields in the struct do NOT
> change after init.
>
> So I set out to sprinkle a few "const" qualifiers here and there, to
> see if that would convince the compiler that some optimizations are
> indeed possible (I know this looks like a clear case of premature
> optimization, but I figured I might as well learn something new along
> the way!)
>
> I don't think I can just const-qualify the entire struct, because
> 1) the prototype for a thread's entry point is imposed by POSIX
> 2) const-qualifying a pointer parameter is only a contract between
> the user and the function's implementer, saying "my function won't
> touch your preciousss struct", it doesn't say that the struct won't
> be changed by something else.
>
> Whereas, I was under the impression that a const-qualified field
> means "hear, hear, this field SHALL NEVER change!"
>
> Anyway, I'd be happy to hear the word from the regs, and/or take
> this to comp.unix.programmer at some point (although I do believe
> that the core of my question is a C question, not POSIX).
>

Although I gave the wrong answer about C >= C99 aggregate/union 
initializer-list, if you'll still permit, I'd like to offer some 
thoughts on why using 'offsetof' (as a previous post did) might have 
benefit:

   1. Your non-constant initializers require C >= C99.

   2. The work of initialization and then copying to allocated storage 
is more work (but prettier, perhaps) than assigning directly to the 
allocated storage, unless working with automatic objects has a special 
advantage in terms of speed.

   3. For C >= C99, the effective types for the sub-objects in the 
allocated storage will remain modifiable, but the 'struct toto *' used 
to access any of those will only allow 'const' access; perhaps the 
artificial limitation you desire?  "Internal" functions could use 
'offsetof', if needed.

Using 'memcpy' from another 'struct toto' establishes the effective type 
of the allocated storage and then it can be argued that there are 
complications from pointing into the storage with a pointer to an 
unqualified referenced type, then using such pointers to read the value. 
  (Although I think that argument would disappear if you use a 'const 
int *' to read the 'const int' sub-object of the allocated storage, 
instead.)

-- 
- Shao Miller
--
"Thank you for the kind words; those are the kind of words I like to hear.

Cheerily,"  -- Richard Harter

Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

Initializing a malloc'ed struct whose fields are const with run-time values Noob <root@127.0.0.1> - 2013-01-22 16:36 +0100
  Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 11:23 -0500
    Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-22 21:26 +0000
      Re: Initializing a malloc'ed struct whose fields are const with run-time values James Kuyper <jameskuyper@verizon.net> - 2013-01-22 16:41 -0500
    Re: Initializing a malloc'ed struct whose fields are const with run-time values William Ahern <william@wilbur.25thandClement.com> - 2013-01-22 13:25 -0800
      Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 23:17 -0500
    Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 13:09 +0000
      Re: Initializing a malloc'ed struct whose fields are const with run-time values Noob <root@127.0.0.1> - 2013-01-23 14:16 +0100
        Re: Initializing a malloc'ed struct whose fields are const with run-time values "BartC" <bc@freeuk.com> - 2013-01-23 13:25 +0000
          Re: Initializing a malloc'ed struct whose fields are const with run-time values Noob <root@127.0.0.1> - 2013-01-23 15:21 +0100
            Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 14:56 +0000
              Re: Initializing a malloc'ed struct whose fields are const with run-time values Noob <root@127.0.0.1> - 2013-01-23 16:40 +0100
                Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 18:26 +0000
                Re: Initializing a malloc'ed struct whose fields are const with run-time values Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-23 20:39 -0800
            Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-23 17:14 -0500
  Re: Initializing a malloc'ed struct whose fields are const with run-time values Ike Naar <ike@sverige.freeshell.org> - 2013-01-22 17:59 +0000
    Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 14:09 -0500
      Re: Initializing a malloc'ed struct whose fields are const with run-time values Noob <root@localhost> - 2013-01-22 21:59 +0100
        Re: Initializing a malloc'ed struct whose fields are const with run-time values glen herrmannsfeldt <gah@ugcs.caltech.edu> - 2013-01-22 21:19 +0000
        Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 23:47 -0500
      Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-22 21:05 +0000
        Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 16:15 -0500
    Re: Initializing a malloc'ed struct whose fields are const with   run-time values Ian Collins <ian-news@hotmail.com> - 2013-01-23 09:43 +1300
      Re: Initializing a malloc'ed struct whose fields are const with   run-time values William Ahern <william@wilbur.25thandClement.com> - 2013-01-22 13:19 -0800
        Re: Initializing a malloc'ed struct whose fields are const with   run-time values William Ahern <william@wilbur.25thandClement.com> - 2013-01-22 13:35 -0800
          Re: Initializing a malloc'ed struct whose fields are const with   run-time values Ian Collins <ian-news@hotmail.com> - 2013-01-23 10:45 +1300
            Re: Initializing a malloc'ed struct whose fields are const with   run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 17:07 -0500
            Re: Initializing a malloc'ed struct whose fields are const with  run-time values Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-22 21:29 -0800
              Re: Initializing a malloc'ed struct whose fields are const with  run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 13:54 +0000
                Re: Initializing a malloc'ed struct whose fields are const with  run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-23 17:08 -0500
                Re: Initializing a malloc'ed struct whose fields are const with    run-time values Ian Collins <ian-news@hotmail.com> - 2013-01-24 11:29 +1300
                Re: Initializing a malloc'ed struct whose fields are const with    run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-23 18:26 -0500
                Re: Initializing a malloc'ed struct whose fields are const with      run-time values Ian Collins <ian-news@hotmail.com> - 2013-01-24 14:03 +1300
                Re: Initializing a malloc'ed struct whose fields are const with      run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-23 23:58 -0500
                Re: Initializing a malloc'ed struct whose fields are const with  run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 23:36 +0000
                Re: Initializing a malloc'ed struct whose fields are const with  run-time values Ian Collins <ian-news@hotmail.com> - 2013-01-24 13:55 +1300
                Re: Initializing a malloc'ed struct whose fields are const with  run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-24 02:44 +0000
                Re: Initializing a malloc'ed struct whose fields are const with  run-time values Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-23 21:39 -0800
                Re: Initializing a malloc'ed struct whose fields are const with  run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-24 00:54 -0500
                Re: Initializing a malloc'ed struct whose fields are const with  run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-24 17:56 +0000
                Re: Initializing a malloc'ed struct whose fields are const with  run-time values Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-24 13:58 -0800
        Re: Initializing a malloc'ed struct whose fields are const with   run-time values James Kuyper <jameskuyper@verizon.net> - 2013-01-22 16:37 -0500
          Re: Initializing a malloc'ed struct whose fields are const with  run-time values Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-22 21:22 -0800
      Re: Initializing a malloc'ed struct whose fields are const with  run-time values Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-22 21:06 -0800
        Re: Initializing a malloc'ed struct whose fields are const with  run-time values Ian Collins <ian-news@hotmail.com> - 2013-01-23 19:55 +1300
          Re: Initializing a malloc'ed struct whose fields are const with  run-time values Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-23 00:48 -0800
            Re: Initializing a malloc'ed struct whose fields are const with  run-time values Ian Collins <ian-news@hotmail.com> - 2013-01-23 23:06 +1300
    Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-22 21:02 +0000
      Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 16:12 -0500
        Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-22 22:37 +0000
          Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 22:29 -0500
            Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 04:28 +0000
              Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-23 00:07 -0500
                Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 14:04 +0000
    Re: Initializing a malloc'ed struct whose fields are const with run-time values Noob <root@localhost> - 2013-01-22 22:10 +0100
  Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-22 21:13 +0000
    Re: Initializing a malloc'ed struct whose fields are const with run-time values Shao Miller <sha0.miller@gmail.com> - 2013-01-22 16:25 -0500
      Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-22 22:21 +0000
    Re: Initializing a malloc'ed struct whose fields are const with run-time values William Ahern <william@wilbur.25thandClement.com> - 2013-01-22 13:32 -0800
      Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 03:50 +0000
    Re: Initializing a malloc'ed struct whose fields are const with run-time values Nick Bowler <nbowler@draconx.ca> - 2013-01-22 21:43 +0000
      Re: Initializing a malloc'ed struct whose fields are const with run-time values Ben Bacarisse <ben.usenet@bsb.me.uk> - 2013-01-23 03:45 +0000
        Re: Initializing a malloc'ed struct whose fields are const with run-time values Keith Thompson <kst-u@mib.org> - 2013-01-24 08:23 -0800
  Re: Initializing a malloc'ed struct whose fields are const with run-time values Tim Rentsch <txr@alumni.caltech.edu> - 2013-01-22 20:48 -0800

csiph-web