0

This is a part of the header file

using namespace std;

namespace graph {

    typedef string Name;
    typedef bool Male;
    typedef string Relation;
    typedef int Age;

    struct Node;
    struct Vertex;

    typedef Node* Grafo;
    typedef Vertex* AdjacentList;

    const Grafo emptyGraph = NULL;
    const AdjacentList emptyAdjacentList = NULL;
}

Here is the struct I used to implement the data structure linked adjacency list;

struct graph::Node {
    Name nome;
    Male maschio;
    Age anni;
    Node* next;
    Vertex* adjList;
};


struct graph::Vertex {
    Node* vertPtr;
    Relation rel;
    Vertex* next;
};

here is the function that gives me the error it should add the relation to the persons one example of call could be: addRelation(Mark,Lucas,Dad,Son,g)

bool graph::addRelation(Name n, Name m, Relation rel1, Relation rel2, Grafo & g) {
    if (n == m) //I make sure the two persons I wanna bond aren't the same one
        return false;

    Node * first = g; //I initialize a ptr and I iter util it point to the desired person
    while (first != emptyGraph && first -> nome != n)
        first = first -> next;

    if (first == emptyGraph)
        return false;

    Node * second = g; //same with second person
    while (second != emptyGraph && second -> nome != m)
        second = second -> next;

    if (second == emptyGraph) //if one of them is missing we exit
        return false;

    if (first -> adjList != emptyAdjacentList) {
        Vertex * auxFirst = first -> adjList;
        while (auxFirst -> next != emptyAdjacentList && auxFirst -> vertPtr -> nome != m)
            auxFirst = auxFirst -> next;

        if (auxFirst -> vertPtr -> nome == m)
            return false;

        Vertex * secondHalfEdgeToAdd = new Vertex;
        auxFirst -> next = secondHalfEdgeToAdd;
        auxFirst -> rel = rel1;
        auxFirst -> vertPtr = first;
        auxFirst -> next = emptyAdjacentList;
    }
}

I dunno why I still need to add details otherwise I can't post this so I'm just writing this phrase hoping this works;

RandomGuy
  • 15
  • 3
  • Nothing jumps out immediately. Have you tried using GDB? https://stackoverflow.com/questions/2876357/determine-the-line-of-code-that-causes-a-segmentation-fault – teambob Jun 29 '21 at 11:13
  • 1
    Do you have rest of the code? –  Jun 29 '21 at 11:32
  • You might want to create a short, self-contained and compilable example. I tried to run your code, but I didn't want to spend more time on getting it to work. Anyway, what compiler are you using? Try making a debug build. If you use GCC, then use the `-g2 -Og` arguments and run your program in GDB. when it segfaults, you can get a backtrace with `bt` and you know _where_ it crashes. – Benjamin Maurer Jun 29 '21 at 11:37
  • one advice in `C++` use `nullptr` instead of `NULL`. –  Jun 29 '21 at 11:46
  • Thanks for the suggestions I'm not familiar with debugging I tried the gdb command but didn't worked I use g++ btw – RandomGuy Jun 29 '21 at 16:07

1 Answers1

0

Without more code, one thing that pops up to me is this:

auxFirst -> next = secondHalfEdgeToAdd;
auxFirst -> rel = rel1;
auxFirst -> vertPtr = first;
auxFirst -> next = emptyAdjacentList;

auxFirst -> next = secondHalfEdgeToAdd; than you have auxFirst -> next = emptyAdjacentList;. You are overwriting auxFIrst->next pointer.

IDK if you meant to set auxFirst->next->next = NULL.