VS2010 릴리스에서 동일한 프로그램을 컴파일 할 때 이상한 동작이 발생합니다. cmd.exe에서 여러 번 프로그램을 실행하면 결과가 횡설수가됩니다. 그러나 디버그에서 프로그램을 실행하면 결과는 항상 동일합니다.동일한 프로그램을 여러 번 실행할 때 정의되지 않은 동작
새 명령 프롬프트를 시작하면 프로그램이 올바른 출력을 다시 제공합니다.
어떤 원인 일 수 있습니까?
편집 : 코드 :
는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();
}
코드의 나머지 부분은 게터입니다. 이제 파일 I 입력으로 제공 :
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
분명히 일부 정의되지 않은 동작입니다. 이 코드는 의미있는 진단을 수행하는 데 필요합니다. 당신이 그런 방식으로 사용하지 않아야하는 메모리 위치에서 읽거나 쓰는 것이거나 다른 곳에서 행해지는 그러한 메모리 액세스로 이어지는 12 가지 다른 것들이 될 수 있습니다. – delnan
코드를 게시합니다. 그러나 프로그램의 두 번째 실행시에만 어떻게 될 수 있는지에 대해 나는 깜짝 놀랐다. –