I am using the following code to create a ‘Key’ to be used to test a hash table (in particular, I am testing the time required to remove items):
void remove_keys()
{
for (int i = 0; i < NUM_ITEMS; i++) {
char temp_key[20];
sprintf((char *)&temp_key, "Key: %d", i);
size_t key_len = strlen(temp_key) + 1;
char *key = malloc(sizeof(char) * (key_len));
sprintf(key, "%s", temp_key); // THIS LINE
htable_item *item = htable_item_search(root, key, key_len);
if (!item) {
printf("Item not found: %s\n", key);
} else {
//printf("Item found: %s - %s\n", key, item->value);
if (!htable_item_delete(root, item)) {
printf("Error while deleting: %s\n", key);
}
}
}
}
In the line I marked with a comment there is a strange behavior. I am using sprintf to copy the content of “temp_key” to “key”. Before that, I used strncpy to copy the content of “temp_key” to “key” but the result I got from this operation was this (printed from XCode’s debugger):
Printing description of key:
(char *) key = 0x0000000100103ed0 "Key: 10\xb0\xe7\x03\x01\x10"
while “temp_key” produces the following output:
Printing description of temp_key:
(char [20]) temp_key = "Key: 10" {
[0] = 'K'
[1] = 'e'
[2] = 'y'
[3] = ':'
[4] = ' '
[5] = '1'
[6] = '0'
[7] = '\0'
[8] = '\0'
[9] = '\0'
[10] = '\0'
[11] = '\0'
[12] = '\0'
[13] = '\0'
[14] = '\0'
[15] = '\0'
[16] = '\0'
[17] = '\0'
[18] = '\0'
[19] = '\0'
}
the hashtable uses memcmp to compare the keys in the htable_item_search function. But using strncpy there are some items (like “Key: 10”) which are not found while using sprintf it works perfectly. So why this difference?
From http://www.cplusplus.com/reference/clibrary/cstring/strncpy/
No null-character is implicitly appended at the end of destination if source is longer than num (thus, in this case, destination may not be a null terminated C string).
strncpy doesnt add a null terminator to your string, so you’ll have garbage at the end of your string while using this function.