如何看这个程序文件啊?不知道如何下手?请教大家

superwavelet 2011-03-18 10:31:49
这个IRGraph.cpp是一个程序中的一个文件。老师让我看懂然后移植这部分程序到我的程序里。但是我不太清楚该如何看,怎么下手啊?求助


#include "IRGraph.h"
#include "../../Analysis/Sort/sorting.h"
#include <fstream>
#include <sstream>
#include <stdio.h>
#include <string.h>

IRGraph::IRGraph()
{
this->filepath[0] = 0;
igraph_empty(&this->g, 0, 0);
this->nodeNames = NULL;
this->attrNames = NULL;
this->attrValues = NULL;
this->centrality = NULL;
this->clusterNum = 0;
this->membership = NULL;
this->clusterSize = NULL;
this->clusterSelectionStatusChanged = NULL;
this->nodeIsClusterCore = NULL;
this->distToCluster = NULL;
this->selected = NULL;
this->usedFocusKey = 0;
}

IRGraph::~IRGraph()
{
igraph_destroy(&g);
if (this->nodeNames != NULL)
delete [] this->nodeNames;
if (this->attrNames != NULL)
delete [] this->attrNames;
if (this->attrValues != NULL)
delete [] this->attrValues;
if (this->centrality != NULL)
delete [] this->centrality;
if (this->membership != NULL)
delete [] this->membership;
if (this->clusterSize != NULL)
delete [] this->clusterSize;
if (this->clusterSelectionStatusChanged != NULL)
delete [] this->clusterSelectionStatusChanged;
if (this->nodeIsClusterCore != NULL)
delete [] this->nodeIsClusterCore;
if (this->distToCluster != NULL)
delete [] this->distToCluster;
if (this->selected != NULL)
delete [] this->selected;

IPHash::iterator it;
it = nodeInFocusHash.begin();
while (it != nodeInFocusHash.end())
{
delete [] (bool *)(it->second);
it ++;
}
nodeInFocusHash.clear();
it = distToFocusHash.begin();
while (it != distToFocusHash.end())
{
delete [] (int *)(it->second);
it ++;
}
distToFocusHash.clear();
}

void IRGraph::LoadGraphEdgeList(char *filename)
{
strcpy(this->filepath, filename);

FILE *fp = fopen(filename, "r");
igraph_read_graph_edgelist(&g, fp, 0, 0);
fclose(fp);
this->nodeNum = igraph_vcount(&g);
this->edgeNum = igraph_ecount(&g);

// Assign some node information (label ...)
this->nodeNames = new char [this->nodeNum][128];
for (int i=0; i<this->nodeNum; i++)
sprintf(this->nodeNames[i], "Node%d", i);
}

void IRGraph::LoadGraphGML(char *filename)
{
strcpy(this->filepath, filename);

// Read graph information
FILE *fp = fopen(filename, "r");
igraph_read_graph_gml(&g, fp);
fclose(fp);
this->nodeNum = igraph_vcount(&g);
this->edgeNum = igraph_ecount(&g);

// Read node information (label attribute)
this->nodeNames = new char [this->nodeNum][128];
std::ifstream stream;
char line[1024];
int id = -1;
std::string s, name, value;
std::string::size_type index1, index2;
stream.open(filename);
while (!stream.eof())
{
stream.getline(line, 1024);
s = line;
index1 = s.find_first_not_of(' ');
index2 = s.find_last_not_of(' ');
if (index1 == std::string::npos || index2 == std::string::npos)
continue;
s = s.substr(index1, index2-index1+1);
index1 = s.find_first_of(' ');
if (index1 == std::string::npos)
continue;
name = s.substr(0, index1);
value = s.substr(index1+1);
if (value.at(0) == '\"' && value.at(value.length() - 1) == '\"')
value = value.substr(1, value.length() - 2);
if (name.compare("id") == 0)
id = atoi(value.c_str());
else if (name.compare("label") == 0)
{
if (id >=0 && id < this->nodeNum)
strcpy(this->nodeNames[id], value.c_str());
}
else if (name.compare("attribute") == 0)
{
if (id >=0 && id <= this->nodeNum)
{
int i = 0;
std::string::size_type p1, p2;
p1 = p2 = 0;
while (p2 != std::string::npos && i < this->attrNum)
{
p2 = value.find_first_of(' ', p1);
if (p2 != std::string::npos)
{
this->attrValues[id][i++] = atof(value.substr(p1, p2-p1).c_str());
p1 = p2 + 1;
}
else
this->attrValues[id][i++] = atof(value.substr(p1).c_str());
}
}
}
else if (name.compare("attribute_list") == 0)
{
std::string buf[128];
int n = 0;
std::string::size_type p1, p2;
p1 = p2 = 0;
while (p2 != std::string::npos)
{
p2 = value.find_first_of(' ', p1);
if (p2 != std::string::npos)
{
buf[n++] = value.substr(p1, p2-p1);
p1 = p2 + 1;
}
else
buf[n++] = value.substr(p1);
}
if (n > 0)
{
int i, j;
this->attrNum = n;
this->attrNames = new char [n][128];
for (i=0; i<n; i++)
strcpy(this->attrNames[i], buf[i].c_str());
this->attrValues = new double * [this->nodeNum];
for (i=0; i<this->nodeNum; i++)
{
this->attrValues[i] = new double [n];
for (j=0; j<n; j++)
this->attrValues[i][j] = 0.0;
}
}
}
}
stream.close();
}


...全文
152 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
superwavelet 2011-03-26
  • 打赏
  • 举报
回复
好的谢谢哦
superwavelet 2011-03-18
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 yaoyaoflyfreely 的回复:]

单步用Nisight
[/Quote]

您好
Nisight是什么?我用的是vs 2008
superwavelet 2011-03-18
  • 打赏
  • 举报
回复
这个是用一个igrpah的库把graph数据分类聚合的
yaoyaoflyfreely 2011-03-18
  • 打赏
  • 举报
回复
你这是 渲染的吗
yaoyaoflyfreely 2011-03-18
  • 打赏
  • 举报
回复
单步用Nisight
superwavelet 2011-03-18
  • 打赏
  • 举报
回复
我会单步运行,如何debug?可以告诉下吗?

ps:如何学习debug这些?需要看什么书?谢谢

非常感谢
c_losed 2011-03-18
  • 打赏
  • 举报
回复

作为C || C++ 新手
debug和单步是必备技能
当然 扣代码也是
先保证程序能编译通过 然后Debug调试 把你需要的部分扣出来 并优化~
superwavelet 2011-03-18
  • 打赏
  • 举报
回复

bool IRGraph::IsClusterCore(int id)
{
return this->nodeIsClusterCore[id];
}

void IRGraph::CalDistToCluster()
{
int i, j, member;

int n = this->nodeNum;
if (this->distToCluster == NULL)
{
this->distToCluster = new int * [n];
for (i=0; i<n; i++)
this->distToCluster[i] = new int [this->clusterNum];
}
for (i=0; i<n; i++)
{
for (j=0; j<this->clusterNum; j++)
this->distToCluster[i][j] = 2 * n;
}
igraph_vs_t from;
igraph_matrix_t pathLen;
igraph_matrix_init(&pathLen, 1, n);
int len;
for (i=0; i<n; i++)
{
if (this->nodeIsClusterCore[i])
{
member = this->membership[i];
igraph_vs_1(&from, i);
igraph_shortest_paths(&g, &pathLen, from, IGRAPH_ALL);
for (j=0; j<n; j++)
{
if (MATRIX(pathLen, 0, j) > 2.0 * n)
len = 2 * n;
else
len = (int)(MATRIX(pathLen, 0, j) + 0.01);
if (len < this->distToCluster[j][member])
this->distToCluster[j][member] = len;
}
}
}
igraph_matrix_destroy(&pathLen);
}

int IRGraph::GetDistToCluster(int id, int clusterIndex)
{
return this->distToCluster[id][clusterIndex];
}

void IRGraph::ClearSelection()
{
for (int i=0; i<this->nodeNum; i++)
SetNodeSelected(i, false);
}

bool* IRGraph::GetSelection()
{
return this->selected;
}

void IRGraph::SetNodeSelected(int id, bool s)
{
if (this->selected[id] != s)
{
int member = this->GetNodeMembership(id);
wxASSERT(member >=0 && member <this->clusterNum);
this->clusterSelectionStatusChanged[member] = true;
}
this->selected[id] = s;
}

bool IRGraph::IsNodeSelected(int id)
{
return this->selected[id];
}

bool IRGraph::IsClusterSelectionStatusChanged(int clusterID)
{
wxASSERT(clusterID >=0 && clusterID <this->clusterNum);
return this->clusterSelectionStatusChanged[clusterID];
}

void IRGraph::SetClusterSelectionStatusChanged(int clusterID, bool s)
{
wxASSERT(clusterID >=0 && clusterID <this->clusterNum);
this->clusterSelectionStatusChanged[clusterID] = s;
}

int IRGraph::CreateFocusFromSelection()
{
bool *nodeIsFocused = new bool [this->nodeNum];
int *distance = new int [this->nodeNum];
int i, j, len;
for (i=0; i<this->nodeNum; i++)
{
nodeIsFocused[i] = this->selected[i];
distance[i] = 2 * this->nodeNum;
}
igraph_vs_t from;
igraph_matrix_t pathLen;
igraph_matrix_init(&pathLen, 1, this->nodeNum);
for (i=0; i<this->nodeNum; i++)
{
if (nodeIsFocused[i])
{
igraph_vs_1(&from, i);
igraph_shortest_paths(&this->g, &pathLen, from, IGRAPH_ALL);
for (j=0; j<this->nodeNum; j++)
{
if (MATRIX(pathLen, 0, j) > 2.0 * this->nodeNum)
len = 2 * this->nodeNum;
else
len = (int)(MATRIX(pathLen, 0, j) + 0.01);
if (len < distance[j])
distance[j] = len;
}
}
}
igraph_matrix_destroy(&pathLen);

this->nodeInFocusHash[usedFocusKey] = nodeIsFocused;
this->distToFocusHash[usedFocusKey] = distance;
usedFocusKey ++;
return usedFocusKey - 1;
}

void IRGraph::RemoveFocus(int keyIndex)
{
bool *nodeInFocusArray = (bool *)this->nodeInFocusHash[keyIndex];
if (nodeInFocusArray != NULL)
delete [] nodeInFocusArray;
this->nodeInFocusHash.erase(keyIndex);
int *distToFocusArray = (int *)this->distToFocusHash[keyIndex];
if (distToFocusArray != NULL)
delete [] distToFocusArray;
this->distToFocusHash.erase(keyIndex);
}

int IRGraph::GetFocusSize(int keyIndex)
{
int size = 0;
bool *nodeInFocusArray = (bool *)this->nodeInFocusHash[keyIndex];
if (nodeInFocusArray != NULL)
{
for (int i=0; i<this->nodeNum; i++)
{
if (nodeInFocusArray[i])
size++;
}
}
return size;
}

bool IRGraph::IsNodeInFocus(int id, int keyIndex)
{
bool *nodeInFocusArray = (bool *)this->nodeInFocusHash[keyIndex];
return (nodeInFocusArray != NULL) ? nodeInFocusArray[id] : false;
}

int IRGraph::GetDistToFocus(int id, int keyIndex)
{
int *distToFocusArray = (int *)this->distToFocusHash[keyIndex];
return (distToFocusArray != NULL) ? distToFocusArray[id] : 999999999;
}

IIHash IRGraph::FindNodeByName(std::string label)
{
IIHash ids;
int i, j;

for (int i=0; i<(int)label.size();++i)
label[i] = tolower(label[i]);

int size = GetNodeNum();
for (i = 0; i < size; i++)
{
std::string name = GetNodeName(i);
for (j = 0; j < (int)name.size(); ++j)
name[j] = tolower(name[j]);

if (name.find(label) != std::string::npos)
ids[i] = i;
}
return ids;
}
superwavelet 2011-03-18
  • 打赏
  • 举报
回复
接上面的

void IRGraph::SetAttrValue(int id, int i, double value)
{
this->attrValues[id][i] = value;
}

void IRGraph::CalCentrality()
{
igraph_arpack_options_t options;
igraph_vector_t igraph_centrality;
igraph_arpack_options_init(&options);
igraph_vector_init(&igraph_centrality, 0);
igraph_eigenvector_centrality(&g, &igraph_centrality, NULL, /*scale=*/1, /*weights=*/NULL, &options);
if (this->centrality != NULL)
delete [] this->centrality;
this->centrality = new double [this->nodeNum];
for (int i=0; i<this->nodeNum; i++)
this->centrality[i] = VECTOR(igraph_centrality)[i];
}

double IRGraph::GetNodeCentrality(int id)
{
return centrality[id];
}

void IRGraph::CalCluster()
{
if (this->membership != NULL)
delete [] this->membership;
this->membership = new int [this->nodeNum];

igraph_vector_t tmp_membership;
igraph_integer_t no;
igraph_vector_init(&tmp_membership, 0);
igraph_clusters(&g, &tmp_membership, NULL, &no, IGRAPH_WEAK);
if (no > 1) // if there are more than one component, don't do clustering
{
int member, i;
this->clusterNum = 0;
for (i=0; i<this->nodeNum; i++)
{
member = (int)(VECTOR(tmp_membership)[i] + 0.01);
this->membership[i] = member;
if (member > this->clusterNum)
this->clusterNum = member;
}
this->clusterNum ++;
igraph_vector_destroy(&tmp_membership);
}
else //there is one component, then do clustering
{
bool use_igraph_algorithm = false;
if (use_igraph_algorithm) // use the clustering algorithm in igraph
{
igraph_arpack_options_t options;
igraph_arpack_options_init(&options);
igraph_community_leading_eigenvector(&g, NULL, &tmp_membership, igraph_vcount(&g), &options);

int member, i;
this->clusterNum = 0;
for (i=0; i<this->nodeNum; i++)
{
member = (int)(VECTOR(tmp_membership)[i] + 0.01);
this->membership[i] = member;
if (member > this->clusterNum)
this->clusterNum = member;
}
this->clusterNum ++;
igraph_vector_destroy(&tmp_membership);
}
else //use MCL for clustering
{
char result[1024];
strcpy(result, this->filepath);
strcat(result, ".clu");
FILE *fp = fopen(result, "r");
if (fp == NULL)
{
char mclFile[1024], cmdline[2048];
strcpy(mclFile, this->filepath);
strcat(mclFile, ".mcl");
this->SaveSelection(mclFile);
sprintf(cmdline, "mcltool\\mcl.exe %s -o %s --abc", mclFile, result);
system(cmdline);
fp = fopen(result, "r");
}

char *line = new char [65536];
this->clusterNum = 0;
char seps[4];
strcpy(seps, " \t");
char *token, *next_token;
while (fgets(line, 65536, fp) != NULL)
{
token = strtok_s(line, seps, &next_token);
while (token != NULL)
{
this->membership[atoi(token)] = this->clusterNum;
token = strtok_s(NULL, seps, &next_token);
}
this->clusterNum ++;
}
fclose(fp);
}
}
}

void IRGraph::SortCluster(int sortMode)
{
int i, j;

if (this->clusterSize != NULL)
delete [] this->clusterSize;
this->clusterSize = new int [this->clusterNum];

if (sortMode == 1) // sort by average centrality, need number to get average value
{
for (i=0; i<this->clusterNum; i++)
this->clusterSize[i] = 0;
for (i=0; i<this->nodeNum; i++)
this->clusterSize[this->membership[i]] ++;
}

SortStruct *sortArray = new SortStruct[this->clusterNum];
for (i=0; i<this->clusterNum; i++)
{
sortArray[i].number = i;
sortArray[i].value = 0;
}
for (i=0; i<this->nodeNum; i++)
{
if (sortMode == 0) // sort by number of nodes in the cluster
sortArray[this->membership[i]].value += 1.0;
else if (sortMode == 1) // sort by average centrality
sortArray[membership[i]].value += centrality[i];
}
if (sortMode == 1) // sort by average centrality, need number to get average value
{
for (i=0; i<this->clusterNum; i++)
sortArray[i].value /= (double)this->clusterSize[i];
}
qsort(sortArray, this->clusterNum, sizeof(SortStruct), DecreaseSort);
for (i=0; i<this->nodeNum; i++)
{
for (j=0; j<this->clusterNum; j++)
{
if (this->membership[i] == sortArray[j].number)
{
this->membership[i] = j;
break;
}
}
}
delete [] sortArray;

// Reset cluster size since clusters are reordered
for (i=0; i<this->clusterNum; i++)
this->clusterSize[i] = 0;
for (i=0; i<this->nodeNum; i++)
this->clusterSize[this->membership[i]] ++;

// Reset clusterSelectionStatusChanged array
if (this->clusterSelectionStatusChanged != NULL)
delete [] this->clusterSelectionStatusChanged;
this->clusterSelectionStatusChanged = new bool [this->clusterNum];
for (i = 0; i < this->clusterNum; i++)
this->clusterSelectionStatusChanged[i] = true;
}

int IRGraph::GetClusterNum()
{
return this->clusterNum;
}

int IRGraph::GetClusterSize(int index)
{
return this->clusterSize[index];
}

int IRGraph::GetNodeMembership(int id)
{
return this->membership[id];
}

void IRGraph::CalClusterCore(double threshold)
{
if (this->nodeIsClusterCore == NULL)
this->nodeIsClusterCore = new bool [this->nodeNum];

int i;
double *maxCentrality = new double [this->clusterNum];
int *maxCentIndex = new int [this->clusterNum];
for (i=0; i<this->clusterNum; i++)
maxCentrality[i] = -100.0;
int member;
double cent1;
for (i=0; i<this->nodeNum; i++)
{
cent1 = this->centrality[i];
this->nodeIsClusterCore[i] = (cent1 > threshold);
member = this->membership[i];
if (maxCentrality[member] < cent1)
{
maxCentrality[member] = cent1;
maxCentIndex[member] = i;
}
}

// Each group has at least one core node. Set max centrality node as a core node.
for (i=0; i<this->clusterNum; i++)
{
this->nodeIsClusterCore[maxCentIndex[i]] = true;
}

delete [] maxCentrality;
delete [] maxCentIndex;
}

superwavelet 2011-03-18
  • 打赏
  • 举报
回复
接上面的


void IRGraph::LoadFamousGraph(char *name)
{
igraph_famous(&g, name);
this->nodeNum = igraph_vcount(&g);
this->edgeNum = igraph_ecount(&g);
}

void IRGraph::LoadAttrFromOkc(char *filename, int &node_num)
{
int i, j;

FILE *fp = fopen(filename, "r");
fscanf(fp, "%d%d", &this->attrNum, &node_num);

// Assign image file name
this->nodeNames = new char [node_num][128];

this->attrNames = new char [this->attrNum][128];
for (i=0; i<this->attrNum; i++)
fscanf(fp, "%s", this->attrNames[i]);

this->attrValues = new double * [node_num];
for (i=0; i<node_num; i++)
{
fscanf(fp, "%s", this->nodeNames[i]);
this->attrValues[i] = new double [this->attrNum];
for (j=0; j<this->attrNum; j++)
fscanf(fp, "%lf", &this->attrValues[i][j]);
}
fclose(fp);
}

void IRGraph::LoadGraphFromMatrix(char *filename, int n, double threshold)
{
strcpy(this->filepath, filename);

char graphFile[512];
strcpy(graphFile, "data\\temp_edgelist.txt");
FILE *fp1, *fp2;

fp1 = fopen(graphFile, "w");
fp2 = fopen(filename, "r");
int i, j;
char *p1, *p2;
char *buffer = new char [32768];
for (i=0; i<n; i++)
{
fgets(buffer, 32768, fp2);
p1 = buffer;
for (j=0; j<n; j++)
{
p2 = strchr(p1, ' ');
if (i < j)
{
if (p2 != NULL)
*p2 = 0;
if (atof(p1) < threshold)
fprintf(fp1, "%d %d\n", i, j);
}
p1 = p2 + 1;
}
}
fclose(fp1);
fclose(fp2);
delete [] buffer;

fp1 = fopen(graphFile, "r");
igraph_empty(&g, n, 0);
igraph_read_graph_edgelist(&g, fp1, n, 0);
this->nodeNum = igraph_vcount(&g);
this->edgeNum = igraph_ecount(&g);
fclose(fp1);

remove(graphFile);
}

void IRGraph::SaveSelection(char *filename)
{
char *suffix = strrchr(filename, '.');
if (suffix == NULL)
return;
if (strcmp(suffix, ".gml") == 0)
{
int i, j;
FILE *fp = fopen(filename, "w");
fprintf(fp, "Creator \"wxVisTool.exe\"\n");
fprintf(fp, "graph\n");
fprintf(fp, "[\n");
fprintf(fp, " directed 0\n");
if (this->attrNum > 0 && this->attrNames != NULL)
{
fprintf(fp, " attribute_list \"");
for (i=0; i<this->attrNum; i++)
{
if (i > 0)
fprintf(fp, " ");
fprintf(fp, "%s", this->attrNames[i]);
}
fprintf(fp, "\"\n");
}
int *newId = new int [this->nodeNum];
int id = 0;
for (i=0; i<this->nodeNum; i++)
{
if (this->selected[i])
{
newId[i] = id;
fprintf(fp, " node\n");
fprintf(fp, " [\n");
fprintf(fp, " id %d\n", id);
fprintf(fp, " label \"%s\"\n", this->nodeNames[i]);
fprintf(fp, " attribute \"");
for (j=0; j<this->attrNum; j++)
{
if (j > 0)
fprintf(fp, " ");
fprintf(fp, "%lf", this->attrValues[i][j]);
}
fprintf(fp, "\"\n");
fprintf(fp, " ]\n");
id++;
}
}
int *edges = new int [this->edgeNum * 2];
this->GetAllGraphEdges(edges);
for (i=0; i<this->edgeNum; i++)
{
if (this->selected[edges[i*2]] && this->selected[edges[i*2+1]])
{
fprintf(fp, " edge\n");
fprintf(fp, " [\n");
fprintf(fp, " source %d\n", newId[edges[i*2]]);
fprintf(fp, " target %d\n", newId[edges[i*2+1]]);
fprintf(fp, " ]\n");
}
}
fprintf(fp, "]\n");
fclose(fp);
delete [] newId;
delete [] edges;
}
else if (strcmp(suffix, ".mcl") == 0)
{
int *edges = new int [this->edgeNum * 2];
this->GetAllGraphEdges(edges);
FILE *fp = fopen(filename, "w");
for (int i=0; i<this->edgeNum; i++)
{
fprintf(fp, "%d %d 1.0\n", edges[i*2], edges[i*2+1]);
}
fclose(fp);
delete [] edges;
}
}

bool IRGraph::CreateIOV(std::string fileName)
{
if (this->attrNum <= 0 || this->attrNames == NULL)
return false;

int i, j, k;

//create okc file
std::string okcFile = fileName + ".okc";
std::ofstream outposfile(okcFile.c_str());

int dimNum = this->attrNum;
int itemNum = this->nodeNum;
outposfile << dimNum << " " << itemNum << std::endl;

for (i = 0; i < dimNum; i++)
outposfile << this->attrNames[i] << std::endl;

for (i = 0; i < itemNum; i++)
{
outposfile << this->nodeNames[i] << " ";
for (j = 0; j < dimNum; j++)
outposfile << this->attrValues[i][j] << " ";
outposfile << std::endl;
}
outposfile.close();

// create iov file
outposfile.open(fileName.c_str());
double **dis_matrix = new double *[itemNum];
for (i = 0; i < itemNum; i++)
dis_matrix[i] = new double[itemNum];

double sum1, sum2;
for (i = 0; i < itemNum; i++)
{
for (j = 0; j < itemNum; j++)
{
sum1 = 0;
sum2 = 0;
for (k = 0; k < dimNum; k++)
{
if (fabs(this->attrValues[i][k]-this->attrValues[j][k]) > 0.5)
sum1 += 1;
if ((this->attrValues[i][k] + this->attrValues[j][k]) > 0.5)
sum2 += 1;
}
if (sum2 < 0.5)
dis_matrix[i][j] = dis_matrix[j][i] = 1;
else
dis_matrix[i][j] = dis_matrix[j][i] = sum1/sum2;
}
}

for (i = 0; i < itemNum; i++)
{
for (j = 0; j < itemNum; j++)
outposfile << dis_matrix[i][j] << " ";
outposfile << std::endl;
}

outposfile.close();

for (i = 0; i < itemNum; i++)
{
delete [] dis_matrix[i];
}
delete [] dis_matrix;
}

void IRGraph::CalAll()
{
int i, j;
char result[1024];
strcpy(result, this->filepath);
strcat(result, ".res");
FILE *fp = fopen(result, "r");
if (fp == NULL)
{
CalCentrality();
CalCluster();
SortCluster(0);
CalClusterCore(-1.0);
CalDistToCluster();

fp = fopen(result, "w");
for (i=0; i<this->nodeNum; i++)
fprintf(fp, "%lf\n", this->centrality[i]);

fprintf(fp, "%d\n", this->clusterNum);
for (i=0; i<this->nodeNum; i++)
fprintf(fp, "%d\n", this->membership[i]);
for (i=0; i<this->clusterNum; i++)
fprintf(fp, "%d\n", this->clusterSize[i]);

for (i=0; i<this->nodeNum; i++)
fprintf(fp, "%d\n", this->nodeIsClusterCore[i]);

for (i=0; i<this->nodeNum; i++)
{
for (j=0; j<this->clusterNum; j++)
fprintf(fp, "%d\n", this->distToCluster[i][j]);
}
}
else
{
if (this->centrality != NULL)
delete [] this->centrality;
this->centrality = new double [this->nodeNum];
for (i=0; i<this->nodeNum; i++)
fscanf(fp, "%lf", &this->centrality[i]);

if (this->membership != NULL)
delete [] this->membership;
if (this->clusterSize != NULL)
delete [] this->clusterSize;
if (this->clusterSelectionStatusChanged != NULL)
delete [] this->clusterSelectionStatusChanged;
fscanf(fp, "%d", &this->clusterNum);
this->membership = new int [this->nodeNum];
for (i=0; i<this->nodeNum; i++)
fscanf(fp, "%d", &this->membership[i]);
this->clusterSize = new int [this->clusterNum];
for (i=0; i<this->clusterNum; i++)
fscanf(fp, "%d", &this->clusterSize[i]);
this->clusterSelectionStatusChanged = new bool [this->clusterNum];
for (i=0; i<this->clusterNum; i++)
this->clusterSelectionStatusChanged[i] = true;

if (this->nodeIsClusterCore == NULL)
this->nodeIsClusterCore = new bool [this->nodeNum];
for (i=0; i<this->nodeNum; i++)
fscanf(fp, "%d", &this->nodeIsClusterCore[i]);

if (this->distToCluster == NULL)
{
this->distToCluster = new int * [this->nodeNum];
for (i=0; i<this->nodeNum; i++)
this->distToCluster[i] = new int [this->clusterNum];
}
for (i=0; i<this->nodeNum; i++)
{
for (j=0; j<this->clusterNum; j++)
fscanf(fp, "%d", &this->distToCluster[i][j]);
}
}
fclose(fp);

this->selected = new bool [this->nodeNum];
for (int i=0; i<this->nodeNum; i++)
this->selected[i] = false;
}

char *IRGraph::GetFilePath()
{
return this->filepath;
}

igraph_t *IRGraph::GetIGraph()
{
return &g;
}

int IRGraph::GetGraphVCount()
{
return this->nodeNum;
}

int IRGraph::GetGraphECount()
{
return this->edgeNum;
}

void IRGraph::GetAllGraphEdges(int *edges)
{
int i;
igraph_es_t es;
igraph_eit_t eit;
igraph_integer_t eid, from, to;

igraph_es_all(&es, IGRAPH_EDGEORDER_FROM);
igraph_eit_create(&g, es, &eit);
i = 0;
while (!IGRAPH_EIT_END(eit)) {
eid = IGRAPH_EIT_GET(eit);
igraph_edge(&g, eid, &from, &to);
edges[i++] = from;
edges[i++] = to;
IGRAPH_EIT_NEXT(eit);
}
igraph_eit_destroy(&eit);
igraph_es_destroy(&es);
}

int IRGraph::GetNodeNum()
{
return this->nodeNum;
}

int IRGraph::GetEdgeNum()
{
return this->edgeNum;
}

char *IRGraph::GetNodeName(int id)
{
return this->nodeNames[id];
}

int IRGraph::GetAttrNum()
{
return this->attrNum;
}

char *IRGraph::GetAttrName(int i)
{
return this->attrNames[i];
}

double IRGraph::GetAttrValue(int id, int i)
{
return this->attrValues[id][i];
}


出现这个现象有方面的,一是硬件,即内存方面有问题,二是软件,这就有多方面的问题了。 一:先说说硬件: 一般来说,电脑硬件是很不容易坏的。内存出现问题的可能性并不大(除非你的内存真的是杂牌的一塌徒地),主要方面是:1。内存条坏了(二手内存情况居多)、2。使用了有质量问题的内存,3。内存插在主板上的金手指部分灰尘太多。4。使用不同品牌不同容量的内存,从而出现不兼容的情况。5。超频带来的散热问题。你可以使用MemTest 这个软件来检测一下内存,它可以彻底的检测出内存的稳定度。 二、如果都没有,那就从软件方面排除故障了。 先说原理:内存有个存放数据的地方叫缓冲区,当程序把数据放在缓冲区,需要操作系统提供的“功能函数”来申请,如果内存分配成功,函数就会将所新开辟的内存区地址返回给应用程序,应用程序就可以通过这个地址使用这块内存。这就是“动态内存分配”,内存地址也就是编程中的“光标”。内存不是永远都招之即来、用之不尽的,有时候内存分配也会失败。当分配失败时系统函数会返回一个0值,这时返回值“0”已不表示新启用的光标,而是系统向应用程序发出的一个通知,告知出现了错误。作为应用程序,在每一次申请内存后都应该检查返回值是否为0,如果是,则意味着出现了故障,应该采取一些措施挽救,这就增强了程序的“健壮性”。若应用程序没有检查这个错误,它就会按照“思维惯性”认为这个值是给它分配的可用光标,继续在之后的执行中使用这块内存。真正的0地址内存区储存的是计算机系统中最重要的“中断描述符表”,绝对不允许应用程序使用。在没有保护机制的操作系统下(如DOS),写数据到这个地址会导致立即当机,而在健壮的操作系统中,如Windows等,这个操作会马上被系统的保护机制捕获,其结果就是由操作系统强行关闭出错的应用程序,以防止其错误扩大。这时候,就会出现上述的内存不能为“read”错误,并指出被引用的内存地址为“0x00000000“。内存分配失败故障的原因很多,内存不够、系统函数的版本不匹配等都可能有影响。因此,这种分配失败多见于操作系统使用很长时间后,安装了多种应用程序(包括无意中“安装”的病毒程序),更改了大量的系统参数和系统档案之后。 在使用动态分配的应用程序中,有时会有这样的情况出现:程序试图读写一块“应该可用”的内存,但不知为什么,这个预料中可用的光标已经失效了。有可能是 “忘记了”向操作系统要求分配,也可能是程序自己在某个时候已经注销了这块内存而“没有留意”等等。注销了的内存被系统回收,其访问权已经不属于该应用程序,因此读写操作也同样会触发系统的保护机制,企图“违法”的程序唯一的下场就是被操作终止执行,回收全部资源。计算机世界的法律还是要比人类有效和严厉得多啊!像这样的情况都属于程序自身的BUG,你往往可在特定的操作顺序下重现错误。无效光标不一定总是0,因此错误提示中的内存地址也不一定为 “0x00000000”,而是其它随机数字。 首先建议: 1、 检查系统中是否有木马或病毒。这类程序为了控制系统往往不负责任地修改系统,从而导致操作系统异常。平常应加强信息安全意识,对来源不明的可执行程序绝不好奇。 2、 更新操作系统,让操作系统的安装程序重新拷贝正确版本的系统档案、修正系统参数。有时候操作系统本身也会有BUG,要注意安装官方发行的升级程序。 3、 尽量使用最新正式版本的应用程序、Beta版、试用版都会有BUG。 4、 删除然后重新创建 WinntSystem32WbemRepository 文件夹中的文件:在桌面上右击我的电脑,然后单击管理。在"服务和应用程序"下,单击服务,然后关闭并停止 Windows Management Instrumentation 服务。 删除 WinntSystem32WbemRepository 文件夹中的所有文件。(在删除前请创建这些文件的备份副本。)打开"服务和应用程序",单击服务,然后打开并启动 Windows Management Instrumentation 服务。当服务重新启动时,将基于以下注册表项中所提供的信息重新创建这些文件: HKEY_LOCAL_MACHINESOFTWAREMicrosoftWBEMCIMOMAutorecover MOFs 下面搜集几个例子给大家分析: 例一:IE浏览器出现“0x0a8ba9ef”指令引用的“0x03713644” 内存,或者“0x70dcf39f”指令引用的“0x00000000”内存。该内存不能为“read”。要终止程序,请单击“确定”的信息框,单击“确定”后,又出现“发生内部错误,您正在使用的其中一个窗口即将关闭”的信息框,关闭该提示信息后,IE浏览器也被关闭。解决方法: 1、 开始-运行窗口,输入“regsvr32 actxprxy.dll”回车,接着会出现一个信息对话 框“DllRegisterServer in actxprxy.dll succeeded”,确定。再依次运行以下命令。(这个方法有人说没必要,但重新注册一下那些.dll对系统也没有坏处,反正多方下手,能解决问题就行。) regsvr32 shdocvw.dll regsvr32 oleaut32.dll regsvr32 actxprxy.dll regsvr32 mshtml.dll regsvr32 msjava.dll regsvr32 browseui.dll regsvr32 urlmon.dll 2、 修复或升级IE浏览器,同时打上系统补丁。看过其中一个修复方法是,把系统还原到系统初始的状态下。建议将IE升级到了6.0。 例二:有些应用程序错误: “0x7cd64998” 指令参考的 “0x14c96730” 内存。该内存不能为 “read”。解决方法:Win XP的“预读取”技术这种最佳化技术也被用到了应用程序上,系统对每一个应用程序的前几次启动情况进行分析,然后新增一个描述套用需求的虚拟“内存映像”,并把这些信息储存到WindowsPrefetch文件夹。一旦建立了映像,应用软件的装入速度大大提高。XP的预读取数据储存了最近8次系统启动或应用软件启动的信息。建议将虚拟内存撤换,删除WindowsPrefetch目录下所有*.PF文件,让windows重新收集程序的物理地址。 例三:在XP下双击光盘里面的“AutoRun.exe”文件,显示“0x77f745cc”指令引用的“0x00000078”内存。该内存不能为“written”,要终止程序,请单击“确定”,而在Windows 98里运行却正常。解决方法:这可能是系统的兼容性问题,winXP的系统,右键“AutoRun.exe”文件,属性,兼容性,把“用兼容模式运行这个程序”项选择上,并选择“Windows 98/Me”。win2000如果打了SP的补丁后,只要开始,运行,输入:regsvr32 c:winntapppatchslayerui.dll。右键,属性,也会出现兼容性的选项。 例四:RealOne Gold关闭时出现错误,以前一直使用正常,最近却在每次关闭时出现“0xffffffff”指令引用的“0xffffffff”内存。该内存不能为 “read” 的提示。解决方法:当使用的输入法为微软拼音输入法2003,并且隐藏语言栏时(不隐藏时没问题)关闭RealOne就会出现这个问题,因此在关闭RealOne 之前可以显示语言栏或者将任意其他输入法作为当前输入法来解决这个问题。 例五:我的豪杰超级解霸自从上网后就不能播放了,每次都提示“0x060692f6”(每次变化)指令引用的“0xff000011”内存不能为 “read”,终止程序请按确定。解决方法:试试重装豪杰超级解霸,如果重装后还会,到官方网站下载相应版本的补丁试试。还不行,只好换就用别的播放器试试了。 例六:双击一个游戏的快捷方式,“0x77f5cd0”指令引用“0xffffffff”内 存,该内存不能为“read” ,并且提示Client.dat程序错误。解决方法:重装显卡的最新驱动程序,然后下载并且安装DirectX9.0。 例七:一个朋友发信息过来,我的电脑便出现了错误信息:“0x772b548f”指令引用的“0x00303033”内存,该内存不能为 “written”,然后QQ自动下线,而再打开QQ,发现了他发过来的十几条的信息。解决方法:这是对方利用QQ的BUG,发送特殊的代码,做QQ出错,只要打上补丁或升级到最新版本,就没事了。 该内存不能为read或written的解决方案关键词: 该内存不能为"read" 该内存不能为"written" 从网上搜索来的几篇相关文章. 【文章一】 使用Windows操作系统的人有时会遇到这样的错误信息: 「“0X????????”指令引用的“0x00000000”内存,该内存不能为“read”或“written”」,然后应用程序被关闭。 如果去请教一些「高手」,得到的回答往往是「Windows就是这样不稳定」之类的义愤和不屑。其实,这个错误并不一定是Windows不稳定造成的。本文就来简单分析这种错误的一般原因。 一、应用程序没有检查内存分配失败 程序需要一块内存用以储存数据时,就需要使用操作系统提供的「功能函数」来申请,如果内存分配成功,函数就会将所新开辟的内存区地址返回给应用程序,应用程序就可以通过这个地址使用这块内存。这就是「动态内存分配」,内存地址也就是编程中的「光标」。内存不是永远都招之即来、用之不尽的,有时候内存分配也会失败。当分配失败时系统函数会返回一个0值,这时返回值「0」已不表示新启用的游标,而是系统向应用程序发出的一个通知,告知出现了错误。作为应用程序,在每一次申请内存后都应该检查返回值是否为0,如果是,则意味着出现了故障,应该采取一些措施挽救,这就增强了程序的「健壮性」。若应用程序没有检查这个错误,它就会按照「思维惯性」认为这个值是给它分配的可用游标,继续在之后的执行中使用这块内存。真正的0地址内存区储存的是计算机系统中最重要的「中断描述符表」,绝对不允许应用程序使用。在没有保护机制的操作系统下(如DOS),写数据到这个地址会导致立即当机,而在健壮的操作系统中,如 Windows等,这个操作会马上被系统的保护机制捕获,其结果就是由操作系统强行关闭出错的应用程序,以防止其错误扩大。这时候,就会出现上述的「写内存」错误,并指出被引用的内存地址为「0x00000000」。内存分配失败故障的原因很多,内存不够、系统函数的版本不匹配等都可能有影响。因此,这种分配失败多见于操作系统使用很长时间后,安装了多种应用程序(包括无意中「安装」的病毒程序),更改了大量的系统参数和系统档案之后。 二、应用程序由于自身BUG引用了不正常的内存光标 在使用动态分配的应用程序中,有时会有这样的情况出现:程序试突读写一块「应该可用」的内存,但不知为什么,这个预料中可用的光标已经失效了。有可能是「忘记了」向操作系统要求分配,也可能是程序自己在某个时候已经注销了这块内存而「没有留意」等等。注销了的内存被系统回收,其访问权已经不属于该应用程序,因此读写操作也同样会触发系统的保护机制,企图「违法」的程序唯一的下场就是被操作终止执行,回收全部资源。计算机世界的法律还是要比人类有效和严厉得多啊!像这样的情况都属于程序自身的BUG,你往往可在特定的操作顺序下重现错误。无效光标不一定总是0,因此错误提示中的内存地址也不一定为「0x00000000」,而是其它随机数字。如果系统经常有所提到的错误提示,下面的建议可能会有说明 : 1.检视系统中是否有木马或病毒。这类程序为了控制系统往往不负责任地修改系统, 从而导致操作系统异常。平常应加强信息安全意识,对来源不明的可执行程序绝不好奇。 2.更新操作系统,让操作系统的安装程序重新拷贝正确版本的系统档案、修正系统参数。 有时候操作系统本身也会有BUG,要注意安装官方发行的升级程序。 3.试用新版本的应用程序。 Mode: 将虚拟内存撤换 答案: 目前为止是肯定的,也就是如在下次冷天到来时亦没再发生,就代表这是主因 追加: 如果你用 Ghost 恢复 OS 后建议 删除WINDOWS\PREFETCH目录下所有*.PF文件因为需让windows重新收集程序的物理地址 有些应用程序错误 "0x7cd64998" 指令参考的 "0x14c96730" 内存。该内存不能为 "read"推论是此原因 源由: Win XP的「预读取」技术 这种最佳化技术也被用到了应用软件上,系统对每一个应用软件的前几次启动情况进行分析,然后新增一个描述套用需求的虚拟「内存映像」,并把这些信息储存到 WINDOWSPREFETCH数据夹。一旦建立了映像,应用软件的装入速度大大提高。XP的预读取数据储存了最近8次系统启动或应用软件启动的信息。 后叙: 目前此方法亦是独步网络的(其码自己针对此问题查了许久),也是常见问题,原本几乎每天睡前关闭软件时一些程序都会发生...read... 现在就没发生了。 【文章二】 运行某些程序的时候,有时会出现内存错误的提示(0x后面内容有可能不一样),然后该程序就关闭。 “0x????????”指令引用的“0x????????”内存。该内存不能为“read”。 “0x????????”指令引用的“0x????????”内存,该内存不能为“written”。 不知你出现过类似这样的故障吗? 一般出现这个现象有方面的,一是硬件,即内存方面有问题,二是软件,这就有多方面的问题了。 下面先说说硬件: 一般来说,内存出现问题的可能性并不大,主要方面是:内存条坏了、内存质量有问题,还有就是2个不同牌子不同容量的内存混插,也比较容易出现不兼容的情况,同时还要注意散热问题,特别是超频后。你可以使用MemTest 这个软件来检测一下内存,它可以彻底的检测出内存的稳定度。 假如你是双内存,而且是不同品牌的内存条混插或者买了二手内存时,出现这个问题,这时,你就要检查是不是内存出问题了或者和其它硬件不兼容。 如果都没有,那就从软件方面排除故障了。 先简单说说原理:内存有个存放数据的地方叫缓冲区,当程序把数据放在其一位置时,因为没有足够空间,就会发生溢出现象。举个例子:一个桶子只能将一斤的水,当你放入两斤的水进入时,就会溢出来。而系统则是在屏幕上表现出来。这个问题,经常出现在windows2000和XP系统上,Windows 2000/XP对硬件的要求是很苛刻的,一旦遇到资源死锁、溢出或者类似Windows 98里的非法操作,系统为保持稳定,就会出现上述情况。另外也可能是硬件设备之间的兼容性不好造成的。 下面我从几个例子给大家分析: 例一:打开IE浏览器或者没过几分钟就会出现"0x70dcf39f"指令引用的"0x00000000"内存。该内存不能为“read”。要终止程序,请单击“确定”的信息框,单击“确定”后,又出现“发生内部错误,您正在使用的其中一个窗口即将关闭”的信息框,关闭该提示信息后,IE浏览器也被关闭。解决方法:修复或升级IE浏览器,同时打上补丁。看过其中一个修复方法是,Win2000自升级,也就是Win2000升级到Win2000,其实这种方法也就是把系统还原到系统初始的状态下。比如你的IE升级到了6.0,自升级后,会被IE5.0代替。 例二:在windows xp下双击光盘里面的“AutoRun.exe”文件,显示“0x77f745cc”指令引用的“0x00000078”内存。该内存不能为 “written”,要终止程序,请单击“确定”,而在Windows 98里运行却正常。解决方法:这可能是系统的兼容性问题,winXP的系统,右键“AutoRun.exe”文件,属性,兼容性,把“用兼容模式运行这个程序”项选择上,并选择“Windows 98/Me”。win2000如果打了SP的补丁后,只要开始,运行,输入:regsvr32 c:winntapppatchslayerui.dll。右键,属性,也会出现兼容性的选项。 例三:RealOne Gold关闭时出现错误,以前一直使用正常,最近却在每次关闭时出现“0xffffffff”指令引用的“0xffffffff”内存。该内存不能为 “read” 的提示。解决方法:当使用的输入法为微软拼音输入法2003,并且隐藏语言栏时(不隐藏时没问题)关闭RealOne就会出现这个问题,因此在关闭RealOne 之前可以显示语言栏或者将任意其他输入法作为当前输入法来解决这个问题。 例四:我的豪杰超级解霸自从上网后就不能播放了,每次都提示“0x060692f6”(每次变化)指令引用的“0xff000011”内存不能为 “read”,终止程序请按确定。解决方法:试试重装豪杰超级解霸,如果重装后还会,到官方网站下载相应版本的补丁试试。还不行,只好换就用别的播放器试试了。 例五:双击一个游戏的快捷方式,“0x77f5cd0”指令引用“0xffffffff”内 存,该内存不能为“read” ,并且提示Client.dat程序错误。 解决方法:重装显卡的最新驱动程序,然后下载并且安装DirectX9.0。 例六:一个朋友发信息过来,我的电脑便出现了错误信息:“0x772b548f”指令引用的“0x00303033”内存,该内存不能为 “written”,然后QQ自动下线,而再打开QQ,发现了他发过来的十几条的信息。解决方法:这是对方利用QQ的BUG,发送特殊的代码,做QQ出错,只要打上补丁或升级到最新版本,就没事了。 【原因 解决方法】 1 内存条坏了 更换内存条 2 双内存不兼容 使用同品牌的内存或只要一条内存 3 内存质量问题 更换内存条 4 散热问题 加强机箱内部的散热 5 内存和主板没插好或其他硬件不兼容 重插内存或换个插槽 6 硬件有问题 更换硬盘 7 驱动问题 重装驱动,如果是新系统,应先安装主板驱动 8 软件损坏 重装软件 9 软件有BUG 打补丁或更新到最新版本 10 软件和系统不兼容 给软件打上补丁或是试试系统的兼容模式 11 软件和软件之间有冲突 如果最近安装了什么新软件,卸载了试试 12 软件要使用其他相关的软件有问题 重装相关软件,比如播放某一格式的文件时出错,可能是这个文件的解码器有问题 13 病毒问题 杀毒 14 杀毒软件与系统或软件相冲突 由于杀毒软件是进入底层监控系统的,可能与一些软件相冲突,卸载试试 15 系统本身有问题 有时候操作系统本身也会有BUG,要注意安装官方发行的更新程序,象SP的补丁,最好打上.如果还不行,重装系统,或更换其他版本的系统。 〔又一说〕 在控制面板的添加/删除程序中看看你是否安装了微软NET.Framework,如果已经安装了,可以考虑卸载它,当然如果你以后在其它程序需要NET.Framework时候,可以再重新安装。 另外,如果你用的是ATI显卡并且你用的是SP2的补丁(一些ATI的显卡驱动需要在NET.Framework正常工作的环境下)。这种情况你可以找一款不需要NET.Framework支持的ATI显卡驱动。 如果以上两种方法并不能完全解决问题,你试着用一下“IE修复”软件,并可以查查是否有病毒之类的。 〔微软NET.Framework升级到1.1版应该没问题了〕 〔还有一说〕 方法一: 微软新闻组的朋友指点:开始--运行:regsvr32 jscript.dll 开始--运行:regsvr32 vbscript.dll 不过没解决---但提供了路子-----一次运行注册所有dll 搜索查找到方法如下: 运行 输入cmd 回车在命令提示符下输入 for %1 in (%windir%system32*.dll) do regsvr32.exe /s %1 这个命令老兄你慢慢输 输入正确的话会看到飞快地滚屏 否则……否则失败就是没这效果。回车后慢慢等(需要点时间1-2分钟) 都运行完再打开看 方法二: 这是个典型问题~~~~~引起这个问题的原因很多。一般来讲就是给系统打上补丁和更换内存、给内存换个插槽这3种方法来解决。[系统补丁只要到Microsoft Update网站在线更新就可以了] 造成这种问题的原因很多,不能单纯的下结论,尽量做到以下几点可能对你有帮助: 1。确保使用的是未修改过的软件(非汉化、破解版) 2。使用改软件时尽量不要运行其他软件。(这是个临时文件,可能某些软件也在使用临时文件夹,所以产生干扰) 3。把那些什么桌面工具,内存整理工具通通关掉(你至少有2个类似的工具在运行)” 处理方法: 运行regedit进入注册表, 在HKEY_LOCAL_MACHINESOFTWAREMicrosoftWindowsCurrentVersionExplorerShellExecuteHooks 下,应该只有一个正常的键值"{AEB6717E-7E19-11d0-97EE-00C04FD91972}, 将其他的删除。 〔我个人的最后解决和看法〕 我今天尝试了多种办法,最后我发现问题出在微软的NET.Framework上面。我升级了这个软件,并打齐了补丁,短暂平安后,有出现“内存不能为read”的情况。后来我受上面文章的启发,卸载了微软的NET.Framework1.0和1.1,世界太平了。 另外:如果是打开“我的电脑”、“我的文档”等的时候出现上述情况,还有一种可能,就是你的右键菜单太臃肿了,此时只要清理右键菜单问题就解决了。 -------------------------------------------------------------------------------- 〔试验的结果〕 上面的方法,最管用、最彻底的方法是这个: 运行 输入cmd 回车在命令提示符下输入 for %1 in (%windir%system32*.dll) do regsvr32.exe /s %1 【技巧】如果怕输入错误的话,可以复制这条指令,然后在命令提示框点击左上角的c:,使用下面的“编辑-粘贴”功能就不容易输错了。在飞速滚屏完全静止之后,别着急启动其他程序,先耐心等一会儿,因为此时dll们还在找位置。直到你的指示灯不闪了再做别的。 其他建议 使用Windows操作系统的人有时会遇到这样的错误信息:“0X????????指令引用的0x00000000内存,该内存不能written”,然后应用程序被关闭。如果去请教一些“高手”,得到的回答往往是“Windows就是这样不稳定”之类的义愤和不屑。其实,这个错误并不一定是Windows不稳定造成的。本文就来简单分析这种错误的常见原因。 一、应用程序没有检查内存分配失败 程序需要一块内存用以保存数据时,就需要调用操作系统提供的“功能函数”来申请,如果内存分配成功,函数就会将所新开辟的内存区地址返回给应用程序,应用程序就可以通过这个地址使用这块内存。这就是“动态内存分配”,内存地址也就是编程中的“指针”。 内存不是永远都招之即来、用之不尽的,有时候内存分配也会失败。当分配失败时系统函数会返回一个0值,这时返回值“0”已不表示新启用的指针,而是系统向应用程序发出的一个通知,告知出现了错误。作为应用程序,在每一次申请内存后都应该检查返回值是否为0,如果是,则意味着出现了故障,应该采取一些措施挽救,这就增强了程序的“健壮性”。 若应用程序没有检查这个错误,它就会按照“思维惯性”认为这个值是给它分配的可用指针,继续在之后的运行中使用这块内存。真正的0地址内存区保存的是计算机系统中最重要的“中断描述符表”,绝对不允许应用程序使用。在没有保护机制的操作系统下(如DOS),写数据到这个地址会导致立即死机,而在健壮的操作系统中,如Windows等,这个操作会马上被系统的保护机制捕获,其结果就是由操作系统强行关闭出错的应用程序,以防止其错误扩大。这时候,就会出现上述的“写内存”错误,并指出被引用的内存地址为“0x00000000”。 内存分配失败故障的原因很多,内存不够、系统函数的版本不匹配等都可能有影响。因此,这种分配失败多见于操作系统使用很长时间后,安装了多种应用程序(包括无意中“安装”的病毒程序),更改了大量的系统参数和系统文件之后。 二、应用程序由于自身BUG引用了不正常的内存指针 在使用动态分配的应用程序中,有时会有这样的情况出现:程序试图读写一块“应该可用”的内存,但不知为什么,这个预料中可用的指针已经失效了。有可能是“忘记了”向操作系统要求分配,也可能是程序自己

65,206

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧