1

I have been having a lot of trouble with this and running it through a debugger but I still can't figure out the problem. There is a segmentation fault caused by trying to access a member of an array of pointers, which should be initialized to zero. The line that is causing the issue is below. Any guidance on why this error is occurring would be much appreciated.

class BNode
{
public:
    const int m = 6;
    BNode();
    ~BNode();
    int keyCount;
    BNode **pointers;
    int *keys;
    void split(int index, BNode *child);
    void _insert(int value);
};

BNode::BNode()
{
    pointers = new BNode*[m];
    cout <<pointers[0]->keyCount; //THIS IS THE TROUBLE LINE**************
    keys = new int[m - 1];
    keyCount = 0;
}
1
  • I should also say that the line is in there just for testing purposes, I realize it is pointless to have it in there Commented Apr 14, 2016 at 4:41

1 Answer 1

2

Your constructor allocated an array for six pointers.

You then accessed the object referenced by the first pointer.

The problem is that you have not allocated any objects. Your six pointers are uninitialized.

new BNode*[6]; // ...essentially

This allocates six pointers. This does not allocate six objects. Two completely different things.

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

Comments

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.