I'm having an issue with Heap Corruption while writing any index of a previously expanded array. I have created an object with protected fields. Two of these fields are pointers holding arrays while a third field is the simple int size of the array. I create the object with size 0 and everytime I want to add something into the arrays, I increase the size, make a new array with 1 more spot, copy the old array into the new array, delete the pointer to the old array, and point it at the new array. Yet when I try to set the value of an index starting with 0 in that array - I always get - "Heap Corruption Detected: CRT detected that the application wrote to memory after end of heap buffer".
Shown below is the basic class structure, the functions which increase the size field of the object and rewrite new arrays with the old values copied into them and unwritten values at the end.
When the size is only 1, the function 'BinarySearchAndInsert' returns 0 into insert, meaning that the setter function should be called with index 0. Even when you create the object with a larger size, it returns this error when writing either array beginning with array '_URLs'. I have tried setting this value directly accessing the fields and it did not work either.
class setOfURLs {
private:
int binarySearchAndInsert(myString& u);
int binarySearchAndRemove(myString& u);
protected:
myString* _URLs;
int* _frequencies;
int _size;
public:
setOfURLs();
setOfURLs(int numOfWords);
myString* get_Words();
int* get_Freq();
int get_size();
void setURL(int index, myString URL);
void setFreq(int index, int freq);
void setSize(int i);
void incrementSize();
void rewriteSet(int old_size, int _size);
void deleteNULL();
void addURL(myString& u);//insert word w into the array _URLs - keep it sorted alphabetically
int countFreq(myString& u); // Count how many times this word appears
void sortFreq(); //sort words array based on frequency
void sortURLs(); //sort words array alphabetically
void display(); //print word followed by a colon followed by a single space and frequency
setOfURLs* removeURLs(myString* URLsToFilterOut, int numURLsToFilterOut); //URLsToFilterOut from _URLs array
~setOfURLs();
};
// default constructor - initializes with no URLs
setOfURLs::setOfURLs()
{
_size = 0;
_URLs = new myString[0];
_frequencies = new int[0];
}
// default destructor
setOfURLs::~setOfURLs()
{
delete _URLs;
delete _frequencies;
}
// non default constructor - initializes with a known number of URLs
setOfURLs::setOfURLs(int numOfWords)
{
_size = numOfWords;
_URLs = new myString[numOfWords];
_frequencies = new int[numOfWords];
}
// getter for the array of URLs of the setOfURLs
myString* setOfURLs::get_Words()
{
return _URLs;
}
// getter for the array of frequencies of the setOfURLs
int* setOfURLs::get_Freq()
{
return _frequencies;
}
// getter for the size of the setOfURLs
int setOfURLs::get_size()
{
return _size;
}
// setter for the array of URLs of the setOfURLs
void setOfURLs::setURL(int index, myString URL)
{
_URLs[index] = URL;
}
// setter for the array of frequencies of the setOfURLs
void setOfURLs::setFreq(int index, int freq)
{
_frequencies[index] = freq;
}
// store the value of the size of the setOfURLs
void setOfURLs::setSize(int i)
{
int old_size = _size;
_size = i;
rewriteSet(old_size, _size);
}
// increments the value of the size of the setOfURLs
void setOfURLs::incrementSize()
{
int old_size = _size;
++_size;
rewriteSet(old_size, _size);
}
// rewrite set after increasing size
void setOfURLs::rewriteSet(int oldSize, int size)
{
myString* new_URLs = new myString[size];
int* new_frequencies = new int[size];
for (int i = 0; i < oldSize; ++i) {
new_URLs[i] = _URLs[i];
new_frequencies[i] = _frequencies[i];
}
delete[] _URLs;
delete[] _frequencies;
_URLs = new_URLs;
_frequencies = new_frequencies;
}
The Error occurs here (I have modified this method to show what should happen when insert = 0)
void setOfURLs::addURL(myString& newWord)
{
myString string = newWord;
int insert = binarySearchAndInsert(newWord);
int size = get_size();
if (insert == 0) {
int newFreq = countFreq(newWord);
setURL(insert, string);// ERROR LINE
setFreq(insert, newFreq + 1);
}
}
int main() {
int numURLsToFilterOut;
char* url;
myString* urlString;
int numPages;
int pageNo;
int numNeighbors;
int neighbor;
// Read a text containing URLs and store the in the setOfURLs instance
setOfURLs* mySetOfURLs = new setOfURLs(0);
url = getNextURL(); //first read the next URL as an array of characters
while (url != NULL)
{
if (stringLength(url) > 1) {
(*mySetOfURLs).incrementSize();
urlString = new myString(url); //create a myString object with the URL just read
(*mySetOfURLs).addURL(*urlString); //add URL to mySetOfURLs
}
url = getNextURL();
}
delete [] URLsToFilterOutList;
delete mySetOfURLs;
delete newSetOfURLs;
delete[] myLinkStructure;
return 0;
}