全局变量堆内存申请顺序
打开_MAP______OK宏编译运行:
tmp-> g++ 1.cpp -o main && ./main
111111111111111111111111
222222222222222222222222
333333333333333333333333
111111111111111111111111
Segmentation fault
tmp->
tmp-> cat 1.cpp
/// @file 1.cpp
/// @brief
/// @author EastonWoo
/// 0.01
/// @date 2015-12-05
#include <stdio.h>
#include <map>
#define _MAP______OK // 屏蔽出现段错误
bool test();
#ifdef _MAP______OK
// bss区, _map比 ok全局变量 申请堆内存空间要早。
std::map<int, int> _map;
#endif
bool ok = test(); // bss区, 申请ok 的堆内存时, ok 有 '=' 号,需要初始化, 初始调用了code区的test段
#ifndef _MAP______OK
// bss区, _map比 ok全局变量 申请堆内存空间要迟。 所在会出现段错误
std::map<int, int> _map;
#endif
bool test() {
printf("111111111111111111111111\n");
_map[1] = 1;
printf("222222222222222222222222\n");
return true;
}
int main (int argc, const char * argv[]) {
printf("333333333333333333333333\n");
return 0;
}
打开_MAP______OK宏编译运行:
tmp-> g++ 1.cpp -o main && ./main
111111111111111111111111
222222222222222222222222
333333333333333333333333
屏蔽_MAP______OK宏编译运行:
tmp-> g++ 1.cpp -o main && ./main
111111111111111111111111
Segmentation fault
tmp->