Address versus expression at address in static initializer in C

(This question was inspired by a research earlier question )

I have a sample code that initializes two global static variables: one is a pointer to an external variable, the other is an expression computed from this pointer:

#include <stdint.h>

#define UNCACHE_MASK 0xABCDEF12UL // Value of the mask to apply

extern int memory_area;

const void * virtual_address = &memory_area;

const uintptr_t int_address = ((uintptr_t)&memory_area) | UNCACHE_MASK;

When compiling, I get the following:

$ gcc -c  test.c
test.c:6:1: error: initializer element is not computable at load time
 const uintptr_t int_address = ((uintptr_t)&memory_area) | UNCACHE_MASK;
 ^

I am not very expert in C, but it seems that if it &memory areais good for initialization virtual_address, it should also be useful for initialization int_address.

What am I missing?

(gcc version 4.8.2, Cygwin on Win 7)

+1
source share
1 answer

C ( ), ( ). , "- ". (uintptr_t) &memory_area , . .

, , &memory_area , (uintptr_t) &memory_area .

, GCC .

+3

All Articles