0

I get a strange behavior when I compile the same program in Release in VS2010. If I run the program from cmd.exe multiple times, the results become gibberish. However, if I run the program in Debug, the result is always the same.

Starting a new command prompt makes the program give out the right outputs again.

Any idea of what might be causing this?

Screenshot of the command prompt

Edit : The code :

int main(int argc, char* argv[])
{
    int* R;
    int capacity;

    if (argc < 2)
    {
        cerr << "Veuillez entrer le chemin du fichier d'entree" << endl;
        return 1;
    }

    vector<BTS> stations;
    LoadFromFile(argv[1], stations, capacity);

    int count = 1;
    if (argc == 3)
    {
        count = atoi(argv[2]);
    }

    clock_t startClock = clock();

    R = new int[capacity+1];
    for(int j = 0; j < count; j++)
    {
        memset(R, 0, capacity + 1);

        for(unsigned int i=0; i < stations.size(); i++)
        {
            for(int j=capacity; j >= 0; j--)
            {
                if(j-stations[i].getNumberOfSubscribers() >= 0)
                {
                    R[j] = max(stations[i].getRevenue() + R[j-stations[i].getNumberOfSubscribers()], R[j]);
                }
            }
        }
    }

    // Print the results
    cout << "Value : " << R[capacity] << endl;
    cout << "Temps total : " << ((float)(clock() - startClock) / (float)CLOCKS_PER_SEC) << " s" << endl;

    delete[] R;

    return 0;
}

void LoadFromFile(std::string filePath, std::vector<BTS>& baseStations, int& capacity)
{
    fstream source(filePath);

    int numberOfStation;
    source >> numberOfStation;
    source >> capacity;

    for (int i = 0; i < numberOfStation; i++)
    {
        int index, revenue, numberOfSuscribers;
        source >> index;
        source >> revenue;
        source >> numberOfSuscribers;

        BTS station = BTS(index, revenue, numberOfSuscribers);

        baseStations.push_back(station);
    }

    source.close();
}

The rest of the code is only getters. Now for the file I give as input :

10 25
    1     7     6
    2     4     4
    3     7     6
    4     2     1
    5     7     8
    6     9    10
    7     4     5
    8    10    10
    9     1     1
   10    10    10
2
  • Obviously, some undefined behaviour. The code is required to do any meaningful diagnostics. It could be anything reading from or writing to a memory location you're not supposed to use in that way, or a dozen other things that lead to such memory access being done somewhere else, or something else entirely. Commented Nov 4, 2011 at 1:19
  • Will post the code, however I'm flabbergasted as of how can this happen only on the second execution of the program Commented Nov 4, 2011 at 1:24

1 Answer 1

2

Your memset isn't correct. You forgot the sizeof():

memset(R, 0, capacity + 1);

should be:

memset(R, 0, (capacity + 1) * sizeof(int));

Since you didn't zero all of R, the upper parts of it are undefined and are giving you junk data.

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

1 Comment

Damn. Hadn't done C++ in a while and now it shows. Thanks a lot!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.