Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Initializing a malloc'ed struct whose fields are const with run-timevalues (http://www.velocityreviews.com/forums/t956793-initializing-a-malloced-struct-whose-fields-are-const-with-run-timevalues.html)

Noob 01-22-2013 03:36 PM

Initializing a malloc'ed struct whose fields are const with run-timevalues
 
Hello,

Is it possible, in standard C89 and C99, to initialize a
struct whose fields are const with values only known at
run-time?

For example, consider:

struct toto { const int i; const float f; const void *p; };

Is it possible to portably implement:

struct toto *foo(int i, float f, void *p);

which allocates space for a "struct toto", initializes it
with i, f, p and returns the address of this struct?

The best I could come up with is:

#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);
return res;
}

which has several defects:

1) Apparently, C89 does not allow one to use elements "not computable
at load time" in an initialization list. However, it is allowed both
in C99 (right?) and in gnu89.

2) I'm not sure it is well-defined to memcpy stuff into some const
fields. Does it tickle the cranky UB gods?

Regards.

Shao Miller 01-22-2013 04:23 PM

Re: Initializing a malloc'ed struct whose fields are const with run-timevalues
 
On 1/22/2013 10:36, Noob wrote:
> Hello,
>
> Is it possible, in standard C89 and C99, to initialize a
> struct whose fields are const with values only known at
> run-time?
>
> For example, consider:
>
> struct toto { const int i; const float f; const void *p; };
>


There is probably a better example, since none of the members here are
non-'const'. You could just have these members non-'const'-qualified
and return a 'const struct toto *', possibly with a cast. I'll assume
you have at least one non-'const' member for further discussion.

> Is it possible to portably implement:
>
> struct toto *foo(int i, float f, void *p);
>
> which allocates space for a "struct toto", initializes it
> with i, f, p and returns the address of this struct?
>
> The best I could come up with is:
>
> #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);
> return res;
> }
>


How about:

#include <stddef.h>
#include <stdlib.h>

struct toto {
const int i;
const float f;
const void * p;
};

struct toto * foo(int i, float f, void * p) {
unsigned char * obj;

obj = malloc(sizeof (struct toto));
if (obj) {
*(int *) (obj + offsetof(struct toto, i)) = i;
*(float *) (obj + offsetof(struct toto, f)) = f;
*(void **) (obj + offsetof(struct toto, i)) = p;
}
return (void *) obj;
}

int main(void) {
struct toto * test = foo(42, 3.14, NULL);
free(test);
return 0;
}

> which has several defects:
>
> 1) Apparently, C89 does not allow one to use elements "not computable
> at load time" in an initialization list. However, it is allowed both
> in C99 (right?) and in gnu89.
>


The initializers in an initializer list for an aggregate or union must
be constant expressions, and no, I do not believe that's changed in any
newer Standard.

> 2) I'm not sure it is well-defined to memcpy stuff into some const
> fields.
>


You can 'memcpy' into allocated storage just fine. In C >= C99, you
need to be concerned with effective type, so it's not completely
straight-forward.

> Does it tickle the cranky UB gods?
>


This is one of the funnier things I've read in comp.lang.c! :)

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

Cheerily," -- Richard Harter

Ike Naar 01-22-2013 05:59 PM

Re: Initializing a malloc'ed struct whose fields are const withrun-time values
 
On 2013-01-22, Noob <root@127.0.0.1> 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;

> 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;
}

Shao Miller 01-22-2013 07:09 PM

Re: Initializing a malloc'ed struct whose fields are const with run-timevalues
 
On 1/22/2013 12:59, Ike Naar wrote:
> On 2013-01-22, Noob <root@127.0.0.1> 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.

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

Cheerily," -- Richard Harter

Ian Collins 01-22-2013 08:43 PM

Re: Initializing a malloc'ed struct whose fields are const withrun-time values
 
Ike Naar wrote:
> On 2013-01-22, Noob <root@127.0.0.1> 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;
>
>> 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;
> }


Is that legal? struct toto has const members, so can it be returned by
value from a function?

--
Ian Collins

Noob 01-22-2013 08:59 PM

Re: Initializing a malloc'ed struct whose fields are const with run-timevalues
 
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).

Regards.


Ben Bacarisse 01-22-2013 09:02 PM

Re: Initializing a malloc'ed struct whose fields are const with run-time values
 
Ike Naar <ike@sverige.freeshell.org> writes:

> On 2013-01-22, Noob <root@127.0.0.1> 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 it'll provoke an error! The struct's members are const so the
assignment is a constraint violation.

>> 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 limits what you can do with it for the same reason. You can't
assign a strust toto because of its const members.

--
Ben.

Ben Bacarisse 01-22-2013 09:05 PM

Re: Initializing a malloc'ed struct whose fields are const with run-time values
 
Shao Miller <sha0.miller@gmail.com> writes:

> On 1/22/2013 12:59, Ike Naar wrote:
>> On 2013-01-22, Noob <root@127.0.0.1> 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? ;)


But in front of the scenes, they are very different! The assignment is
a constraint violation.

<snip>
--
Ben.

Noob 01-22-2013 09:10 PM

Re: Initializing a malloc'ed struct whose fields are const with run-timevalues
 
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;


Right... I'll have to try that, and see what gcc thinks.

> And how about returning by value?
>
> struct toto foo(int i, float f, void *p)
> {
> struct toto res = {i, f, p};
> return res;
> }


This won't work for me, I have to manage the struct's
lifetime by hand, using dynamic allocation. (See my reply
to Shao for a complete description.)

Regards.


Shao Miller 01-22-2013 09:12 PM

Re: Initializing a malloc'ed struct whose fields are const with run-timevalues
 
On 1/22/2013 16:02, Ben Bacarisse wrote:
> Ike Naar <ike@sverige.freeshell.org> writes:
>
>> On 2013-01-22, Noob <root@127.0.0.1> 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 it'll provoke an error! The struct's members are const so the
> assignment is a constraint violation.
>
>>> 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 limits what you can do with it for the same reason. You can't
> assign a strust toto because of its const members.
>


Are you sure about that? 'res' undergoes lvalue conversion and no
longer has qualified type.

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

Cheerily," -- Richard Harter


All times are GMT. The time now is 02:34 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57