<分区>
我猜想一个未定义的行为给我的项目带来了一些麻烦。为了更好地说明我的问题,我在下面举了一个简短的例子:
#include "stdlib.h"
#include "stdio.h"
#include "stdint.h"
typedef uint32_t ntype_t;
int main(){
int foo[2] = {0, 1};
ntype_t a,b;
int * pt = &foo[1];
a = 1;
b = 2;
printf("%d\n", pt[a-b]);
}
问题
我在没有任何选项的情况下编译了这个程序,得到了一个Segmentation fault (core dumped)
。当我用 -O2
编译这个程序时,结果是 0
。这个问题也和平台有关,我的环境是Ubuntu linux 3.11.0-15-generic + GCC 4.8.1。