0

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;
}
13
  • Is foo1() doing anything with multiple threads? If there is no threading anywhere, foo2() and foo3() will run sequentially after foo1() as completed. If there are threads, all bets are off and the answer is a bit more complicated. Commented Jul 21, 2010 at 16:09
  • You allude to memory in the title, but your question doesn't mention memory usage at all. Is there something missing? Commented Jul 21, 2010 at 16:11
  • Is foo1 overflowing some global variable that then affect foo2 and foo3? Try a debugger and put a breakpoint at the end of foo1 and start tracing; see what happens. Commented Jul 21, 2010 at 16:12
  • A side note on your code is that the value_type of MainMemory is an int and therefore can hold a max value of 2^31 - 1 but you are doing "MainMemory[i] = 3735928559" which will wrap round. Commented Jul 21, 2010 at 16:29
  • Could you clarify the difference between the working case and the 'giant for loop' case e.g. what the code difference is? e.g. the bounds of the for loop etc. Commented Jul 21, 2010 at 16:33

7 Answers 7

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

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...
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.
1

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

Comments

1

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.

Comments

0

Yes, they will always occur in that order.

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

1 Comment

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...
0

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

Comments

0

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.