Похожие чаты

Does anybody know what is mismatched free or delete means

in valgrind?

6 ответов

7 просмотров

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()

Bruria- Автор вопроса
Sunbro
it means that there is a mismatched free or delete...

//-----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; }

Bruria- Автор вопроса
Sunbro
it means that there is a mismatched free or delete...

//-----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; }

Bruria- Автор вопроса
Sunbro
it means that there is a mismatched free or delete...

אסתרר, [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;

Похожие вопросы

Обсуждают сегодня

30500 за редактор? )
Владимир
47
а через ESC-код ?
Alexey Kulakov
29
Anyone knows where there are some instructions or discort about failed bridge transactions ?
Jochem
21
Чёт не понял, я ж правильной функцией воспользовался чтобы вывести отладочную информацию? но что-то она не ловится
notme
18
Привет)) уже кажется эту тему перемусолили, но вот я так и не понял. Я сейчас сижу на 27дюймов 2к мониторе. На Актуальной макоси, если я куплю 27д 4к монитор: - будет ли изобр...
Vladislav Piskunov
16
Also, why can’t the community have a vote/ say when it comes to initiatives like buybacks. Isn’t the point of crypto decentralisation? Don’t we deserve input as long term supp...
👨🏽‍🦰
13
any reference of this implementation?
BitBuddha
29
Страшнейшая правда про списки ЦБ. С первых дней жизни P2P сферы, молодые человеки, начитавшись законодательной базы и "внутренних" документов, решили, что им противостоит сер...
Foxcool
3
У меня есть функция где происходит это: write_bit(buffer, 1); write_bit(buffer, 0); write_bit(buffer, 1); write_bit(buffer, 1); write_bit(buffer, 1); w...
~
14
Hi guys, any problem with Pulsebrige? Trying to transfer from wETH to ETH. First it tells me to connect my metamask "through mobile app" not desktop. Then I did and confirmed ...
Snowflakecrypto
13
Карта сайта