storage area of the struct data type

I was trying to run this program.
I am getting this error

" An unhandled exception of type 'System.AccessViolationException' occurred in Project_Euler_MS_Intervie_2009.exe

Additional information: Attempted to read or write protected memory. This is often an indication that other memory is corrupt"

Does this indicate I am writing to the data area.
So the question is whether the struct here is stored in data area?

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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include "stdafx.h"
#include "iostream"
#include "conio.h"
using namespace std;

class btree
{
    public:
        btree(int);
        ~btree();
        void insert(int);
        void destroy_tree();
 
    private:
		struct node
		{
		  int key_value;
		  node *left;
		  node *right;
		}*root;
        void destroy_tree(node*);
        void insert(int,node*);        
		//node *root;
};

btree::btree(int val)
{
	root->key_value=val;
	root->left= NULL;
	root->right= NULL;
}

btree::~btree()
{
  destroy_tree();
}

void btree::destroy_tree(node* leaf)
{
  if(leaf!=NULL)
  {
    destroy_tree(leaf->left);
    destroy_tree(leaf->right);
    delete leaf;
  }
}

void btree::insert(int key, node* leaf)
{
  if(key< leaf->key_value)
  {
    if(leaf->left!=NULL)
     insert(key, leaf->left);
    else
    {
      leaf->left=new node;
      leaf->left->key_value=key;
      leaf->left->left=NULL;    //Sets the left child of the child node to null
      leaf->left->right=NULL;   //Sets the right child of the child node to null
    }  
  }
  else if(key>=leaf->key_value)
  {
    if(leaf->right!=NULL)
      insert(key, leaf->right);
    else
    {
      leaf->right=new node;
      leaf->right->key_value=key;
      leaf->right->left=NULL;  //Sets the left child of the child node to null
      leaf->right->right=NULL; //Sets the right child of the child node to null
    }
  }
}

void btree::insert(int key)
{
  if(root!=NULL)
    insert(key, root);
  else
  {
    root=new node;
    root->key_value=key;
    root->left=NULL;
    root->right=NULL;
  }
}

void btree::destroy_tree()
{
  destroy_tree(root);
}

int main(void)
{
	int val=5;
	btree *b1 = new btree(val);
	//b1->insert(val);
	b1->destroy_tree();
	return 0;
}
It looks like this: You call destroy tree, which deletes the tree and also the head node. Then your object is destructed and it is called again, and tries to delete memory you don't own anymore.
Topic archived. No new replies allowed.