initializing pointer in struct with address of auto identifier 
Author Message
 initializing pointer in struct with address of auto identifier

Hello,

playing around with some code I got following message from gcc:

x.c:11: initializer element is not computable at load time
x.c:11: (near initialization for `vars[0]')

#include <stdio.h>

struct lookup {
  int *p;

Quote:
};

int main(void)
{
  int foo = 0;
  struct lookup vars[] =  {
    {&foo},
  };

  printf("%p\n",vars);

  return 0;

Quote:
}

I thought that the address of foo should be computable
pretty easy. Anyhow I probably missunderstand this message.

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sat, 25 Oct 2003 20:13:21 GMT  
 initializing pointer in struct with address of auto identifier


Quote:
> Hello,

> playing around with some code I got following message from gcc:

> x.c:11: initializer element is not computable at load time
> x.c:11: (near initialization for `vars[0]')

[some vertical compression here]

Quote:
> #include <stdio.h>

> struct lookup { int *p; };

> int main(void)
> {
>   int foo = 0;
>   struct lookup vars[] =  { {&foo},};
>   printf("%p\n",vars);
>   return 0;
> }

> I thought that the address of foo should be computable
> pretty easy. Anyhow I probably missunderstand this message.

Note the "at load time" qualifier; `&foo` cannot be computed until `main`
is called, which is (not very) long after load time (which, in Unix-speak,
usually means when the program is *linked*, not when it is *run*, if I
recall correctly).

And ISO C requires structure literals to be load-time constants, not
run-time values. Which is, of course, a pain in the tentacles.

--
Chris "how else can one count to 16?" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Sat, 25 Oct 2003 20:23:24 GMT  
 initializing pointer in struct with address of auto identifier

Quote:

> x.c:11: initializer element is not computable at load time
> x.c:11: (near initialization for `vars[0]')

     Indeed.  Use static storage instead.

Tak-Shing



Sat, 25 Oct 2003 20:34:31 GMT  
 initializing pointer in struct with address of auto identifier

Quote:

> playing around with some code I got following message from gcc:

> x.c:11: initializer element is not computable at load time
> x.c:11: (near initialization for `vars[0]')

> #include <stdio.h>

> struct lookup {
>   int *p;
> };

> int main(void)
> {

#define MAXVARS 5 /* say */

Quote:
>   int foo = 0;
>   struct lookup vars[] =  {
>     {&foo},
>   };

    struct lookup vars[MAXVARS + 1];

    vars[0].p = &foo;

Quote:

>   printf("%p\n",vars);

    printf("%p\n", vars[0].p); /* Maybe this is what you want */

Quote:
>   return 0;
> }

> I thought that the address of foo should be computable
> pretty easy. Anyhow I probably missunderstand this message.

It is probably something like "stack_frame_ptr + constant".  How
can the compiler know the value of stack_frame_ptr at compile
time?

--

http://www.qwikpages.com/backstreets/cbfalconer :=(down for now)
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Sat, 25 Oct 2003 21:20:18 GMT  
 initializing pointer in struct with address of auto identifier


Quote:

>> x.c:11: initializer element is not computable at load time
>> x.c:11: (near initialization for `vars[0]')

>      Indeed.  Use static storage instead.

(quoting more context would be useful, so ...)

Quote:
> int main(void)
> {
>   int foo = 0;
>   struct lookup vars[] =  { {&foo},};
>   printf("%p\n",vars);
>   return 0;
> }

While this may solve the particular problem Zoran had, in that it shuts
up the compiler message, it doesn't explain *why* he gets that message,
and it isn't a general solution -- he may want a different instance of
the structure in a recursive call, for example.

(Indeed, I have just such a one to hand, but I would rather not display
its ikky layout in this margin. I mean posting.)

--
Chris "three initial values that can't be member initialisers, pah" Dollin
C FAQs at: http://www.faqs.org/faqs/by-newsgroup/comp/comp.lang.c.html



Sat, 25 Oct 2003 21:40:41 GMT  
 initializing pointer in struct with address of auto identifier

Quote:

>> playing around with some code I got following message from
>> gcc:

>> x.c:11: initializer element is not computable at load time
>> x.c:11: (near initialization for `vars[0]')

>> #include <stdio.h>

>> struct lookup {
>>   int *p;
>> };

>> int main(void)
>> {

> #define MAXVARS 5 /* say */

>>   int foo = 0;
>>   struct lookup vars[] =  {
>>     {&foo},
>>   };

>     struct lookup vars[MAXVARS + 1];

>     vars[0].p = &foo;

Well, I know that this works, but I wanted to know why my
code did not work. Anyhow Chris Dollin gave a good explanation
what is wrong with my code. The sollution you offer
will need extra mantainanced for the array size which I wanted
to avoid.

Quote:

>>   printf("%p\n",vars);

>     printf("%p\n", vars[0].p); /* Maybe this is what you want
>     */

No it's not, that was just to make the compiler silent about
unused variables.

Quote:

>>   return 0;
>> }

>> I thought that the address of foo should be computable pretty
>> easy. Anyhow I probably missunderstand this message.

> It is probably something like "stack_frame_ptr + constant".
> How can the compiler know the value of stack_frame_ptr at
> compile time?

Well, the compiler can't know about it at compile time or link
time, which obviously is needed for a struct initializer. Anyhow
we can use an address of a auto identifier for initializing a
simple pointer. In future I simply have to recall that structs
need to be initialized with compile time constants.

Does this change with C99?

--

"LISP  is worth learning for  the profound enlightenment  experience
you will have when you finally get it; that experience will make you
a better programmer for the rest of your days."   -- Eric S. Raymond



Sat, 25 Oct 2003 23:08:26 GMT  
 initializing pointer in struct with address of auto identifier


Quote:
>Hello,

>playing around with some code I got following message from gcc:

>x.c:11: initializer element is not computable at load time
>x.c:11: (near initialization for `vars[0]')

>#include <stdio.h>

>struct lookup {
>  int *p;
>};

>int main(void)
>{
>  int foo = 0;
>  struct lookup vars[] =  {
>    {&foo},
>  };

>  printf("%p\n",vars);

%p requires an argument of type void *. A cast is appropriate here.

Quote:
>  return 0;

>}

>I thought that the address of foo should be computable
>pretty easy. Anyhow I probably missunderstand this message.

In C90 the initialisers for structures, unions and arrays must be constant
expressions. The address of an automatic variable is not a constant
expression but thee address of a static variable is.

In C99 initialisers for automatic structures, unions and arrays are no
longer required to be constant expresisons.

--
-----------------------------------------


-----------------------------------------



Sun, 26 Oct 2003 20:27:45 GMT  
 initializing pointer in struct with address of auto identifier
Groovy hepcat CBFalconer was jivin' on Tue, 08 May 2001 13:20:18 GMT
in comp.lang.c.
Re: initializing pointer in struct with address of auto identifier's a
cool scene! Dig it!

Quote:
>> int main(void)
>> {
>#define MAXVARS 5 /* say */

>>   int foo = 0;
>>   struct lookup vars[] =  {
>>     {&foo},
>>   };

>    struct lookup vars[MAXVARS + 1];

  Why MAXVARS + 1?

Quote:
>    vars[0].p = &foo;
>>   printf("%p\n",vars);

>    printf("%p\n", vars[0].p); /* Maybe this is what you want */

  I doubt that he wants that, since it invokes undefined behaviour.
printf() here expects a void *, but you're giving it an int *. No
conversion is done on a pointer passed into a variadic function. And
int * is not guaranteed to be compatible with void *.
  Solution: cast the pointer to the correct type.

    printf("%p\n", (void *)vars[0].p);
--

Dig the even newer still, yet more improved, sig!

http://alphalink.com.au/~phaywood/
"Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
I know it's not "technically correct" English; but since when was rock & roll "technically correct"?



Wed, 29 Oct 2003 08:51:00 GMT  
 initializing pointer in struct with address of auto identifier

Quote:

> Groovy hepcat CBFalconer was jivin' on Tue, 08 May 2001 13:20:18 GMT
> in comp.lang.c.
> Re: initializing pointer in struct with address of auto identifier's a
> cool scene! Dig it!

> >> int main(void)
> >> {
> >#define MAXVARS 5 /* say */

> >>   int foo = 0;
> >>   struct lookup vars[] =  {
> >>     {&foo},
> >>   };

> >    struct lookup vars[MAXVARS + 1];

>   Why MAXVARS + 1?

because I would reuse the MAXVARS value elsewhere, and this way it
signifies the maximum index available.  Just avoids inadverdent
out-of-range indexing problems.  Depends on your habits.

--

http://www.qwikpages.com/backstreets/cbfalconer :=(down for now)
   (Remove "NOSPAM." from reply address. my-deja works unmodified)



Wed, 29 Oct 2003 17:38:40 GMT  
 
 [ 9 post ] 

 Relevant Pages 

1. Initializing const auto structs

2. Initializing a pointer inside a struct

3. initialize a struct pointer

4. Initialize struct with struct

5. pointers to functions in structs which take a pointer to that struct as an arg

6. cast pointer to struct to pointer to type of first member of struct

7. Auto variable address as initializer for auto struct field?

8. Trouble with auto initializing

9. memory leak: pointer->pointer->pointer->struct

10. memory Leak: pointer->pointer->pointer->struct

11. initialize a pointer to pointer

12. Address of auto not constant initializer?

 

 
Powered by phpBB® Forum Software