Reputation: 11
When I was reading the 7th chapter of CSAPP, I came across a problem that I still can't solve till now.
foo.c
#include <stdio.h>
void f(void);
int y = 15212;
int x = 15213;
int main(void)
{
f();
printf("x = 0x%x y = 0x%x\n", x, y);
printf("x address = %p, y address = %p", &x, &y);
return 0;
}
bar3.c
double x;
void f()
{
x = -0.0;
}
I compiled these two pieces with options shown below. Providing IEEE754 expression and some knowledge about linkers, I can understand why "test2" gets the following results (y = 0x80000000 and x = 0x00000000).
Results:
link@ubuntu:~/Desktop$ gcc -Wall -o test2 foo.c bar3.c
/usr/bin/ld: Warning: alignment 4 of symbol `x' in /tmp/ccqoQr0c.o is smaller than 8 in /tmp/ccmrFBLe.o
link@ubuntu:~/Desktop$ gcc -Wall -Og -o test3 foo.c bar3.c
/usr/bin/ld: Warning: alignment 4 of symbol `x' in /tmp/ccu0POss.o is smaller than 8 in /tmp/ccL8gbUH.o
link@ubuntu:~/Desktop$ ./test2
x = 0x0 y = 0x3b6c
x address = 0x55616c976014 y address = 0x55616c976010
link@ubuntu:~/Desktop$ ./test3
x = 0x0 y = 0x80000000
x address = 0x55d6dd75a010 y address = 0x55d6dd75a014
link@ubuntu:~/Desktop$
But how does the optimization option in GCC 7.5.0 change the address order of the global variable x and y, could you please provide me some infomation concerning this part?
I've tested -O1,-O2,-O3 etc. The results are the same.
I will be grateful for any advice.
Upvotes: 1
Views: 42