1

在阅读CSAPP第7章的时候,遇到了一个到现在还解决不了的问题。

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;
}

我用下面显示的选项编译了这两部分。提供 IEEE754 表达式和有关链接器的一些知识,我可以理解为什么“test2”会得到以下结果(y = 0x80000000 和 x = 0x00000000)。

结果:

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$

但是 GCC 7.5.0 中的优化选项如何改变全局变量 x 和 y 的地址顺序,您能否提供一些有关这部分的信息?

我已经测试了-O1、-O2、-O3 等。结果是一样的。

我将不胜感激任何建议。

4

0 回答 0