2

I am trying to dynamically create an array of size 'n' that can contain pointers to multiple adjacency lists. However when I run my code, it doesn't result in an array of nodes, it just results in this:

Netbeans debugging variables

It's not an array, it's just a single node.

Here's my code:

main.cpp

#include "graph.h"
using namespace std;

int main() {
    graph g;
    return 0;
}

graph.cpp

#include "graph.h"

// Constructor
graph::graph()
{
    int size = 5; // 
    // Allocate array of size (n) and fill each with null
    adjListArray = new node*[size];

    // Set each entry in array to NULL
    for (int i = 0; i < size; i++)
    {
        adjListArray[i] = NULL;
    }
}

graph.h

#include<cassert>
#include<iostream>
#include<string>
#include<fstream>
using namespace std;

struct node
{
    int name;
    node *next;
};

class graph
{
    public:
        // Constructors/destructors
        graph();
        ~graph();

    private:
        node** adjListArray; // Array of pointers to each adjacency list
};

Where am I going wrong?

1
  • 1
    Not sure I see any problem. It is perhaps a matter of how your debugger represents the data... and whether you can coerce it to display adjListArray as an array. Keep in mind that the attribute that you're displaying is not declared as an array, but as a node ** Commented Apr 22, 2014 at 3:05

1 Answer 1

1

The issue is that there is no way to tell how large a piece of memory when you only have a pointer to the start of the memory and so your IDE just assumes that it is size is sizeof(datatype) and only shows you the first element.

Sign up to request clarification or add additional context in comments.

1 Comment

Very foolish of me to blindly trust the debugger and not test to see if the values existed myself. Issue solved, thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.