0

Im currently running into an issue where I have to input words (strings) into a binary search tree and am doing so by putting words(strings) into string arrays, however when I try to put it into the first element, it is segmentation faulting.

Here is what I have:

node.h

typedef struct Node{
    char letter;
    int asiccValue;
    struct Node *left, *right;
    string words[99];
    int wordCount;
}Node;

tree.cpp

// This function creates new nodes as needed
Node *createNode(string word){
    // Assigns values
    struct Node *temp = (Node*)malloc(sizeof(Node));
    temp->letter = word[0];
    temp->asiccValue = (int)word[0];
    temp->left = NULL;
    temp->right = NULL;
    temp->words[0] = word;
    temp->wordCount = 1;
    return temp;
}
8
  • @TomKarzes I've adjusted that but getting the same error. Commented Oct 13, 2020 at 3:34
  • if string is actually std::string and this question is for the C++ programming language the answer will be radically different that it will be if string is a typedef and this is a C question. The crux would be under no circumstances should you malloc storage for std::string unless you're already an expert in C++ and would not need to ask this question. mallocing strings requires special handling (search keyterm: placement new) because malloc only provides storage. It does not call constructors, and an unconstructed string is a timebomb.. Commented Oct 13, 2020 at 3:34
  • @AndreasWenzel Fixed Commented Oct 13, 2020 at 3:34
  • @user4581301 have adjusted to be just a c++ question Commented Oct 13, 2020 at 3:34
  • 1
    Turns out vector is unnecessary. struct Node *temp = (Node*)malloc(sizeof(Node)); doesn't know how to construct the 99 strings in the word member of Node (see comment above). You can solve this with struct Node *temp = new Node; new allocates storage for and then constructs a Node. Commented Oct 13, 2020 at 3:46

1 Answer 1

1

malloc doesn't call constructors, meaning your string array isn't initialized. For any non-trivial type, you really want to avoid malloc unless you know what you're doing (see also: placement new).

Using new should fix your problem. Make sure you update existing code to use delete instead of free. Also, consider getting rid of new/delete entirely, and using make_unique and friends.

Node *createNode(string word){
    // Assigns values
    Node * temp = new Node;
    temp->letter = word[0];
    temp->asiccValue = (int)word[0];
    temp->left = NULL;
    temp->right = NULL;
    temp->words[0] = word;
    temp->wordCount = 1;
    return temp;
}
Sign up to request clarification or add additional context in comments.

1 Comment

When using C++ you should avoid malloc as the memory is allocated in potentially different area than used by C++ programs that would normally use new. Mixxing dynamic memory allocation from different mechanism makes for potential issues.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.