Nikolas
Nikolas

Reputation: 161

Seg Fault:I can't understand why

I get a pretty weird segmentation fault error when I am trying to use the same function in two different places.

printTVNode function work fine on main. On Main:

printTVNode(headTVNode); /* Works fine here */

TVNodePointer headTopic = NULL;
TopicEmmissions(&headTopic,headTVNode,desiredTopic);

When I am trying to use printTVNode inside TopicEmmissions function a get Seg Fault.

void TopicEmmissions(TVNodePointer * headTopic,TVNodePointer headTVNode,char * desiredTopic){
    TVNodePointer currentTVNode = headTVNode;
    EmmissionPointer currentEmmission;
    EventPointer currentEvent;
    EventPointer topicSubjects = NULL;
    int flag,countEvent = 1,countEmmission = 1;

    printTVNode(headTVNode); /* Get Segmentation Fault here*/
    ...

printTVNode function:

void printTVNode(TVNodePointer headTVNode){
    TVNodePointer currentTVNode = headTVNode;

    while ( currentTVNode != NULL ){
        printEmmission(*(currentTVNode->anEmmission));

        currentTVNode = currentTVNode->next;
    }
}

Upvotes: 2

Views: 58

Answers (1)

Corb3nik
Corb3nik

Reputation: 1177

The problem seems to be in the following line :

printEmmission(*(currentTVNode->anEmmission));

In a situation where anEmmission is NULL, when you try to dereference it, I think you will get a segfault.

Make sure to check that anEmmission is not NULL before doing dereferencing.

Upvotes: 2

Related Questions