B
bill
I'm not sure if this belongs in comp.lang.c or in a compiler group, but
I'm experiencing some confusion. Consider:
[tmp]$ cat a.c
const char t[]={0xef, 0xbe, 0xad, 0xde};
const char *a=t;
int bar(void);
int
main()
{
return bar();
}
[tmp]$ cat b.c
extern const char *t;
int
bar(void)
{
return t[0];
}
[tmp]$ gcc -c b.c
[tmp]$ gcc a.c b.o
[tmp]$ ./a.out
Segmentation fault
Looking at the running process through gdb, we see that the segfault
occurs because the attempt to reference t[0] in bar attempts to
dereference the address 0xdeadbeef, which is an illegal address. If
we replace 't' with 'a' in baz, everything works fine. I'm trying to
understand the proper way to initialize constant arrays that are used
accross multiple files (don't want to define them as static as I end up
with multiple copies in the a.out, which I suppose is compiler
dependent and gcc is not doing the right thing with -fmerge-constants
(well, I called it as -Os, and my reading of the man page is that this
implies -fmerge-constants)).
Any insight appreciated.
I'm experiencing some confusion. Consider:
[tmp]$ cat a.c
const char t[]={0xef, 0xbe, 0xad, 0xde};
const char *a=t;
int bar(void);
int
main()
{
return bar();
}
[tmp]$ cat b.c
extern const char *t;
int
bar(void)
{
return t[0];
}
[tmp]$ gcc -c b.c
[tmp]$ gcc a.c b.o
[tmp]$ ./a.out
Segmentation fault
Looking at the running process through gdb, we see that the segfault
occurs because the attempt to reference t[0] in bar attempts to
dereference the address 0xdeadbeef, which is an illegal address. If
we replace 't' with 'a' in baz, everything works fine. I'm trying to
understand the proper way to initialize constant arrays that are used
accross multiple files (don't want to define them as static as I end up
with multiple copies in the a.out, which I suppose is compiler
dependent and gcc is not doing the right thing with -fmerge-constants
(well, I called it as -Os, and my reading of the man page is that this
implies -fmerge-constants)).
Any insight appreciated.