tags:

views:

115

answers:

7

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

Yes, they will always occur in that order.

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

glowcoder
It might be much faster to just describe it. I arbitrated my program a lot... Basically foo1 loads "deadbeef" into a bunch of memory elements in a map, then foo2 and 3 load the same memory map with actual data where necessary (hence replacing the deadbeef). Unfortunately the deadbeef is in memory locations where foo2 should have written to. Perhaps foo2 just doesn't write to memory correctly...
Dan Snyder
+1  A: 

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
Mike Caron
A: 

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

KLee1
A: 

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.

TreDubZedd
+1  A: 

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.

TheJuice
+1  A: 

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.

Gustavo V
It looks like foo1 2 and 3 do happen. Maybe the issue is in the foo2 isn't able to write to memory elements filled with deadbeef for some reason...
Dan Snyder
If a pointer has the value 0xdeadbeef, that means it's probably invalid. You should check to make sure all your pointers are initialized correctly.
Mike Caron
A: 

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.

Brian