I’m losing 1 byte of memory when using Valgrind, and I’m not sure why… Here, I’m assigning a string to a malloc. I malloc memory for decode_hex inside of dctinsert and I malloc memory for string in the outside of dctinsert. For some reason, it says I am not freeing string with the following code. Any ideas why??
if (strcmp(argv[1], "-x") == 0){
output = fopen(argv[3], "w");
dct = dctcreate();
decode(dct, input, output);
index = 0;
decode_hex[0] = (index & 0x00FF); // Holds lower 8 bits.
decode_hex[1] = ((index >> 8) & 0x000F); // Holds upper 4 bits.
decode_hex[2] = '';
while ((value = (char *)dctget(dct, decode_hex)) != NULL) {
printf("Freeing value at decode_hex: %02x%02x, index: %d, value: %pn", decode_hex[0], decode_hex[1], index, value);
free(value);
index++;
decode_hex[0] = (index & 0x00FF); // Holds lower 8 bits.
decode_hex[1] = ((index >> 8) & 0x000F); // Holds upper 4 bits.
decode_hex[2] = '';
printf("Updated decode_hex to: %02x%02xn", decode_hex[0], decode_hex[1]);
}
// Initialize dictionary with ASCII value characters ranging from 0 - 127.
for (val_i = 0; val_i <= 127; val_i++) {
decode_hex[0] = (val_i & 0x00FF); // Holds lower 8 bits.
decode_hex[1] = ((val_i >> 8) & 0x000F); // Holds upper 4 bits.
decode_hex[2] = ''; // Ends with NULL character.
asci_c[0] = (char)val_i; // Turns i to char.
asci_c[1] = ''; // Ends i with NULL character.
string = (char *)malloc((strlen(asci_c) + 1)*sizeof(char)); // Mallocing memory to store string.
LINE 109 strcpy(string, asci_c); // copying asci_c to string.
dctinsert(dict, decode_hex, string); // decode_hex is hex value of the string in value argument.
}
==31530==
==31530== HEAP SUMMARY:
==31530== in use at exit: 1 bytes in 1 blocks
==31530== total heap usage: 1,430 allocs, 1,430 frees, 23,244 bytes allocated
==31530==
==31530== 1 bytes in 1 blocks are definitely lost in loss record 1 of 1
==31530== at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==31530== by 0x400CD0: decode (lzw.c:109)
==31530== by 0x400AFE: main (lzw.c:52)
==31530==
==31530== LEAK SUMMARY:
==31530== definitely lost: 1 bytes in 1 blocks
==31530== indirectly lost: 0 bytes in 0 blocks
==31530== possibly lost: 0 bytes in 0 blocks
==31530== still reachable: 0 bytes in 0 blocks
==31530== suppressed: 0 bytes in 0 blocks
==31530==
==31530== For lists of detected and suppressed errors, rerun with: -s
==31530== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
I checked to see that all indexes from 0-127 were freed. Hence, the print statements. I’ve been stepping through the code through GDB.