How can a function finish it's objective, only to break down after returning to main?

-4

From what I've seen, sometimes in codeblocks a certain code will only work a certain part of the times it's run, how can it be possible, that without compiling such code again, it only works let's say two thirds of the times. Shouldn't codes always work or not work? Note that these codes don't depend on time and keep getting (0xC0000005) pointers error.

On the other hand, I have a code that does just the same.

I've tried making the function that I suspect doesn't work, print something that confirms it indeed finishes the function. I also suspect it's because it is a void function alterating a certain object.

graph G (0);
cout<<"Starting construction"<<endl;
G.Construct();
cout<<"Printing"<<endl;
cout<<G;

When it works, it prints the graph:

Starting construction
Ending construction
Printing
1 : (1, 2, 1)(1, 4, 1)

2 : (2, 1, 1)

3 : (3, 2, 1)

4 : (4, 2, 1)(4, 3, 1)

When it doesn't, it prints :

Starting construction
Ending construction
c++
asked on Stack Overflow Feb 5, 2019 by Mario Aldean

1 Answer

-3

I had:

    for (int i=0; i<6;i++)
    {
        S[i].resize(n);
    }

where S is a std::vector.

I replaced 6 (the loop extent) with m (even though m = 6).

This seems to have solved the issue. Undefined behaviour at its best.

answered on Stack Overflow Feb 5, 2019 by Mario Aldean • edited Feb 5, 2019 by Cody Gray

User contributions licensed under CC BY-SA 3.0