it means that there is a mismatched free or delete... also lazy pic of code
use pastebin please
Make sure - new type is deleted with delete - new type[n] is deleted with delete[] - malloc() is freed with free()
//-----erase memory----- //recieves a pointer to pointer and clears the memory void erase_memory(int &mat,int rows) { //goes over the matrix rows and clears every line for (int i = 0; i < rows;i++) { delete mat[i]; } //clears the matrix delete[] mat; } //-----enter values----- //this function creates a matrix of size rows X cols as the user's request, //and initilizes with input values. int enter_value(int &mat) { //varibles declartion, will save the sizes of the matrix int rows, cols; //revieves the number of rows from the user cin>>rows; //dynamiclly allocates matrix with 'rows' rows mat=new(std::nothrow)int*[rows]; //checks if the allocation succeeded or not. if(mat==NULL) { cerr<<"ERROR"; exit(EXIT_FAILURE); } //goes over the rows, and for each row recieves the current //row size and reads values as the size of the current row. for(int i=0;i<rows;i++) { cin>>cols; //dynamicly allocates memory for each row 'cols'+1 cols mat[i]=new(std::nothrow)int[cols+1]; //checks if the allocation succeeded or not. if(mat[i]==NULL) { cerr<<"ERROR"; exit(EXIT_FAILURE); } //the first cell of the row is equel to the size of the current row. mat[i][0]=cols; for(int j=1;j<cols+1;j++) cin>>mat[i][j]; } //returns the number of rows as recieved from the user return rows; } //-----sum----- //recieves a matrix and number of rows and cacultes the matrix //values sum int sum(int**mat,const int rows) { //declares and initilizes a sum veriable int sum_mat=0; //goes over matrix values for(int i=0;i<rows;i++) { for(int j=1;j<=mat[i][0];j++) //adds element to the total sum sum_mat+=mat[i][j]; } //returns the sum return sum_mat; }
//-----struct section----- struct Sentences { char _data ; int _num_of_sentences ; } ; //-----prototypes----- void read_data(struct Sentences &strings); void allocate_dynamic(struct Sentences &strings); struct Sentences convert_to_words(const struct Sentences &strings); int count_words(const char *sentence); void copy_words(const struct Sentences &strings,struct Sentences &words); void print_words(const struct Sentences &words); void delete_memory(struct Sentences &string); //-----main----- int main() { struct Sentences strings; read_data(strings); struct Sentences words=convert_to_words(strings); print_words(words); delete_memory(strings); delete_memory(words); return EXIT_SUCCESS; } void delete_memory(Sentences &strings) { for(int i = 0;i<strings._num_of_sentences;i++) delete strings._data[i]; delete strings._data; } void read_data(struct Sentences &strings) { cin>>strings._num_of_sentences; cin.get(); allocate_dynamic(strings); for(int i=0;i<strings._num_of_sentences;i++) { cin.getline(strings._data[i], LENGTH); } } void allocate_dynamic(struct Sentences &strings) { strings._data=new(std::nothrow)char*[strings._num_of_sentences]; if(strings._data==NULL) { cerr<<"ERROR"; exit(EXIT_FAILURE); } for(int i=0;i<strings._num_of_sentences;i++) { strings._data[i]=new(std::nothrow)char[LENGTH]; if(strings._data[i]==NULL) { cerr<<"ERROR"; exit(EXIT_FAILURE); } } } struct Sentences convert_to_words(const struct Sentences &strings) { struct Sentences words; words._num_of_sentences=0; for(int i=0;i<strings._num_of_sentences;i++) { words._num_of_sentences+=count_words(strings._data[i]); } allocate_dynamic(words); copy_words(strings,words); return words; } int count_words(const char *sentence) { int counter=0,index=0; while(sentence[index]!='\0') { if(sentence[index]==' ') counter++; index++; } return counter+1; } void copy_words(const struct Sentences &strings,struct Sentences &words) { int index_words=0,start=0,index=0; for(int i=0;i<strings._num_of_sentences;i++) { start=0; index=0; while(strings._data[i][index]!='\0') { if(strings._data[i][index]==' ') { strncpy(words._data[index_words],strings._data[i]+start,index-start); index_words++; start=index+1; } index++; } strncpy(words._data[index_words],strings._data[i]+start,index-start); index_words++; } } void print_words(const struct Sentences &words) { for(int i=0;i<words._num_of_sentences;i++) { if(strlen(words._data[i])) cout<<words._data[i]<<endl; } } אסתרר, [21.04.21 14:45] /* * ex1a.cc * * Created on: Apr 9, 2021 * Author: esthernu */ //-----include section----- #include <iostream> #include <cstdlib> #include <new> //-----using sectiuon----- using std::cin; using std::cout; using std::endl; using std::cerr; //-----prototypes----- //this function creates a matrix of size rows X cols as the user's request, //and initilizes with input values. int enter_value(int &mat); //this function recieves a matrix with number of rows in the matrix, //and calculates the sum of all matrix values. int sum(int**mat,const int rows); //recieves matrix and number of rows and clears matrix memory void erase_memory(int &mat,int rows); //-----main----- int main() { int mat;/*declartion of pointer to pointer - we use this to allocate memory for matrix.*/ int rows=enter_value(mat);/*sending to function to resize the matrix and to enter values as the user request, and recieves back the num of rows.*/ cout<<sum(mat,rows);/*prints the sum of the matrix values that the function 'sum' has calculated.*/ erase_memory(mat,rows);//clears the matrix memory. return EXIT_SUCCESS; }
אסתרר, [20.04.21 21:49] /* * ex1a.cc * * Created on: Apr 9, 2021 * Author: esthernu */ //-----include section----- #include <iostream> #include <cstdlib> #include <new> //-----using sectiuon----- using std::cin; using std::cout; using std::endl; using std::cerr; //-----prototypes----- //this function creates a matrix of size rows X cols as the user's request, //and initilizes with input values. int enter_value(int** &mat); //this function recieves a matrix with number of rows in the matrix, //and calculates the sum of all matrix values. int sum(int**mat,const int &rows); //recieves matrix and number of rows and clears matrix memory void erase_memory(int &mat,int rows); //-----main----- int main() { int mat; //declartion of pointer to pointer - we use this //to allocate memory for matrix. int rows=enter_value(mat);//sending to function to resize the matrix and //to enter values as the user request, and recieves back the num of rows. cout<<sum(mat,rows);//prints the sum of the matrix values that the //function 'sum' has calculated. erase_memory(mat,rows);//clears the matrix memory. return EXIT_SUCCESS; } //recieves a pointer to pointer and clears the memory void erase_memory(int &mat,int rows) { //goes over the matrix rows and clears every line for (int i = 0; i < rows;i++) { delete mat[i]; } //clears the matrix delete[] mat; } int enter_value(int &mat) { //varibles declartion, will save the sizes of the matrix int rows, cols; //revieves the number of rows from the user cin>>rows; //dynamiclly allocates matrix with 'rows' rows mat=new(std::nothrow)int*[rows]; //checks if the allocation succeeded or not. if(mat==NULL) { cerr<<"ERROR"; exit(EXIT_FAILURE); } //goes over the rows, and for each row recieves the current //row size and reads values as the size of the current row. for(int i=0;i<rows;i++) { cin>>cols; mat[i]=new(std::nothrow)int[cols+1]; //checks if the allocation succeeded or not. if(mat[i]==NULL) { cerr<<"ERROR"; exit(EXIT_FAILURE); } //the first cell of the row is equel to the size of the current row. mat[i][0]=cols; for(int j=1;j<cols+1;j++) cin>>mat[i][j]; } //returns the number of rows as recieved from the user return rows; } //recieves a matrix and number of rows and cacultes the matrix //values sum int sum(int**mat,const int &rows) { //declares and initilizes a sum veriable int sum_mat=0; //goes over matrix values for(int i=0;i<rows;i++) { for(int j=1;j<=mat[i][0];j++) //adds element to the total sum sum_mat+=mat[i][j]; } //returns the sum return sum_mat; } אסתרר, [20.04.21 21:50] /* * ex1b.cc * * Created on: Apr 10, 2021 * Author: esthernu */ //-----include section------- #include <iostream> #include <cstdlib> #include <new> #include <cstring> //-----using section----- using std::cin; using std::cout; using std::endl; using std::cerr; //-----const----- int const LENGTH=1000;
Обсуждают сегодня