0

I have implemented an undirected graph using LinkedList. My problem is that I get read acces violation (p was 0xCDCDCDCDCD) for every LinkedList function.

class LinkedList {

public:

    Node *start;
    LinkedList() { start = NULL; } //default constructor

    //---------------------------------

    ~LinkedList() //destructor

    {

        Node *temp;
        while (start)

        {

            temp = start->next;
            delete start;
            start = temp;

        }

    }
    //----------------------------------
    //get the address of first node of the linked list

    Node* getStart() { return start; }

    //---------------------------------
    //insert an element into linked list

    void insert(int data)

    {

        Node *ptr = new Node(data);
        if (start == NULL)

        {

            start = ptr;

        }

        else

        {

            Node *temp = start;

            while (temp->next)

                temp = temp->next;

            temp->next = ptr;

        }

    }

    //---------------------------------
    bool findNode(int data) {
        Node *p = start;
        while (p)

        {

            if (p->info == data)
                return true;

            p = p->next;

        }
        return false;
    }


};

Then i have implemented Graph class like that:

class Graph {

private:
    LinkedList *A;
    int vertex;
    int edges;

public:
    Graph(int vertex) {
        this->vertex = vertex;
        edges = 0;
        A = (LinkedList *)malloc(sizeof(LinkedList)*vertex);
    }
    void insertEdge(int u, int v) {
        if (A[u].findNode(v) == false) {
            A[u].insert(v);
            A[v].insert(u);
            edges++;
        }
    }
};

And the Node:

class Node {
    friend class LinkedList;
    int info;
    Node *next;
    Node() //default constructor
    {
        info = 0;
        next = 0;
    }
    Node(int item) //parameterized constructor

    { 
        info = item;
        next = 0; 
    }

};

Main:

int main()

{
    Graph G(7);
    G.insertEdge(1, 3);
    G.insertEdge(4, 6);
    G.displayGraph();
    return 0;
}

I'm not exactly sure what is going on but, every time I try to debug it it'll send me to the function for pointer.

Energizer7
  • 65
  • 1
  • 9
  • `0xCDCDCDCDCD` = Uninitialized heap memory. https://stackoverflow.com/questions/127386/in-visual-studio-c-what-are-the-memory-allocation-representations – drescherjm May 15 '19 at 22:53
  • 7
    In the `Graph()` constructor, `A = (LinkedList *)malloc(sizeof(LinkedList)*vertex);` needs to be `A = new LinkedList[vertex];` instead. `malloc()` does not call constructors, `new` does. And don't forget to `delete[] A` in `~Graph()`. Though, you really should be using `std::vector` and not use `new[]` at all: `vector A; ... A.resize(vertex);` Then you don't have to worry about `delete[]`, and you can still use `A[u]` to access individual lists. – Remy Lebeau May 15 '19 at 22:56
  • Wikipedia keeps a good [list of magic programming values](https://en.wikipedia.org/wiki/Magic_number_(programming)#Magic_debug_values). When you get an insane, but highly repetitious number like 0xCDCDCDCDCD, look it up. There will probably be offsets from an origin and other little details that make the least significant end look a little fuzzy, but you can usually get a lot of helpful information. – user4581301 May 15 '19 at 23:02
  • 1
    Addendum to @RemyLebeau 's comment: Make sure your `LinkedList` properly observes the [Rule of Three or the Rule of Five](https://en.cppreference.com/w/cpp/language/rule_of_three) before trying to place it in a `std::vector`. Worth doing this anyway because It's quite a nasty surprise if you accidentally copy a non-observing object. – user4581301 May 15 '19 at 23:05
  • 3
    @user4581301 Good point. Though a better option is to simply get rid of `LinkedList` and use `std::list` instead. – Remy Lebeau May 15 '19 at 23:06
  • 1
    +1 for `std::vector`. And while you are at it, replace your buggy `LinkedList` by `std::list` as well. In fact, list might not even be a best structure for this job, as std::vector will likely perform better despite possible reallocations (cache locality will most likely beat the list's pointer chasing). – EmDroid May 15 '19 at 23:07
  • (so to clarify, vector of vectors of ints would be my choice, no list at all ;-] ) – EmDroid May 15 '19 at 23:11
  • @axalis come on, man. you know he's doing this for a school assignment. they are supposed to use those data structures to learn programming principles. This is equivalent to a kid asking for help on a book report, and you laughing at them and saying "Look at all these book reports already written about this book! Why you trying to re-write it, silly kid!!1" Just stop. – MPops May 15 '19 at 23:23
  • @MPops that's probably the correct read, but there's no reason not to bring up `std::list` or a better-suited library container on the off chance the asker is too unfamiliar with the language to know the option is available. – user4581301 May 15 '19 at 23:36
  • 2
    If they're teaching this instead of the use of std::vector and std::list, they're doing it wrong. – Mirko May 16 '19 at 00:45
  • @RemyLebeau Ty, your code works :) – Energizer7 May 16 '19 at 08:51
  • 1
    @Mirko They are implementing an undirected graph for a school project. They must be making assignments wrong too, because these have been implemented before. What a waste of time. – MPops May 16 '19 at 21:37

0 Answers0