Problem with pointers

Hello everyone! I have a structure:

1
2
3
4
5
6
7
8
9
typedef struct Node_ 
{
	struct Node_ *left;
	struct Node_ *right;
	struct Node_ *parent;
	bool color;
	int data;
}
Node;


And I can't use this code (it says that program generated erors and stops working):

1
2
3
4
5
Node *root;
root->parent = NULL;
root->left = NULL;
root->right = NULL;
root->data = 0;


Maybe someone can say me what is wrong and how can I set Node to NULL?
Thanks in advance!
root is not initialized.

You could only use something like that if root was an object, rather than a pointer to an object, but since that's not the case, you have to call the constructor yourself:
Node *root=new Node;

By the way, all those initializations of parent, left, etc. should be in the constructor.
Can I write a constructor for struct?
Last edited on
Yes.
Now I have same problem with this code:

1
2
3
4
5
6
7
8
9
10
11
Node *current = new Node;
current = current->right;
if (current == NULL)
{
                current->color = true;
                current->left = NULL;
                current->right = NULL;
                current->parent = p;
                if (p->data < data) p->left = current;
                else p->right = current;
}
Did you initialize current->right in Node's constructor?
If you didn't, then the value of current->right is unknown. It may not even be NULL or 0.
It must be NULL. Because everytime I enter new Node I set ->left and ->right to NULL.
This is all code so far:

1
2
3
4
root->parent = NULL;
root->left = NULL;
root->right = NULL;
root->data = 0;


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
if (root->right == NULL)
    {
        Node *x = new Node;
        x->data = data;
        x->left = NULL;
        x->right = NULL;
        x->parent = NULL;
        x->color = false;
        root->right = x;
    }
    else
    {
        Node *current = new Node;
        Node *p = new Node;
        current = root->right;
        for (;;)
        {
            p = current;
            if (current->data < data) current = current->left;
            else current = current->right;
            if (current == NULL)
            {
                current->color = true;
                current->left = NULL;
                current->right = NULL;
                current->parent = p;
                if (p->data < data) p->left = current;
                else p->right = current;
                if (!current->parent->color) break;
                else break;
            }
        }
    }
Last edited on
In that case, if you know current->right, and later current, is NULL, and therefore doesn't point to a valid object, why are you trying to set NULL->color to true?

Also, that code has a memory leak. Once you set current to current->right, the object you created can no longer be freed.

EDIT: It seems to me like you're making quite a mess with pointers.
Last edited on
So how can I fix it?
You'll have to rework quite a bit of that code.
First, it looks like you're actually not using a constructor. You're just setting the members yourself. Aside from being tedious, this is error-prone.
Second, If you're going to set unassigned pointers to NULL, then when you check for NULLness, make the pointer point to something useful. Never access members of invalid pointers.

If you tell me what you're trying to do, I might be of a little more help.
Last edited on
Yes you are right I'm making mess with C pointers... I'm trying to do red-black tree, but don't understand those pointers and I don't know how can I write a constructor to struct because it's not a class...
Last edited on
In C++, the only difference between structs and classes is that structs default their access to public:
1
2
3
struct A{
    int a; //<-- this is public
};

That's it. In every other sense, structs and classes are equivalient.
If I just need a pointer x to some Node of my tree how I need to initialize it? Like Node *x = new Node or is there other way without that Node?
Last edited on
If you need a pointer, then yes, you must do it that way. But:

REMEMBER TO delete EVERYTHING YOU USE new ON
Topic archived. No new replies allowed.