ich in der Hauptfunktion der folgenden habenÜbergabe eines 2D-Array an eine Funktion
void checkGrid(int rows, int cols, int **src){
cout<<"Testing";
int neighbors;
for(int i = 0; i < rows; i++){
for(int j = 0; i < cols; j++){
neighbors = getNeighborsCount(rows, cols, i , j, src);
}
}
}
int main(int argc, char *argv[])
{
if (argc != 3) {
cerr << "Invalid command line - usage: <input file> <number of threads>" << endl;
exit(-1);
}
// Extract parameters
ifstream ifile(argv[1]);
int num_threads = atoi(argv[2]);
// Set the number of threads
task_scheduler_init init(num_threads);
// Get the size of the problem - on the first line of the input file.
int size;
ifile >> size;
// TODO: Create and initialize data structures
int rows = size;
int cols = size;
int **grid_A = new int*[rows];
for(int i = 0; i < rows; i++){
grid_A[i] = new int[cols];
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
ifile >> grid_A[i][j];
}
}
for(int i = 0; i < size; i++){
for(int j = 0; j < size; j++){
cout << grid_A[i][j];
}
}
// Start the timer
tick_count start = tick_count::now();
// TODO: Execute the parallel algorithm
cout<<"Testing";
checkGrid(rows, cols, grid_A);
// Stop the timer
tick_count end = tick_count::now();
double run_time = (end-start).seconds();
cout << "Time: " << (end-start).seconds() << endl;
// TODO: Print the output to a file
// ofstream outfile("output.txt");
//
//
// outfile.close();
//
// // Append the peformance results to the results file.
// ofstream ofile("life.csv", ios::app);
// ofile << size << "," << num_threads << "," << run_time << endl;
// ofile.close();
for(int i = 0; i < rows; i++){
delete[] grid_A[i];
}
delete []grid_A;
return 0;
}
ich in der Lage bin, das Programm zu kompilieren, aber wenn ich die Build-Datei ausgeführt wird es nicht aus der „Testing“ drucken Erklärung. Könnte jemand darauf hinweisen, ob dies aufgrund der Art und Weise, wie das 2d-Array übertragen wird, oder etwas anderes ist.
EDIT: Posting gesamte Codesegment nach Wunsch auch ich bin neu in C++ und folgte nur einige Beispiele dafür, wie 2D-Arrays als Parameter an eine Funktion
Sie übergeben kein Array (was nur durch Referenz möglich ist), sondern einen Zeiger auf einen int-Zeiger, der zufällig auf den Anfang eines dynamisch zugewiesenen Arrays zeigt. Und dein Code kompiliert nicht. Post kompilierbarer Code bitte. –
@ChristianHackl also benutze ich den folgenden Befehl, um g ++ life.cpp -o life -lbb -std = C++ 11 zu kompilieren und es hat keine Fehler geworfen. Daher dachte ich, es wäre gut kompiliert, aber ich könnte falsch liegen – RRP
Wird es gedruckt "Zeit:"? Dies ist der richtige Zeitpunkt, um einen Debugger zu verwenden. –