ofstream ineterfering with the rest of code

Sep 26, 2010 at 1:13am
Hi, ive started learning c++ about 3 week ago so be lenient with the code style.

Im writing a solver for sokoban.
I've written a method to write a txt file (with times and other stats). The strange thing that is happening is that if I uncoment this line of code "ofstream aFile;" it changes the behavior of the rest of the program without the method in witch its declared ever being called (I put a flashy cout in the beginig of the method to make sure im not calling it by mistake)

The method can be emty with only this declaration (ofstream aFile;) and even with the method never being called, even then it ruins the behavior of the rest of the code.

This problem happens if and only if I compile with the O3 option and the line is not commented.(regardless if the method is called)
UPDATE: It does happens without any O option turned on the change in O's was just forcing a full ercompile

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
void Node::makeTab(const string tabName, const string levelsDir, int typeOfFile, const string comment, int start, int end){

    cout << "\n\n\n\nCALLED makeTab\n\n\n\n";

    Node startNode;
    string printStr;
    int mazeIndex;
    int boxMoves;
    string auxStr;
    clock_t clockStart, clockFinish;
    ostringstream strs;

    printStr.append(comment);
    printStr.append("\n\n\nMaze   Box Moves     Visited Nodes      Time\n");


    for(mazeIndex=start; mazeIndex<=end; mazeIndex++){

        printStr.append("\n");
        printStr.append(intToString((mazeIndex)));
        printStr.append(".txt      ");

        //reading new file
        auxStr = levelsDir;
        startNode = Node(auxStr.append("/").append(intToString(mazeIndex)).append(".txt"),typeOfFile);


        clockStart = clock();
        boxMoves = Node::aStar(startNode);//actual solving
        clockFinish = clock();


        if (boxMoves != 0){
            printStr.append(intToString(boxMoves));
            printStr.append("             ");
            printStr.append(intToString(closedSet.size()));
            printStr.append("             ");

            strs.str("");
            strs.clear();
            strs << (clockFinish - clockStart)/(double)CLOCKS_PER_SEC;
            auxStr = strs.str();

            printStr.append(auxStr);
        }
        else {
            printStr.append("not solved\n");
        }
        printStr.append("\n");

        //cout << printStr << endl;
    }

    //!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

    /*ofstream aFile;
    //aFile.open (tabName.c_str());
    //aFile << printStr;
    aFile.close();*/
}
Last edited on Sep 26, 2010 at 2:07pm
Sep 26, 2010 at 1:34am
>>changes the behavior
What changes when the line is undocumented?

>>O3
What is the O3 option?
Last edited on Sep 26, 2010 at 1:35am
Sep 26, 2010 at 12:13pm
"What changes when the line is undocumented?"
It starts to give wrong results.

"What is the O3 option?"
Optimize fully for speed
Sep 26, 2010 at 12:23pm
Can you say exactly what happens?
Sep 26, 2010 at 1:19pm
Some extra comments
Im using g++ to compile in ubuntu and valgrind finds nothing wrong with my code

I have this field in the class in which its declared (Node)

Board* boardP;

Board has (amongst others) this fields

std::list<int> boxesRow;
std::list<int> boxesCol;

When I compile with O3 and that line isn't commented (ofstream aFile;) the lists above start to loose elements/fill the elements with junk (even if the method in which the ofstream is declared is never called). If i compile without O3 or without that line it does the correct actions.

The code itself is to big to be posted but the main culprit is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
Board& Board::move(int row, int col, int direction, int typeOfMove) const{

    Board* newBoardP = new Board(*this);

    list<int>::iterator boxRowIter = newBoardP->boxesRow.begin();
    list<int>::iterator boxColIter = newBoardP->boxesCol.begin();

    //!Will crash if pointed box doesn't exist
    while( ! (*boxRowIter==row   &&   *boxColIter==col) ){//find the box to be moved
        ++boxRowIter;
        ++boxColIter;
    }

    //from here on box iters point to the box to be moved

    //moving box e positioning soko
    switch(direction){
        case NORTH:
            *(boxRowIter) = *(boxRowIter)-1;
            if(typeOfMove == PUSH){
                newBoardP->sokoCol = col;
                newBoardP->sokoRow = row;
            } else {//PULL
                newBoardP->sokoCol = col;
                newBoardP->sokoRow = row-2;
            }
            break;
        case SOUTH:
            *(boxRowIter) = *(boxRowIter)+1;
            if(typeOfMove == PUSH){
                newBoardP->sokoCol = col;
                newBoardP->sokoRow = row;
            } else {//PULL
                newBoardP->sokoCol = col;
                newBoardP->sokoRow = row+2;
            }
            break;
        case EAST:
            *(boxColIter) = *(boxColIter)+1;
            if(typeOfMove == PUSH){
                newBoardP->sokoCol = col;
                newBoardP->sokoRow = row;
            } else {//PULL
                newBoardP->sokoCol = col+2;
                newBoardP->sokoRow = row;
            }
            break;
        case WEST:
            *(boxColIter) = *(boxColIter)-1;
            if(typeOfMove == PUSH){
                newBoardP->sokoCol = col;
                newBoardP->sokoRow = row;
            } else {//PULL
                newBoardP->sokoCol = col-2;
                newBoardP->sokoRow = row;
            }
            break;
    }

    //setting reach
    newBoardP->reachCol.clear();
    newBoardP->reachRow.clear();
    newBoardP->setReach();

    return *newBoardP;
}


Last edited on Sep 26, 2010 at 1:25pm
Sep 26, 2010 at 2:04pm
UPDATE: It does happens without any O option turned on the change in O's was just forcing a full recompile

My hypothesis is that is some namespace issue since its not even being executed, I compile with the -Wall option and it shows no warnings.
Last edited on Sep 26, 2010 at 2:10pm
Topic archived. No new replies allowed.