I am getting error of "Double free or corruption(out)" after I print my output. But this error is only coming for small inputs. For bigger inputs program doesn't throw that error. When I create the multidimensional arrays inside the main and delete them, I do not get the error. I have only posted the part of the code which is relevant to this issue here. Please kindly explain how to resolve the issue.
#include<iostream>
#include<vector>
using namespace std;
class Knapsack{
public:
int noItems, capacity, value, weight;
int *weightArray, *valueArray;
int **ValueMatrix, **BacktrackMatrix;
vector<int> itemsChosen;
~Knapsack();
void getInputs(); // reads in data
void findItems(); // calculates best value of items
void backTrack(int row, int col); // backtracks items selected
void print(); //prints out data
};
Knapsack::~Knapsack()
{
delete[] weightArray;
delete[] valueArray;
for(int i=1;i<=noItems;i++)
{
delete[] ValueMatrix[i];
}
delete[] ValueMatrix;
for(int i=1;i<=noItems;i++)
{
delete[] BacktrackMatrix[i];
}
delete[] BacktrackMatrix;
}
void Knapsack::getInputs()
{
cin>>noItems;
cin>>capacity;
weightArray=new int[noItems];
valueArray=new int[value];
for(int i=1;i<=noItems;i++)
{
cin>>value;
valueArray[i]=value;
}
for(int i=1;i<=noItems;i++)
{
cin>>weight;
weightArray[i]=weight;
}
ValueMatrix=new int*[noItems];
for(int i=1;i<=noItems;i++)
{
ValueMatrix[i]=new int[capacity+1];
}
BacktrackMatrix=new int*[noItems];
for(int i=1;i<=noItems;i++)
{
BacktrackMatrix[i]=new int[capacity+1];
}
}
int main()
{
Knapsack *knap=new Knapsack();
knap->getInputs();
knap->findItems();
knap->print();
delete knap;
return 0;
}
I believe the root of your issue is caused by the allocation of valueArray, and the fact that you are iterating out of bounds.
The line valueArray=new int[value]; initializes valueArray with an array of size value which is an uninitialized variable. Perhaps you meant to use noItems?
Also, as songyuanyao pointed out in the comments, your for loops look like for(int i=1;i<=noItems;i++) which starts the counter at 1 and finishes with the counter at noItems, which is erroneous. In a lot of languages, C++ included, arrays start at index 0 (meaning the first item is array[0], not array[1]) and the last item is one minus the size of the array (so the last item of an array with 5 elements is array[4]).
If you change your for loop to start at 0 and end one element before noItems you should be golden. That would be for(int i = 0; i < noItems; i++ )
What's probably happening with smaller allocations is the different chunks of memory are arranged sequentially in the same area of the memory heap, so when you overrun the buffer with data, you're smashing new's bookkeeping data.
When you have larger allocations, the new memory can't fit as cleanly into the free space of the heap, so the allocator ends up leaving some slack space between the allocations. Thus, a small overrun doesn't destroy heap information.
Related
I know those are basics, but I can't get over with it.
I want to add x times y elements to my array.
When I add 1x100000 elements it's fine.
However, when I try to add 5000x5000 elements, it stops immediately, with many different errors, never bad alloc.
I'd be extremaly gladful if someone can give me a clue about this...
#include <iostream>
#include <numeric>
#include <chrono>
#include <cstdlib>
class Array
{
int *tab; //dynamic array
int cnt; //count
public:
Array();
void clearArray();//deleting table
void test_addValueToArray(int index, int value);
};
Array::Array() : tab(nullptr), cnt(0){;}
void Array::clearArray()
{
if(cnt==0)
return;
delete tab;
cnt=0;
}
void Array::test_addValueToArray(int index, int value)
{
int *NewTab = new int[cnt+1];
for(int i=0;i<index;++i)
NewTab[i]=tab[i];
NewTab[index]=value;
for(int i=index+1;i<cnt+1;++i)
NewTab[i]=tab[i-1];
delete[] tab;
tab=NewTab;
++cnt;
}
Array myArray;
int main()
{
int elements, times;
std::cout<<"How many elements?";
std::cin>>elements;
std::cout<<"How many times?";
std::cin>>times;
auto start = std::chrono::high_resolution_clock::now();
for(int j=0; j<times; ++j)
{
for(int i=0; i<elements ; ++i)
myArray.test_addValueToArray(0,rand()%1000);
myArray.clearArray();
}
auto stop = std::chrono::high_resolution_clock::now();
std::chrono::duration<double> time=stop-start;
std::cout<<"Avarage time:"<<time.count()/times<<"\n";
}
Problem is because of use of delete[] for lots of continious memory deallocation,
plus you have used only delete tab which should be changed to delete[] tab in cleararray so you can use smart pointers.
plus: In your test_addValueToArray() when cnt=0 you are trying to deallocate memory which is not allocated at all and i didn't understand why you are passing 0 as index while calling addValueToArray() i think it should be i at that point.
So once you fix this and use smart pointers it should work else it will show bad_alloc exception after running for some time.
I'm using xcode and instruments to find my memory leak, it is complaining that this following code creates memory leak:
double **cn2;
cn2= new double*[noOfItem];
for(int i=0;i<noOfItem;i++)
{
cn2[i]=new double[noOfItem];
}
for(int i=0;i<noOfItem;i++)
{
for(int j=0;j<noOfItem;j++)
{
cn2[i][j]=getCN2(noOfItem,i,j,pearson,cn2CutOff);
//cout<<i<<" "<<j<<" "<< cn[i][j]<<endl;
}
}
for(int i=0;i<noOfItem;i++)
{
delete [] cn2[i];
}
delete [] cn2;
This is the function of getCN2, it is used to fill the 2d array:
double getCN2(int _isize,int _i1,int _i2,double **sim,double _cutoff)
{
//cout<< med<<" "<<med1<<endl;
int count=0;
for(int i=0; i<_isize; i++)
{
//if(sim[_i1][i]>_cutoff && sim[_i2][i]>_cutoff)
if(sim[_i1][i]>sim[_i1][_i2] && sim[_i2][i]>sim[_i1][_i2] && sim[_i1][_i2]>0)
{
count++;
}
}
//cout << rez/sqrt(rez1*rez2) <<endl;
return count;
}
And there is no memory leak if I change my code into:
double **cn2= new double*[noOfItem];
for(int i=0;i<noOfItem;i++)
{
cn2[i]=new double[noOfItem];
}
for(int i=0;i<noOfItem;i++)
{
for(int j=0;j<noOfItem;j++)
{
cn2[i][j]=getCN2(noOfItem,i,j,pearson,cn2CutOff);
//cout<<i<<" "<<j<<" "<< cn[i][j]<<endl;
}
}
for(int i=0;i<noOfItem;i++)
{
delete [] cn2[i];
}
delete [] cn2;
The only possible reason I could think of is that when I call double **cn2; it points to something already and when I call cn2= new double*[noOfItem]; it points to something else and the original **cn2 didn’t get freed? Has anyone else encountered this problem before? It is really weird...Do I have to write them in one line instead of 2 when using new to allocate?
do preventing coding to avoid such kind of error
Pointer initialization is must
double *ptr = nullptr;
Check is required before allocating memory
if (nullptr==ptr) { ....}
If you writing new code then avoid usage of raw pointer
unique_ptr. Allows exactly one owner of the underlying pointer.
shared_ptr. Reference-counted smart pointer. ...
weak_ptr. Special-case smart pointer for use in conjunction with shared_ptr
Debugging technique:-
Use of visual studio to find memory related error
Buffer Overflow explained
Use diagnostic tool
Vld and parallel studio
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I am doing my c++ homework on an online judge. There are m strings with a length of n. I need to find the minimal expression of a new string, and then insert it in an trie tree. For each string, I need to return the "positon number" of the first identical string.
Following is my code:
#include <cstdio>
using namespace std;
struct trie_node
{
trie_node * firstSon;
trie_node * nextBro;
char value;
bool isKey;
int firstPos;
trie_node(char value):firstSon(NULL), nextBro(NULL), value(value), isKey(false), firstPos(-1){}
};
class trie_Tree
{
public:
trie_Tree();
int searchStr(char* desStr, int len, int selfPos);
private:
trie_node* searchChar(trie_node* fatherNode, char desChar);
trie_node* root;
};
trie_Tree::trie_Tree()
{
root = new trie_node('0');
}
int trie_Tree::searchStr(char * desStr, int len, int selfPos)
{
trie_node* fatherNode = root;
for (int i=0; i<len; i++)
{
fatherNode = searchChar(fatherNode, desStr[i]);
}
if (!fatherNode->isKey)
{
fatherNode->isKey=true;
fatherNode->firstPos=selfPos;
}
return fatherNode->firstPos;
}
trie_node* trie_Tree::searchChar(trie_node* fatherNode, char desChar)
{
if (fatherNode->firstSon==NULL)
{
fatherNode->firstSon = new trie_node(desChar);
return fatherNode->firstSon;
}
trie_node* travNode = fatherNode->firstSon;
while (travNode->nextBro!=NULL)
{
if (travNode->value==desChar) return travNode;
travNode=travNode->nextBro;
}
if (travNode->value==desChar) return travNode;
else
{
travNode->nextBro = new trie_node(desChar);
return travNode->nextBro;
}
}
char* getMinPre(char *s, int _size)
{
int min=0, trav=1;
while (trav<_size && min<_size)
{
int i;
for (i=0; i<_size; i++)
{
if (s[(min+i)%_size]<s[(trav+i)%_size])
{
trav=trav+i+1;
break;
}
else if (s[(min+i)%_size]>s[(trav+i)%_size])
{
min=trav;
trav=trav+1;
break;
}
}
if (i==_size) break;
}
char * result=new char[_size];
for (int i=0; i<_size; i++)
{
result[i]=s[(min+i)%_size];
}
return result;
}
int main()
{
int m, n, result=0;
scanf("%d %d", &m, &n);
trie_Tree tt=trie_Tree();
char* s=new char[n+1];
for (int i=0; i<m; i++)
{
scanf("%s", s);
s=getMinPre(s, n);
result = tt.searchStr(s, n, i);
printf("%d\n", result);
}
delete[] s;
return 0;
}
I compiled my code with VS and g++, and runned my program lots of times for testing. It worked perfectly.
But when the online judge system returned runtime error(exitcode 6).
I googled "exit code 6". It is raised by the program itself, e.g. by making the abort() system call. It can be caused by new and delete operation. But I still cannot debug my code.
Anyone can help me?
That's a lot of code, but some things to look into:
Inside your main function you allocate s: char* s=new char[n+1];.
You pass s into char* getMinPre(char *s, int _size).
getMinPre allocates another buffer, and returns it, overwriting s: s=getMinPre(s, n); (memory leak of initial s buffer).
This potentially happens a lot in the main function's loop, hence you may run out of memory. (getMinPre allocating and overwriting the pointer to allocated buffer).
Since this is an online judge platform, I'd recommend coming up with extreme test cases (min, max elements, tons of iterations) and running them locally.
Also: add some debug information. You can even encapsulate them within #ifdef so you won't have to remove them.
In your trie_Tree constructor, you use new to allocate dynamic memory, but I don't find you delete that object anywhere. Similarly, in searchChar, you allocate a lot of son nodes but never delete them. Also in getMinPre. All of them will lead to memory leak. The only memory you freed is the result in main().
In C++, dynamic memory management is a really complex topic and error prone, every time you allocate some memory with new, you need to keep in mind deallocate them with delete somewhere. Like in C, every time you use malloc(), you need free().
There are a lot of libraries you can use instead of managing the memory yourself. For a linked list, you may consider std::vector in header<vector>.
BTW, I think this code looks like C with Class, not C++.
I'm trying to implement an unbounded array: What is an unbounded array?
More details on this page:
http://www.cs.cmu.edu/~fp/courses/15122-s11/lectures/12-ubarrays.pdf
This is the code:
#include <iostream>
#include <cstdlib>
using namespace std;
class UBArray
{
public:
int *arr, *arrN, j, *pos; //Initial array is arr. The position of arr is stored in pos. arrN is the new array created when size = limit.
int size, limit; //size is the current size of the array and limit is the total size available untill a new array is created.
UBArray()
{
size = 0;
limit = 10;
arr = new int[10];
pos = arr;
}
private:
void increment()
{
// New array arrN is created and then the values in the old arrays is put into the new array.
// Limit is increased by 10 - this is the extra space the new array contributres.
// pos which carries the address of the current array now carries the address of the new array.
// Later when a new array is created its address will be on the heap which is empty. This address is replace the address stored
// in the arrN. The older array can still be accessed for the array updation process by using the variable pos.
// IMPORTANT: I had initially tried to delete the older array to space space but while trying to debug the segmentation fault, I have
// removed it. I will be adding it again once the error has been fixed.
arrN = new int[size + 10];
for (j = 0; j < size; j++)
{
arrN[j] = pos[j];
}
limit = limit + 10;
pos = arrN;
}
public:
void push(int n)
{
if (size<limit)
{
size++;
pos[size-1]=n;
}
else
{
increment();
push(n);
}
}
int pop()
{
int p = pos[size-1];
size--;
return p;
}
};
int main()
{
UBArray array;
int num;
cout << "Enter 36 elements: ";
for (int k = 0; k<36; k++)
{
cin >> num;
array.push(num);
}
cout << endl << "The last element is : " << array.pop();
}
I have tried to give comments in the code to make it understandable to the reader. I'm copying some of it here:
Initial array is arr. The position of arr is stored in pos. arrN is the new array created when size = limit.
size is the current size of the array and limit is the total size available until a new array is created.
New array arrN is created and then the values in the old array are put into the new array.
Limit is increased by 10 - this is the extra space the new array contributres.
pos which carries the address of the current array now carries the address of the new array.
Later when a new array is created its address will be on the heap which is empty. This address is replaced the address of arrN. The older array can still be accessed for the array updation process by using the variable pos which will be updated by the old values have been copied to the new one.
I get segmentation fault during execution. I have tried to use cout statements to debug the code but it seems really confusing. I could see loops both inside and outside the for loop inside the increment method. I'm unable to figure out much. Any help is appreciated.
UPDATE: As pointed out by jrok, I changed the code and the seg fault is gone. But I'm getting seg fault again at the creation of the 3rd array.
UPDATE 2 Everything fixed now. Thank you.
arr = new int(10*sizeof(int));
That creates a single int, initialized to the value of 10*sizeof(int). The loop you wrote right after this statement runs out of bounds and it's cause of segmentation fault.
What you want is the array form of new:
arr = new int[10]; // note 10 only, new expression knows the size
// of the type it allocates
Note that when you assign the pointer to the new array to the pointer to the old array you lose the handle to it and create a memory leak:
int* arr = new int[10];
int* new_arr = new int[20];
arr = new_arr; // original array at arr has leaked
You need to delete[] arr before you reassign it. Also, I see no use for the third (pos) pointer. Not even for arrN, for that matter. One will do. Create a local pointer inside increment and assign it to arr when you're done deallocating the old array.
Finally, what people have been telling you in the comments, unless this is a learning exercise, don't try to reinvent the wheel and use std::vector instead.
An unbounded array only needs 3 data members (rather than 6): the address of the beginning of the data buffer, the capacity of the buffer, and the actual size (of the part of the buffer used so far). When expanding, you will temporarily need to hold the address of the new buffer in an automatic variable. Also, you should avoid leaking the memory of previous buffers. A simple layout is like this:
struct ua
{
int size,capacity,*buff; // 3 data members only
ua(int n) // constructor: n = initial capacity
: size(0) // initially empty
, capacity(n<0?0:n) // guard against n<0
, buff(capacity?new int[capacity]:0) {} // only allocate if n>0
~ua() { delete[] buff; } // destructor: note: delete[] 0 is ok
void empty() const { return size==0; } // is array empty?
void push(int x) // add another datum at back
{
if(size==capacity) { // buffer is full: we must expand
if(capacity) capacity+=capacity; // double capacity
else capacity=1; // but ensure capacity>0
int*nbuff=new int[capacity]; // obtain new buffer
if(size)
memcpy(nbuff,buff,size*sizeof(int)); // copy data from old to new buffer
delete[] buff; // free memory form old buffer
buff=nbuff; // set member buff to new buffer
}
buff[size++]=x; // write; increment size (post-fix)
}
int pop() // ill-defined if empty()
{ return buff[--size]; } // read; decrement size (pre-fix)
int operator[](int i) const // ill-defined if i<0 or i>=size
{ return buff[i]; }
int&operator[](int i) // ill-defined if i<0 or i>=size
{ return buff[i]; }
// you may add more functionality, for example:
void shrink(); // reduces capacity to size
void reserve(int n); // extends capacity to n, keeping data
ua(ua const&other); // copy buffered data of other
void swap(ua&other); // swap contents with other (no copying!)
};
I have to create a class Histogram and make operations on this class. The input can be one dimensional array or a two dimensional array. The problem appears when i convert the array into a matrix. This what i have tried so far. The error is <Unable to read memory>
histrogram.h
#ifndef HISTOGRAM_H
#define HISTOGRAM_H
#include<iostream>
class Histogram
{
private:
int** matrix;
int lines;
void SortMatrix();
public:
Histogram(){ }
Histogram(int elements[], int elementsNr);
Histogram(int** m, int l);
void Print();
};
#endif
historgram.cpp
#include"histogram.h"
using namespace std;
Histogram::Histogram(int** m, int l)
{
matrix=m;
lines=l;
SortMatrix();
}
Histogram::Histogram(int elements[], int elementsNr)
{
lines=0;
//initialize matrix : elementrNr lines and 2 columns
int** matrix=new int*[elementsNr];
for(int i=0;i<elementsNr;i++)
{
matrix[i]=new int[2];
matrix[i][0]=INT_MIN;
matrix[i][1]=INT_MIN;
}
//search each element from the array in the matrix
bool found=false;
for(int i=0;i<elementsNr;i++)
{
found=false;
for(int j=0;j<elementsNr;j++)
{
//the element was found in the matrix ( on the first column )
if(matrix[j][0] == elements[i])
{
matrix[j][1]++;
found=true;
break;
}
}
if(!found)
{
matrix[lines][0]=elements[i];
matrix[lines][1]=1;
lines++;
}
}
SortMatrix();
}
void Histogram::SortMatrix()
{
bool flag=true;
int temp;
for(int i=0;(i<lines) && flag;i++)
{
flag=false;
if(matrix[i+1][0]>matrix[i][0])
{
temp=matrix[i][0];
matrix[i][0]=matrix[i+1][0];
matrix[i+1][0]=temp;
flag=true;
}
}
}
void Histogram::Print()
{
for(int i=0;i<lines;i++)
{
cout<<matrix[i][0]<<" : " <<matrix[i][1]<<endl;
}
}
main.cpp
#include"histogram.h"
#include<iostream>
using namespace std;
int main()
{
int arr[]={6,7,3,1,3,2,4,4,7,5,1,1,5,6,6,4,5};
Histogram h(arr,17);
h.Print();
}
Here
int** matrix=new int*[elementsNr];
replace with
matrix=new int*[elementsNr];
becausematrix is already a member variable. You are creating a new temporary variable double pointer named matrix and allocating memory to it rather than your member variable matrix
A couple of people have already given you advice about how to fix some of the problems with this code. I'll give slightly different advice that may initially seem a bit brutal by comparison, but I'll try to demonstrate how it's honestly useful rather than nasty.
I would throw out your existing code with the possible exception of what you have in main, and start over, using an std::map. What you're doing right now is basically trying to re-create the capabilities that std::map already provides (and even when your code is fixed, it's not doing the job as well as std::map does right out of the box).
Using map, your whole program comes out to something like this:
std::ostream &operator<<(std::ostream &os, std::pair<int, int> const &d) {
return os << d.first << " : " << d.second;
}
int main() {
std::map<int, int> h;
for (int i=0; i<17; i++)
++h[arr[i]];
std::copy(h.begin(), h.end(),
std::ostream_iterator<std::pair<int, int> >(std::cout, "\n"));
return 0;
}
If you want to maintain virtually the same interface as your histogram class provided, it's pretty easy to do that -- the for loop goes into the constructor, the copy into print (and SortMatrix disappears, because a map is always sorted).
By doing this, you change from an O(N2) algorithm to an O(N log N) algorithm. The bugs others have pointed out disappear completely, because the code that contained them is no longer needed. The only real disadvantage I can see is that the result will probably use a bit more memory -- it uses a balanced tree with individually allocated nodes, which is likely to introduce a fair amount of overhead for nodes that only contain 2 ints (and a bit for balancing). I can't quite imagine worrying about this though -- long before you have enough nodes for the memory usage to become significant, you have way too many to present to even consider presenting to the user.
#mathematician1975 already provided an answer for the main problem. There's another bug in SortMatrix(): you only swap the elements of the first column, therefore after sorting, the counts (in the second column) will not be correct anymore. You'll have to insert
temp=matrix[i][1];
matrix[i][1]=matrix[i+1][1];
matrix[i+1][1]=temp;
to get it working.