Re: [arm-gnu] Getting linker data into a C structure
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [arm-gnu] Getting linker data into a C structure



On Tuesday, May 11, 2010 05:19:54 am Nick Clifton wrote:
> Simply put it is because the value of "foo" in the first two cases might
> change, but the value of "foo" in the last case will not.  But, you will
> say, the const keyword makes the first two value constant.  Not
> necessarily.  Consider this example:
> 
>    % cat a.c
>    extern const int a;
>    extern void foo (void);
>    int
>    main (void)
>    {
>      printf ("a = %d\n", a);
>      foo ();
>      printf ("a = %d\n", a);
>      return 0;
>    }
> 
>    % cat b.c
>    int a = 1;
>    void foo (void) { a = 2; }
> 
>    % gcc a.c b.c
>    % ./a.out
>    a = 1
>    a = 2
> 
> Try changing this test to use arrays and it will fail to compile.  You
> can change the value of integer types like "long" and pointers like
> "long *", but not of arrays like "long[]".

See, in my mind this is a linker error, as the symbol information for 'a' 
should list it as constant for a.o. b.o wouldn't have the same symbol 
information for the symbol 'a' and thus the linking step should fail.

I do realize that what is in my mind and what everyone else endures isn't the 
same thing. :-)

> If you want to be more precise the behaviour is because the C language
> specification says that this is how the compiler should behave.  The
> ISO-C99 standard says this:

[ spec snipped ]

>     An address constant is a null pointer, a pointer to an lvalue
>     designating an object of static storage duration, or a pointer
>     to a function designator; it shall be created explicitly using
>     the unary & operator or an integer constant cast to pointer
>     type, or implicitly by the use of an expression of array or
>     function type.  The array-subscript [] and member-access .
>     and -> operators, the address & and indirection * unary
>     operators, and pointer casts may be used in the creation of
>     an address constant, but the value of an object shall not be
>     accessed by use of these operators.

> This last paragraph is the crucial one.  It allows arrays in the
> initializer expression but not pointers or scalar types.  (Technically
> pointers are scalar types).

I see. I really do appreciate your detailed and thorough response to my 
question! It certainly gives an authoritative answer to my question and, if I 
redefine my internal definition of "const" it even makes sense to me. :-)

Thank you again, I really do appreciate you taking the time to answer my 
question so thoroughly.

Regards,
Andrew