Lets consider two cases:
1.) Static global variables.
When i generate map file i can’t find static global variables in .bss or .data section.
2.) Static members
#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;
...
Variable Tree::FindNode::match is static member of FindNode class. And this variable is presented in map file in bss section. Why so??
*(.bss)
.bss 0x00408000 0x80 C:\Users\Администратор\Desktop\яндекс\runs\runs\\000093.obj
0x00408000 _argc
0x00408004 _argv
0x00408020 Tree::FindNode::match
I use MinGW, os windows 7. All object files obtained by g++ …cpp -o …obj command, map file obtained by ld ….obj -Map …..map command
Global variables are already in the static memory, so C re-used the existing keyword
staticto make a global variable “file-scoped”, and C++ followed the suite. The keywordstatichides your global from the map file.Static members, on the other hand, are class-scoped, therefore they need to be available in the map file: other modules need to be able to access the static members of your class, both member functions and member variables, even if they are separately compiled.