In GCC, If a variable is declared with weak attribute, and, in (static) linking time, no definition is found, the variable will have address zero, i.e., if a pointer is initialized with the address of the variable, the pointer will be NULL, as the following code snippets illustrate:
foobar.c:
extern int foo __attribute__((weak));
extern int bar;
int *a[] = {&foo, &bar};
main.c:
#include <diag/Trace.h>
//int foo;
int bar;
extern int *a[];
int main(void) {
trace_printf("%p, %p", a[0], a[1]);
return 0;
}
The output is: 0, 0x20000120 (I'm using arm-none-eabi-gcc 5.4.1)
The question is: although the behavior is expected, no document mentioned it. Could anyone point me to any material explaining this behavior? Thanks!