../images/Emo53.gifלמבינים בC++
היי לכולם, משהו יודע למה הפונקציה הזאת עובדת מצויין בפעם הראשונה, ובפעם השניה המערכים שמוחזרים ממנה מאופסים? bool AddName(char* NewName, int Size, char** Names) { // Variables definition char** tmp; int curr_index; // Code Section // Allocate new dinamic array tmp = new char* [Size+1]; // Check if the allocation succeded if (tmp == NULL) { // Return false return false; } else { // If the allocation succeded copy old matrix to the temporary one for(curr_index = 0; curr_index<Size; curr_index++) { // Allocate new memory to copy the name tmp[curr_index]=Names[curr_index]; Names[curr_index] = NULL; } // Insert the new name last tmp[Size] = new char[strlen(NewName)+1]; // Check if the allocation succeded if (tmp[Size] == NULL) { return false; } // copy the new name to it´s location in the matrix strcpy(tmp[Size], NewName); } // Release old pointers delete[] Names; // Allocate the names array again Names = new char*[Size+1]; // Check the allocation if (Names == NULL) { return false; } for(curr_index = 0; curr_index<=Size; curr_index++) { // Allocate new memory to copy the name Names[curr_index]=new char[strlen(tmp[curr_index])+1]; strcpy(Names[curr_index], tmp[curr_index]); } // Delete the temporary array delete[] tmp; // The action succeded return true; } תודה!!
היי לכולם, משהו יודע למה הפונקציה הזאת עובדת מצויין בפעם הראשונה, ובפעם השניה המערכים שמוחזרים ממנה מאופסים? bool AddName(char* NewName, int Size, char** Names) { // Variables definition char** tmp; int curr_index; // Code Section // Allocate new dinamic array tmp = new char* [Size+1]; // Check if the allocation succeded if (tmp == NULL) { // Return false return false; } else { // If the allocation succeded copy old matrix to the temporary one for(curr_index = 0; curr_index<Size; curr_index++) { // Allocate new memory to copy the name tmp[curr_index]=Names[curr_index]; Names[curr_index] = NULL; } // Insert the new name last tmp[Size] = new char[strlen(NewName)+1]; // Check if the allocation succeded if (tmp[Size] == NULL) { return false; } // copy the new name to it´s location in the matrix strcpy(tmp[Size], NewName); } // Release old pointers delete[] Names; // Allocate the names array again Names = new char*[Size+1]; // Check the allocation if (Names == NULL) { return false; } for(curr_index = 0; curr_index<=Size; curr_index++) { // Allocate new memory to copy the name Names[curr_index]=new char[strlen(tmp[curr_index])+1]; strcpy(Names[curr_index], tmp[curr_index]); } // Delete the temporary array delete[] tmp; // The action succeded return true; } תודה!!