开发者

Program Memory Issue

开发者 https://www.devze.com 2023-01-08 19:22 出处:网络
Can I always assume that if... int main() { ... foo1(); foo2(); foo3(); ... return 0; } that foo1() will always precede foo2() and foo2() will always precede foo3() in program completion (referring

Can I always assume that if...

int main()
{
    ...
    foo1();
    foo2();
    foo3();
    ...
    return 0;
}

that foo1() will always precede foo2() and foo2() will always precede foo3() in program completion (referring to highest abstraction of completion)?

In my actual program, whether foo2() and foo3() happen depend on whether foo1 is a very "long" function, meaning if there is a giant for loop in foo1(), then by the time I finish and get to the end of the program, foo2(), and foo3() don't happen. In my program foo1()-foo3() access the same map. f001() initializes all usable elements in the map and foo2(),foo3() then replace initializations with aliasing data read from files. Any reason why this is happening?

Here are the 2 functions, the rest of the program is sortof large and off topic:

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;
}

So, the first function is "foo1()" and the second is "foo2()". Here is main:

#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;
}


without real-code is very difficult help you.

use "print" outputs to follow your code (weir but help)

if you aren't using threads foo2 .. fooN will happen, except that some abnormal exit happen. to check this use print or something else to "trace" where your program are.


Assuming that you don't have threads or anything, you can always assume that foo1() will happen before foo2(), which will then also happen before foo3().

You can verify this by using a debugger or the console. Try putting this in each fooX():

std::out << "This is foo1()" << std::endl


Is your program exiting cleanly when foo1() has a 'big loop'? Can you post the code? It's possible that the big loop case actually causes a crash and hence foo2() and foo3() never execute.


Yes, they will always occur in that order.

Care to post the results of your code that show how it's not happening?


foo2 and foo3 will always occur unless something before their invocations (like in foo1) causes the program to exit (most likely abnormally).


If foo2() and foo3() are not being executed, it is likely that foo1() is throwing an exception, or otherwise exiting the application. As a debugging step, you might try wrapping foo1() in a try...catch block.


This is the kind of problem where a debugger would probably be helpful. I suggest placing breakpoints in strategic places to figure out where program execution ends up once foo1 is completed. My guess would be that the program is exiting in the middle of foo1 due to a call to exit or a crash.

0

精彩评论

暂无评论...
验证码 换一张
取 消