problem with deleting allocated memory

closed account (EAk1vCM9)
Hey guys so my program is basically done all im trying to do is delete allocated memory. It lets me delete it for one of them but I get an error for the second one. One of my peers basically has the same thing as me but for him it works but for me it doesnt. So why doesnt it work fro me and how do I fix it?
Ps. the one that gives me the error is delete [] wordFile[i];

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
  const int SIZE=23907;

int main() {


	ifstream infile,infile2;
	char * wordFile[SIZE];
	char temp[30];


	infile.open("unsorted_words.txt");

	if(!infile)
	{
		cout<<"error opening file" <<endl;
	}
//	else
//	{
	    //char * wordFile[SIZE];
		//char temp[30];
		for(int i=0; i <SIZE; i++)

		{
			infile >> temp;
			wordFile[i]= new char[strlen(temp)+1]; // dynamically allocates memory

			strcpy(wordFile[i],temp);
		}
		for( int i =0; i <SIZE; i++)
		{
			for(unsigned int j=0; j < strlen(wordFile[i]); j++)
			{
			wordFile[i][j]= tolower(wordFile[i][j]); // converts all words to lower case
			}
		}
		infile.close();
		bool swichWord;
	    do
		{
			 swichWord=false;
			for(int i=0; i< SIZE; i++)
			{
				if(strcmp(wordFile[i],wordFile[i+1]) ==1)
				{
					swap(wordFile[i],wordFile[i+1]); // sorts dictionary
					swichWord =true;
				}
		   }
		}while(swichWord);
	    int count=0;
	    char* buffer = new char[256];
	    char* keyword;

	    while(infile2)
	    {
	    	if(infile2.eof())
	    	{
	    		break;
	    	}
	    	count++;
	    	infile2.getline(buffer,256); // gets line to buffer
	    	int length;
	    	length = strlen(buffer);
	    	for(int i =0; i <length; i++)
	    	{
	    		if(buffer[i] == ' ' || buffer[i] =='.'|| buffer[i]==',' || buffer[i]=='-') // parses file
	    		{
	    			buffer[i] = '\0';
	    		}
	    		else
	    		{
	    		buffer[i]= tolower(buffer[i]);
	    		}
	    	}
	    	int place =0;
	    	while(place < length)
	    	{
	    		keyword =buffer + place;
	    		if(keyword[0]!=0)
	    		{ // searching wordfile to spell check gettysburg
	    			if(search(keyword,wordFile,SIZE) == false && keyword[strlen(keyword)] =='s')
	    			{
	    				keyword[strlen(keyword)-1] ='\0';
	    			}
	    			if(search(keyword,wordFile,SIZE)== false && keyword[strlen(keyword)] =='\'' && keyword[strlen(keyword)] =='s')
	    			{
	    				keyword[strlen(keyword)-2] = '\0';
	    			}
	    			if(search(keyword,wordFile,SIZE) == false)
	    			{
	    				cout <<"Misspelled word, " << keyword <<" on line: " <<count <<endl;
	    			}
	    		}
	    		place = place + strlen(keyword)+1;
	    	}
	    }
	    infile2.close();
	    delete [] buffer;
	    delete [] wordFile[i];

	//}

	return 0;
}
something seems off in logic... 'i' is strlen of something, and wordfile [i] is... what exactly?
Should just be something like:

for(z =0; z < size; z++)
delete[] wordfile[z];

which is the reverse of how you allocated it.
Last edited on
Plenty of things seem off logic.

For example infile2

line 6: ifstream infile2
line 54: while(infile2)
line 56: if(infile2.eof())
line 61: infile2.getline(buffer,256);
line 97: infile2.close();

Nowhere in the program is the file ever actually opened.


It is frustrating to attempt to answer questions on this topic. I've made a couple of previous responses but see the same problems already fixed re-appearing in the program.

Last edited on
Topic archived. No new replies allowed.