【发布时间】:2010-07-22 20:36:05
【问题描述】:
我可以假设如果...
int main()
{
...
foo1();
foo2();
foo3();
...
return 0;
}
在程序完成中 foo1() 总是在 foo2() 之前,而 foo2() 总是在 foo3() 之前(指完成的最高抽象)?
在我的实际程序中,foo2() 和 foo3() 是否发生取决于 foo1 是否是一个非常“长”的函数,这意味着如果 foo1() 中有一个巨大的 for 循环,那么当我完成并得到在程序结束时,foo2() 和 foo3() 不会发生。在我的程序 foo1()-foo3() 中访问相同的地图。 f001() 初始化地图中的所有可用元素, foo2(),foo3() 然后用从文件中读取的别名数据替换初始化。发生这种情况的任何原因?
这是两个函数,程序的其余部分有点大而且离题:
void loadDEADBEEF()
{
for (long long i=0; i<=268435888; i+=4)//268435888
{
MainMemory[i] = 3735928559;
HEXMainMemory[i] = "DEADBEEF";
}
}
void LoadMemory(string str)//load hex dump into memory
{
filecounter++;
vector<int> V;//temperary vector
vector<string> tempV;//temperary vector
ifstream inClientFile( str.c_str(),ios::in ); //stream object
vector<string> words;
string word;
int offset=0;
if ( !inClientFile ) cerr << "File couldn't be opened" << endl;//test if instruction file can be opened
//fill string vector with all file values and determines length of program
while (inClientFile >> word)words.push_back(word);//capture raw code from file
const int wordCount=words.size();//determine most efficient sizing for vectors
tempV.reserve(wordCount);//size vector
for(int i=0; i<wordCount; i++)
{
if (i==0 && words[i].length()==10) tempV.push_back(words[i]);//include first word to obtain data offset (memory insertion point)
if (words[i].length()==8
&& words[i].find(".")==string::npos )
tempV.push_back(words[i]);//cut out undesired strings from vector
}
for( int y=2; y<10; y++) offset+=hexCharValue(tempV[0][y])<<(4*(9-y));//convert offset from hex to decimal
tempV.erase(tempV.begin());//delete offset from vector
V.resize(tempV.size());//resize vector
for( int j=0; j<tempV.size(); j++ )//convert string hex to numerical decimal
{
for( int y=0; y<8; y++) V[j]+=hexCharValue(tempV[j][y])<<(4*(7-y));//4194608+4*
if (load_memory)
{
MainMemory.insert(mapType::value_type(4*j+offset,V[j]));//insert (location in memory,data)
HEXMainMemory.insert(pair<int, string>(4*j+offset,tempV[j]));
}
}
if( filecounter == 1 ) PC_start = offset-4;
}
所以,第一个函数是“foo1()”,第二个是“foo2()”。这是主要的:
#include
...
typedef map<int, int> mapType;//format of map: ( address, data )
typedef map<int, string> mapType2;//format of map: ( address, data )
mapType MainMemory;
mapType2 HEXMainMemory;
...
int main(int argc, char **argv)
{
...
loadDEADBEEF();
LoadMemory("hello_1.txt");//reginfo
...
return 0;
}
【问题讨论】:
-
foo1()是否对多个线程做任何事情?如果任何地方都没有线程,foo2()和foo3()将在foo1()完成后依次运行。如果有线程,则所有赌注都关闭,答案有点复杂。 -
您在标题中提到了内存,但您的问题根本没有提到内存使用情况。有什么遗漏吗?
-
foo1 是否溢出了一些全局变量,然后影响 foo2 和 foo3?尝试调试器并在 foo1 的末尾放置一个断点并开始跟踪;看看会发生什么。
-
您的代码的旁注是 MainMemory 的 value_type 是一个 int,因此可以容纳 2^31 - 1 的最大值,但是您正在执行“MainMemory[i] = 3735928559”,这将环绕。
-
你能澄清一下工作案例和“巨型循环”案例之间的区别吗?代码区别是什么?例如for 循环的边界等。
标签: c++