This is in VS btw.
I can't delete anything from my wordsarray structure array after it gets modified more than one time. Any ideas why?
DOESN'T Work.
Why can't I delete individual actualselfs?
I get HEAP errors when I do.
Here are memory leaks I am getting.
Since there seems to be Words only partially copied, I switched over to use strcpy_s (teacher says don't have to but I did anyway
) but that did not help.
I can't delete anything from my wordsarray structure array after it gets modified more than one time. Any ideas why?
DOESN'T Work.
Code:
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#include <cstring>
#include <cmath>
#include <iostream>
#include <fstream>
#include "strip.h"
using namespace std;
struct Word
{
int count;
char* actualself;
int again;
//~Word( ) { delete actualself; }
};
void strip(char* sword);
int checkfordigit(char* word);
void addWord(Word** words, int& wordcount, char* wordtoadd);
int CheckForExistance(Word* arr, char* checkword, int wordcount);
int main() {
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
Word* wordarray = 0;
int wordcount = 0;
char* tempword = new char[100];
char* tempword2 = new char[100];
int r = 0;
ifstream infile;
ofstream outfile;
infile.open("2.txt");
outfile.open("log.txt");
if (infile.is_open()) {
cout << "File Was Opened Successfully!" << endl;
while (!infile.eof()) {
if (wordcount == 0) infile >> tempword2;
//strip(&tempword2);
//cout << "read: " << tempword2 << endl;
if (!checkfordigit(tempword2)) {
r = CheckForExistance(wordarray, tempword2, wordcount);
if (!r) {
cout << "found a word" << endl;
wordcount++;
//cout << "word was not found." << endl;
addWord(&wordarray, wordcount, tempword2);
//cout << "added word and count is now: " << wordcount << endl;
//cout << "word count is: " << wordcount << endl;
//cout << endl;
}
}
/*outfile << tempword2 << " word #" << r << endl;*/
if (infile.eof()) cout << "reached end of file. #1" << endl;
infile >> tempword2;
if (infile.eof())
{
cout << "reached end of file. last read word was: " << tempword2 << " count is " << wordcount << endl;
}
};
cout << wordarray[0].actualself << endl;
cout << "Done with " << wordcount << " entries." << endl;
for (int i = 0; i < wordcount; i++) {
outfile << wordarray[i].actualself << endl;
outfile << wordarray[i].count << endl;
}
}
cout << "deleting....";
cout << "wordcount: " << wordcount << endl;
//cout << wordarray[wordcount-1].actualself << endl;
//for (int i = 0; i < wordcount-1; i++) {
//delete [] wordarray[0].actualself;
//}
delete [] tempword;
delete [] tempword2;
delete [] wordarray;
cout << "deleting # " << "END" << endl;
system("pause");
return 0;
}
int CheckForExistance(Word* arr, char* checkword, int wordcount)
{
int result = 0;
for (int i = 0; i < wordcount; i++) {
result = stricmp(arr[i].actualself, checkword);
if (!result)
{
//cout << "checking " << i << " " << arr[i].actualself << " * " << checkword << " * " << result << endl;
//cout << "found " << checkword << endl;
arr[i].count += 1;
return 1;
}
}
//system("pause");
return 0;
}
void addWord(Word** words, int& wordcount, char* wordtoadd)
{
//cout << "adding " << wordtoadd << " wordcount is " << wordcount << endl;
cout << strlen(wordtoadd) +1;
Word* tempwords = new Word[wordcount];
if (wordcount == 1) {
cout << "A " << wordtoadd;
tempwords[0].actualself = new char[ (strlen(wordtoadd)) +1];
strcpy_s(tempwords[0].actualself, strlen(wordtoadd) +1, wordtoadd);
tempwords[0].count = 1;
(*words) = new Word[1];
(*words)[0].actualself = new char[strlen(wordtoadd) + 1];
//(*words) = tempwords;
strcpy_s( (*words)[0].actualself, strlen(tempwords[0].actualself) + 1,tempwords[0].actualself );
(*words)[0].count = tempwords[0].count;
delete [] tempwords[0].actualself;
delete [] tempwords;
}
else
{
for (int i = 0; i < wordcount-1; i++)
{
tempwords[i].actualself = new char[strlen(wordtoadd)+1];
//tempwords[i] = (*words)[i];
strcpy_s(tempwords[i].actualself, strlen((*words)[i].actualself) + 1,(*words)[i].actualself);
tempwords[i].count = (*words)[i].count;
//cout << tempwords[i].actualself << endl;
}
tempwords[wordcount-1].actualself = new char[strlen(wordtoadd) + 1];
strcpy_s(tempwords[wordcount-1].actualself, strlen(wordtoadd)+1, wordtoadd);
tempwords[wordcount-1].count = 1;
for (int i = 0; i < wordcount-1; i++)
{
//delete [] (*words)[i].actualself;
}
delete [] (*words);
(*words) = new Word[wordcount];
for (int i = 0; i < wordcount; i++)
{
(*words)[i].actualself = new char( (strlen (tempwords[i].actualself) ) + 1);
strcpy_s((*words)[i].actualself, strlen(tempwords[i].actualself)+1, tempwords[i].actualself);
(*words)[i].count = tempwords[i].count;
}
/*for (int i = 0; i < wordcount-1; i++)
{
delete [] tempwords[i].actualself;
}*/
delete [] tempwords;
// delete [] tempwords[0].actualself;
//cout << strlen(wordtoadd + 1) << " whoops!" << endl;
//delete tempwords;
for (int i = 0; i < 1 ; i++) {
//delete [] tempwords[i].actualself;
}
}//else
}
Why can't I delete individual actualselfs?
I get HEAP errors when I do.
Here are memory leaks I am getting.
Code:
{181} normal block at 0x00357E28, 1 bytes long.
Data: <B> 42
{180} normal block at 0x00357DE8, 1 bytes long.
Data: <B> 42
{179} normal block at 0x00357C10, 1 bytes long.
Data: <A> 41
{177} normal block at 0x00357D48, 4 bytes long.
Data: <Bob > 42 6F 62 00
{176} normal block at 0x00357D08, 4 bytes long.
Data: <Bobb> 42 6F 62 62
{175} normal block at 0x003578C0, 4 bytes long.
Data: <Alex> 41 6C 65 78
{173} normal block at 0x00357C68, 1 bytes long.
Data: <B> 42
{172} normal block at 0x00357B38, 1 bytes long.
Data: <A> 41
{170} normal block at 0x00357BC8, 6 bytes long.
Data: <Bobby > 42 6F 62 62 79 00
{169} normal block at 0x00357918, 6 bytes long.
Data: <Alex > 41 6C 65 78 00 CD
{167} normal block at 0x00357B80, 5 bytes long.
Data: <Alex > 41 6C 65 78 00
Since there seems to be Words only partially copied, I switched over to use strcpy_s (teacher says don't have to but I did anyway