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;
}
Akira
  • 4,385
  • 3
  • 24
  • 46
Vincent Zhou
  • 141
  • 2
  • 10
  • 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. – Andre Kampling Sep 04 '17 at 06:52
  • 1
    Welcome to Stack Overflow. Please read the [About] and [Ask] pages sometime. More urgently, please read about how to create an MCVE ([MCVE]). 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. – Jonathan Leffler Sep 04 '17 at 06:53
  • In your `insert ` function, `strcpy(pNode->data, word)` is wrong, because `pNode->data` hasn't been set to point anywhere yet. – Steve Summit Sep 04 '17 at 07:16
  • How do you call `insert`. Please [edit] your question and provide a [mcve]. – Jabberwocky Sep 04 '17 at 09:08

1 Answers1

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;
Ganeshdip
  • 389
  • 2
  • 10