-1

I am writing this method (C - Language) which should create a new node for a linked list. It crashes at the line just after the first if (SIGSEGV signal)

I am debugging the method so probably there are more errors in the subsequent lines, but for the moment I will appreciate any observations regarding this particular line.

//Create a new tEquivalenceNode
tEquivalenceNode* createNewEquivalenceNode(tWordEquivalence e){
    //create new equivalence node
    tEquivalenceNode* node = NULL;
    node = (tEquivalenceNode*) malloc(sizeof(tEquivalenceNode));
    //allocate memory for both srcWord and dstWord

    tWordInfo* destiny = NULL;
    tWordInfo* source = NULL;
    destiny = (tWordInfo*) malloc(sizeof(tWordInfo));
    source = (tWordInfo*) malloc(sizeof(tWordInfo));

    if((node != NULL)&&(destiny != NULL) && (source != NULL)){
        node->elem->dstWord = destiny; //Crashes at this line 
        node->elem->srcWord = source;
        //copy information to destiny word in the new Node
        node->elem->dstWord->languageID = e.dstWord->languageID;
        node->elem->dstWord->wordID = e.dstWord->wordID;
        //allocate memory for word and copy the string to the node
        node->elem->dstWord->word = (char*) malloc((strlen(e.dstWord->word) + 1)*sizeof(char));
        if(node->elem->dstWord->word != NULL){
            strcpy(node->elem->dstWord->word, e.dstWord->word);
        }

        //repeat the process for source word
        node->elem->srcWord->languageID = e.srcWord->languageID;
        node->elem->srcWord->wordID = e.srcWord->wordID;
        //allocate memory for word and copy the string to the node
        node->elem->srcWord->word = (char*) malloc((strlen(e.srcWord->word) + 1)*sizeof(char));
        if(node->elem->srcWord->word != NULL){
            strcpy(node->elem->srcWord->word, e.srcWord->word);
        }

        node->next = NULL;
    }
    return node;
}

This is the definition of the tWordInfo data type:

/* Dictionary Word */
typedef struct {
    int languageID;
    int wordID;
    char* word;
} tWordInfo;

tEquivalenceNode:

/* Equivalences list element */
typedef struct tEquivalenceNode tEquivalenceNode;
struct tEquivalenceNode{
    /* EX 1.1 */
    tWordEquivalence* elem; 
    tEquivalenceNode* next;
};

and tWordEquivalence:

typedef struct {
    /* Word in the source language */
    tWordInfo* srcWord;

    /* Equivalent word in destination language */
    tWordInfo* dstWord;

} tWordEquivalence;
tomacco
  • 710
  • 4
  • 26

2 Answers2

0
if((node != NULL)&&(destiny != NULL) && (source != NULL)){
    node->elem->dstWord = destiny; //Crashes at this line 

Crashes because node->elem is not initialized. Thus node->elem->dstWord is dereferencing garbage as a pointer.

You probably want to initialize it to a new memory block:

node = (tEquivalenceNode*) malloc(sizeof(tEquivalenceNode));
node->elem = (tWordEquivalence*) malloc(sizeof(tWordEquivalence));
// ...

For the record, it would simplify things (and you wouldn't have had this problem) if you put tWordEquivalence directly into tEquivalenceNode instead of using a pointer:

struct tEquivalenceNode{
    tWordEquivalence elem; 
    tEquivalenceNode* next;
};
Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
0

You've never initialized node->elem.

tEquivalenceNode* node = malloc(sizeof(tEquivalenceNode));

//allocate memory for elem
node->elem = malloc(sizeof(*(node->elem)));
Barmar
  • 741,623
  • 53
  • 500
  • 612