Question: Finding memory leaks in C++. We are counting the words in a text file using dynamic memory allocation. I am pretty certain I have deleted
Finding memory leaks in C++. We are counting the words in a text file using dynamic memory allocation. I am pretty certain I have deleted all the variables I allocated but I'm getting a lot of memory leaks so I'm missing some or doing it in the wrong spots. It's a new concept for me and I'm having trouble figuring out how to find them. Does anyone spot what the issue is possibly? If it's ran in the debugger there are 13 memory leaks. Any insights would be appreciated!
** thanks for the comment - I am using visual studio for my compiler. I'm sure my issue is that I deleted an array in the wrong spot. I've tried moving things around but can't make any progress so far.
#define _CRTDBG_MAP_ALLOC #define _CRT_SECURE_NO_WARNINGS #include #include #include #include
using std::cin; using std::cout; using std::endl; using std::ifstream; using std::ofstream; using std::setw; using std::right; using std::left;
const char DATA_IN[] = { "data.txt" }; const char DATA_OUT[] = { "report.txt" };
void readWords(int& total, char**& words, int*& count); void writeFile(int& total, char** words, int* count); void outputFile(int& total, char** words, int* count); void countWords(int& total, char**& words, int* count, char* buffer); int findWord(char** words, int* count, int total, char* buffer);
int main() { _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); ifstream input (DATA_IN); ofstream output (DATA_OUT);
int total = 0; char** words = nullptr; int* count = nullptr;
if (input.is_open()) { // load the file readWords(total, words, count);
// write to the file writeFile(total, words, count);
// output to the screen outputFile(total, words, count); } else cout << "Error opening file!";
// free allocated memory for (int i = 0; i < total; i++) { delete[] words[i]; } delete[] words; input.close(); return 0; }
// read words from text file void readWords(int &total, char**& words, int*& count) { ifstream input(DATA_IN);
total = 0; char buffer[256];
input >> buffer; // priming read while (!input.eof()) { int index = findWord(words, count, total, buffer);
// if found if (index >= 0) { count[index]++; } else { char** temp_word = new char* [total + 1]; // copy initial words into dynamic array size_t len = strlen(buffer) + 1; // allocating space in memory for our word temp_word[total] = new char[len]; strcpy_s(temp_word[total], len, buffer); // copy buffer to words
int* temp_count = new int[total + 1]; temp_count = new int[total + 1]; temp_count[total] = 1;
for (int i = 0; i < total; i++) { temp_count[i] = count[i]; temp_word[i] = words[i]; }
delete[] count; count = temp_count; delete[] words; words = temp_word; countWords(total, words, count, buffer); // call process function ++total; } input >> buffer; }
}
int findWord(char** words, int* count, int total, char* buffer) { int found_word = -1; // total number of words in input file
for (int i = 0; i < total && found_word == -1; i++) { if (_stricmp(words[i], buffer) == 0) { found_word = i; } } return found_word; } // count how many times each word is found in the text file void countWords(int& total, char**& words, int* count, char* buffer) { bool found = false; int found_word = 0; findWord(words, count, total, buffer); // if word is already in array if (found) { // allocate new array of temp_word pointers one larger than the current number of words char** temp_word = new char* [total + 1]; int* temp_count = new int[total + 1];
int counter = 0; // make new array point to any words located before the one to delete while (counter < total) { temp_word[counter] = words[counter + 1]; temp_count[counter] = count[counter + 1]; ++counter; } // deallocate desired word delete[] words[found_word]; // make new array point to any words located after the one deleted for (int i = 0; i < total; i++) { temp_word[i] = words[i]; } // deallocate words array and assign words pointer to new temp array delete[] temp_word; delete[] temp_count; words = temp_word; count = temp_count;
} }
//write list of words to file void writeFile(int& total, char** words, int* count) { ofstream output (DATA_OUT);
if (output.is_open()) { // for each entry, write to the screen // currently for each match it is creating a null value? for (int i = 0; i < total; i++) { output << words[i] << endl; } } else { cout << "cannot open file!"; } output.close();
}
// output word frequency occurence to screen void outputFile(int& total, char** words, int* count) { cout << "Word Frequency Analysis" << endl; cout << "Word\t" << right << setw(13) << "Frequency" << endl; // for each entry, print out words and word frequency for (int i = 0; i < total; i++) { cout << left << setw(10) << words[i] << right << setw(3) << count[i] << endl; }
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
