-2

I am developing a software that use BST (Binary Search Tree), but i don't understand what's going on:

while (1)
{
    word = (char *)malloc(sizeof(char) * wordLength);

    readReturn = scanf("%s", word);
    if (readReturn == 0)
        return 0;
    BSTNode new = newBSTNode(word);
    if (strcmp(word, "END") == 0)
        break;
    TreeInsert(Tree, new);
}

In this way the program it's working and give the input "aaa" "bbb", the tree has the right value. But if i declare the variable outside the program stop working and the output is: "bbb" "bbb".

     word = (char *)malloc(sizeof(char) * wordLength);    
     while (1)
     {
        readReturn = scanf("%s", word);

        if (readReturn == 0)
            return 0;
        BSTNode new = newBSTNode(word);
        if (strcmp(word, "END") == 0)
            break;
        TreeInsert(Tree, new);
     }

Please, explain me why this is happening!

  • 2
    Your question title is completely different than your question. Please read [how to ask](https://stackoverflow.com/help/how-to-ask) and improve your question. – JHBonarius Aug 31 '22 at 09:12
  • 2
    Basically, you're re-using the same area of memory for all the nodes in the second case. – bereal Aug 31 '22 at 09:14
  • 2
    My ***guess*** is that `newBSTNode` copies the *pointer* and not the string contents. – Some programmer dude Aug 31 '22 at 09:15
  • 1
    You leave a dangling pointer and a memory leak in the "it works" case. Also `if (readReturn == 0)` should be `if (readReturn != 1)` – Weather Vane Aug 31 '22 at 09:15
  • 1
    On a different note, you should probably check for `"END"` before you create the node. And remember to `free` the memory you allocate. – Some programmer dude Aug 31 '22 at 09:16
  • 1
    [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) – Karl Knechtel Aug 31 '22 at 09:20
  • 1
    In response to title: If you want to store different words, then each word needs its own place in memory... Yes, you have to malloc for each word (or use your own "object pool" that you maintain...) BTW: I don't see any check that the entered "word" does not overflow the allocated buffer... Hmmm..... – Fe2O3 Aug 31 '22 at 10:31

1 Answers1

1

In the second code snippet

 word = (char *)malloc(sizeof(char) * wordLength);    
 while (1)
 {
    readReturn = scanf("%s", word);

    if (readReturn == 0)
        return 0;
    BSTNode new = newBSTNode(word);
    if (strcmp(word, "END") == 0)
        break;
    TreeInsert(Tree, new);
 }

all strings are stored in the same dynamically allocated extent of memory that was allocated once before the while loop.

 word = (char *)malloc(sizeof(char) * wordLength);    

So all nodes refer to it and what was stored last in this extent is outputted for all nodes.

Pay attention to that in the first code snippet

while (1)
{
    word = (char *)malloc(sizeof(char) * wordLength);

    readReturn = scanf("%s", word);
    if (readReturn == 0)
        return 0;
    BSTNode new = newBSTNode(word);
    if (strcmp(word, "END") == 0)
        break;
    TreeInsert(Tree, new);
}

there can be a memory leak if after the loop there is no statement

free( word );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335