2

What should I do to allocate memory space for pNode->data, I want to put a single character into it, like pNode->data = "c". But it shows segmentation fault and the memory address for pNode->data is 0x1 which is out of bound.

Below is my code.

typedef struct node {
    char* data;
    int weight;
    bool end_of_key;
    struct node* left;
    struct node* equal;
    struct node* right;
} node_t;

typedef struct listnode{
    char* data;
    int weight;
    struct listnode* next;
} listnode_t;


node_t* insert(node_t* pNode, char* word, int weight) {
    if(pNode == NULL) {
        /**
         * Create a new pNode, and save a character from word
         */
        pNode = (node_t*) malloc(sizeof(*pNode));

        pNode->left = NULL;
        pNode->equal = NULL;
        pNode->right = NULL;
        strcpy(pNode->data, word);

    }

    if(*word < *(pNode->data)) {
        /**
        * Insert the character on the left branch
        */
        pNode->left = insert(pNode->left, word, weight);
    }
    else if(*word == *(pNode->data)) {
        if(*(word+1) == '\0') {
            /**
            *set pNode end_of_key_flag to true and assign weight
            */
            pNode->end_of_key = true;
            pNode->weight = weight;
        }
        else {
            /**
            * If the word contains more characters, try to insert them
            * under the equal branch
            */
            pNode->equal = insert(pNode->equal, word+1, weight);
        }
    }
    else {
        /**
        * If current char in word is greater than char in pData
        * Insert the character on the right branch
        */
        pNode->right = insert(pNode->right, word, weight);
    }

    return pNode;
}
4
  • 1
    If it should be just a single character: 'c' not "c" as you said (contains null terminator) then change char* data; to char data; and char* word, to char word, and do just an assignment: pNode->data = word; (no allocation needed for that). But if you want to use a string you can either allocate memory for that or use a buffer with a fixed size. Commented Sep 4, 2017 at 6:52
  • 1
    Welcome to Stack Overflow. Please read the About and How to Ask pages sometime. More urgently, please read about how to create an MCVE (minimal reproducible example). Note that your code extract defines struct listnode but AFAICS the code doesn't use it, which means it isn't an MCVE (it isn't minimal). You should seriously consider the merits of replacing char *data with char data[2] if you only want single-character strings in it, or char data if you really want single characters. If you need multi-character strings, consider strdup(). Be careful in the code that frees the data structures. Commented Sep 4, 2017 at 6:53
  • In your insert function, strcpy(pNode->data, word) is wrong, because pNode->data hasn't been set to point anywhere yet. Commented Sep 4, 2017 at 7:16
  • How do you call insert. Please edit your question and provide a minimal reproducible example. Commented Sep 4, 2017 at 9:08

1 Answer 1

3

From declaration of node, I can see that for data you are not assigning memory, you are just creating a pointer to character type, you can change the definition of node as follows(and code change is needed for the same)-

typedef struct node {
char data;
int weight;
bool  end_of_key;
struct node * left;
struct node * equal;
struct node * right;} node_t;
Sign up to request clarification or add additional context in comments.

Comments

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.