0

My speller programs compile and run with no issues however valgrind doesn't seem to think so. I initially used malloc and manually initialised each pointer to null and the boolean to false. I have since switched to calloc and i'm still getting the same errors. This is valgrind's output:

==16497== Memcheck, a memory error detector

==16497== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.

==16497== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info

==16497== Command: ./speller /home/cs50/pset5/texts/quote.txt

==16497== Parent PID: 3464

==16497==

==16497== Invalid read of size 4

==16497== at 0x80490AC: load (dictionary.c:78)

==16497== by 0x8048725: main (speller.c:45)

==16497== Address 0x71f4fe8 is not stack'd, malloc'd or (recently) free'd

==16497==

==16497== Invalid write of size 4

==16497== at 0x80490E4: load (dictionary.c:80)

==16497== by 0x8048725: main (speller.c:45)

==16497== Address 0x71f4fe8 is not stack'd, malloc'd or (recently) free'd

==16497==

==16497== Invalid read of size 4

==16497== at 0x80490EE: load (dictionary.c:83)

==16497== by 0x8048725: main (speller.c:45)

==16497== Address 0x71f4fe8 is not stack'd, malloc'd or (recently) free'd

==16497==

==16497==

==16497== HEAP SUMMARY:

==16497== in use at exit: 576 bytes in 3 blocks

==16497== total heap usage: 367,047 allocs, 367,044 frees, 41,109,744 bytes allocated

==16497==

==16497== 224 (112 direct, 112 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 3

==16497== at 0x402C109: calloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)

==16497== by 0x80490DD: load (dictionary.c:80)

==16497== by 0x8048725: main (speller.c:45)

==16497==

==16497== LEAK SUMMARY:

==16497== definitely lost: 112 bytes in 1 blocks

==16497== indirectly lost: 112 bytes in 1 blocks

==16497== possibly lost: 0 bytes in 0 blocks

==16497== still reachable: 352 bytes in 1 blocks

==16497== suppressed: 0 bytes in 0 blocks

==16497== Reachable blocks (those to which a pointer was found) are not shown.

==16497== To see them, rerun with: --leak-check=full --show-leak-kinds=all

==16497==

==16497== For counts of detected and suppressed errors, rerun with: -v

==16497== ERROR SUMMARY: 4 errors from 4 contexts (suppressed: 0 from 0)

Here's my code:

root = calloc(1, sizeof(node)); node* current = root; for (int c = fgetc(dp); c != EOF; c = fgetc(dp)) { if (c != '\n') { int index = c - '\''; // This is line 78 (the first valgrind invalid read): if (current->children[index] == NULL) { // line 80 (invalid write) current->children[index] = calloc(1, sizeof(node)); } // 83 (invalid read) current = current->children[index]; } else { current->is_word = true; count++; current = root; } } if(!ferror(dp)) { loaded = true; return true; } else { loaded = false; return false; } fclose(dp); 

1 Answer 1

0

I would say that your problem has to do with the value of index, not calloc or malloc. Look at how you are setting index:

int index = c - '\''; 

The result for any lower case letter will be index = c - 39, or 58 through 83. My guess is that your array runs from 0 to 26, so you would be trying to access invalid elements in the array in the node. Did you remove a block of code that tests for apostrophes by accident?

If this answers your question, please click the check mark to accept. Let's keep up on forum maintenance. ;-)

1
  • Oh I was looking at an ascii table and thought character 96 was an apostrophe. Commented Dec 21, 2015 at 12:36

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.