次の 2 つのケースを考えてみましょう。
1.) 静的グローバル変数。マップ ファイルを生成すると、.bss または .data セクションに静的グローバル変数が見つかりません。
2.) 静的メンバー
#include <stdio.h>
#include <iostream>
#include <vector>
#include <list>
#include <algorithm>
using namespace std;
class Tree {
struct Node {
Node(int i, int d): id(i), dist(d) {}
int id;
int dist; // distance to the parent node
list<Node*> children;
};
class FindNode {
static Node* match;
int id;
public:
FindNode(int i): id(i) {}
Node* get_match()
{
return match;
}
bool operator()(Node* node)
{
if (node->id == id) {
match = node;
return true;
}
if (find_if(node->children.begin(), node->children.end(), FindNode(id)) != node->children.end()) {
return true;
}
return false;
}
};
Node* root;
void rebuild_hash();
void build_hash(Node* node, Node* parent = 0);
vector<int> plain;
vector<int> plain_pos;
vector<int> root_dist;
bool hash_valid; // indicates that three vectors above are valid
int ncount;
public:
Tree(): root(0), ncount(1) {}
void add(int from, int to, int d);
int get_dist(int n1, int n2);
};
Tree::Node* Tree::FindNode::match = 0;
...
変数 Tree::FindNode::match は FindNode クラスの静的メンバーです。そして、この変数は bss セクションのマップ ファイルに表示されます。なんでそうなの??
*(.bss)
.bss 0x00408000 0x80 C:\Users\Администратор\Desktop\яндекс\runs\runs\\000093.obj
0x00408000 _argc
0x00408004 _argv
0x00408020 Tree::FindNode::match
MinGW、OS Windows 7 を使用しています。 g++ ...cpp -o ...obj コマンドで取得したすべてのオブジェクト ファイル、ld ....obj -Map .....map コマンドで取得したマップ ファイル