Invalid Read && Write Valgrind Error

Hello, I am getting this error from Valgrind. The programs seems to work fine and all the memory is freed. If I comment out the call to the size function there is no error so that makes me think it is in the size function but I just don't see it I need a fresh pair of eyes. Thank you.

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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212

#include "hashTable.h"

table_node hash_table[TABLE_LENGTH];


//TODO: Error checking
void key_hash_t( ) 
{
	char key = 'a';
	for ( int i = 0; i < TABLE_LENGTH; i++ )
	{
		hash_table[i].letter = (key + i);
	}
}

//TODO: return and error checking
bool load_hash_t(char *file_name)
{
	char word[WORD_LENGTH];
	char c;
	int index = 0, hash_val = 0, size = get_file_size(file_name);
	FILE *file = fopen(file_name, "r");
	
	if (file == NULL)
	{
		printf ("File not found...\n");
		exit(1);
	}

	for (int word_count = 0; word_count < size; word_count++) 
	{
		node *new_node = malloc( sizeof(*new_node) ); 

		// make sure malloc was successful
		if (new_node == NULL)
		{
			printf ("Failed to allocate memory...\n");
			exit(1);
		}

		new_node->next = NULL;
		
		while ((c = fgetc(file)) != '\n')
		{
			word[index] = c;
			index++;
		}

		hash_val = hash(word);         

		for (int j = 0; j < index; j++)
		{
			new_node->word[j] = word[j];
		}

		index = 0;

		// if this bucket has no nodes yet add it here
		if (hash_table[hash_val].next == NULL)
		{
			hash_table[hash_val].next = new_node;
		}
		else
		{
			//add new  node to end of list
			new_node->next = hash_table[hash_val].next;
			hash_table[hash_val].next = new_node;
		}
	}
	fclose(file);
	return true;
}

// TODO: return && error checking
bool unload_hash_t( )
{
	/**
	 * loop through array
	 * traverse each list and
	 * delete each node
	 **/
	for (int i = 0; i < TABLE_LENGTH; i++)
	{
		node *cursor = hash_table[i].next;

		while (cursor != NULL)
		{
			node *temp = cursor;
			cursor = cursor->next;
			free(temp);
		}
	}
	return true;
}

//TODO: error checking
int get_file_size(char *file_name)
{
	char c;
	int line_count = 0;
	FILE *file = fopen(file_name, "r");
	
	if (file == NULL)
	{
		printf("File not found..\n");
		return 1;
	}

	while ((c = fgetc(file)) != EOF)
	{
		if (c == '\n')
		{
			line_count++;
		}
	}
	fclose(file);

	return line_count;
}

//TODO: error checking
int hash(char *str)
{
	if (str == NULL)
	{
		printf("NULL pointer..\n");
		return 1;
	}
	return (tolower(str[0]) - 97);
}

bool check(const char* word)
{
	/**
	 * loop threw array to find bucket
	 * traverse list to find word
	 * return true if found false if not
	 */
	return true;
}

 //TODO: error checking, return 0 if not loaded
unsigned int size( )
{
	node *cursor = malloc(sizeof(*cursor));
	cursor->next = NULL;
	
	int node_count = 0;
	
	for (int i = 0; i < TABLE_LENGTH; i++)
	{
		cursor->next = hash_table[i].next;

		while (cursor->next != NULL)
		{
			cursor = cursor->next;
			node_count++;
		}
	}

	free(cursor);
	
	return node_count;
}
[\code]

Valgrind Output:
[code]
arortell@gentoobox ~/Develoment/Projects/C_Projects/Data_Structures/HashTable $ valgrind --leak-check=full  --read-var-info=yes ./hashTable
==3960== Memcheck, a memory error detector
==3960== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==3960== Using Valgrind-3.9.0 and LibVEX; rerun with -h for copyright info
==3960== Command: ./hashTable
==3960== 
==3960== Invalid read of size 8
==3960==    at 0x400B23: unload_hash_t (hashTable.c:90)
==3960==    by 0x400C89: main (main.c:17)
==3960==  Address 0x51d6570 is 48 bytes inside a block of size 56 free'd
==3960==    at 0x4C2999C: free (vg_replace_malloc.c:468)
==3960==    by 0x400C32: size (hashTable.c:169)
==3960==    by 0x400C7F: main (main.c:16)
==3960== 
==3960== Invalid free() / delete / delete[] / realloc()
==3960==    at 0x4C2999C: free (vg_replace_malloc.c:468)
==3960==    by 0x400B36: unload_hash_t (hashTable.c:91)
==3960==    by 0x400C89: main (main.c:17)
==3960==  Address 0x51d6540 is 0 bytes inside a block of size 56 free'd
==3960==    at 0x4C2999C: free (vg_replace_malloc.c:468)
==3960==    by 0x400C32: size (hashTable.c:169)
==3960==    by 0x400C7F: main (main.c:16)
==3960== 
53i
==3960== 
==3960== HEAP SUMMARY:
==3960==     in use at exit: 56 bytes in 1 blocks
==3960==   total heap usage: 56 allocs, 56 frees, 4,160 bytes allocated
==3960== 
==3960== 56 bytes in 1 blocks are definitely lost in loss record 1 of 1
==3960==    at 0x4C28730: malloc (vg_replace_malloc.c:291)
==3960==    by 0x400B96: size (hashTable.c:146)
==3960==    by 0x400C7F: main (main.c:16)
==3960== 
==3960== LEAK SUMMARY:
==3960==    definitely lost: 56 bytes in 1 blocks
==3960==    indirectly lost: 0 bytes in 0 blocks
==3960==      possibly lost: 0 bytes in 0 blocks
==3960==    still reachable: 0 bytes in 0 blocks
==3960==         suppressed: 0 bytes in 0 blocks
==3960== 
==3960== For counts of detected and suppressed errors, rerun with: -v
==3960== ERROR SUMMARY: 3 errors from 3 contexts (suppressed: 1 from 1) 
I understand from the error unload is 48 bytes inside of the memory freed by size.By freeing cursor in size does that free the hash_table[i].next? Thanks for your time.
Hi could you post your hashTable.h and main() program?
Topic archived. No new replies allowed.