# \[C++\] trying to create binary tree, crash when adding nodes

**URL:** <https://forum.kirupa.com/t/c-trying-to-create-binary-tree-crash-when-adding-nodes/255094>\
**Category:** Uncategorized\
**Created:** [March 19, 2008, 12:34am UTC](https://forum.kirupa.com/t/c-trying-to-create-binary-tree-crash-when-adding-nodes/255094 "2008-03-19T00:34:19Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![johnlouis](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/johnlouis/32/568_2.png) [@johnlouis](https://forum.kirupa.com/u/johnlouis)\
**Post date:** [March 19, 2008, 12:34am UTC](https://forum.kirupa.com/t/c-trying-to-create-binary-tree-crash-when-adding-nodes/255094/1 "2008-03-19T00:34:19Z")

</div>

```auto
struct TreeNode {
              int item; // The data in this node.
              TreeNode *left; // Pointer to the left subtree.
              TreeNode *right; // Pointer to the right subtree.
};
int main (void){
	TreeNode myTree;
	myTree.item = 1;
	myTree.left->item = 2;
	myTree.right->item = 3; //it starts crashing here. 
	myTree.left->left->item = 4;
	myTree.left->right->item = 5;
	myTree.right->left->item = 6;
	myTree.right->right->item = 7;
	return 0;
}

```

it compiles fine, and i think the syntax is fine. but it crashes when i run it. i removed the lines starting at item = 3, and it will run fine.
