==3139== Conditional jump or move depends on uninitialised value(s)
==3139== at 0x4A0673F: strcpy (mc_replace_strmem.c:311)
==3139== by 0x400ADB: htable_insert (hashtable.c:56)
==3139== by 0x400F25: main (mylib.c:11)
Hi everybody, I’m still trying to insert into a hash table. I can’t quite get it to work, i’ve included my print method, just because i thought it might be a problem. I’m trying to do linear probing. When I ran valgrind, I got this error, I think it has something to do with copying into my string, but I’m not really sure what is means? I really don’t at this point know how to get this insertion working, some input would be wonderful..
Line 56 in hashtable insert is strcpy(str, key)
int htable_insert(htable h, char *str) {
int i;
/*convert string to integer*/
unsigned int index = htable_word_to_int(str);
/*calculate index to insert into hash table*/
int remainder = index%h->capacity;
/*once calculated position in the hash table, 3 possibilities occur*/
/*no string in this positon, copy string to that position, increment number of keys, return 1*/
if (h->key[remainder] == NULL) {
char *key = emalloc(strlen(str) + 1);
strcpy(str, key);
h->key[remainder] = key;
h->frequencies[remainder] = 1;
h->num_keys++;
return 1;
}
/*the exact same string is at the position, increment frequency at that position, return frequency*/
if (strcmp(str, h->key[remainder]) == 0) {
h->frequencies[remainder]++;
return h->frequencies[remainder];
}/*a string is at that position, but it isnt the rightone, keep moving along the array
until you find either an open space or the string you are looking for*/
if (h->key[remainder] != NULL && strcmp(str, h->key[remainder]) != 0) {
/*you may need to wrap back around to the beginning of the table, so each time you add
to the position you should also mod by the table capacity.*/
for (i = 0; i <= h->capacity; i++) {
/*no string in this positon, copy string to that position, increment number of keys*/
if (h->key[remainder] == NULL) {
char *key = emalloc(strlen(str) + 1);
strcpy(str, key);
h->key[remainder] = key;
h->frequencies[remainder] = 1;
h->num_keys++;
}
/*if you find the string you were looking for, increment the frequecny at the position
and return the frequency*/
if (strcmp(str, h->key[remainder]) == 0) {
h->frequencies[remainder]++;
return h->frequencies[remainder];
}
if (h->key[remainder] != NULL && h->capacity == i) {
i = 0;
}
}
}
/*if you have kept looking for an open space but there isnt one, the hash table must fu*/
return 0;
}
void htable_print(htable h, FILE *stream) {
int i;
for(i = 0; i < h->capacity; i++) {
if(h->key[i] != NULL) {
fprintf(stream, "%d%s\n", h->frequencies[i], h->key[i]);
}
}
}
Your strcpy’s should be strcpy(key, str) and not the other way around. (You can just use strdup, btw, and save the malloc+strcpy).
Also, in:
if (h->key[remainder] != NULL && strcmp(str, h->key[remainder]) != 0) {
The condition “h->key[remainder] != NULL” is redundant: you’ve already checked that above.
Finally, in your loop (going over the buckets), it seems like:
.. Finally finally — the initial part, the one that’s outside the loop, could be in the loop, saving code.