Problem with odd runtime error

A runtime error I have yet to see before.

Page 1 of 1

2 Replies - 1173 Views - Last Post: 26 February 2008 - 07:20 AM Rate Topic: -----

#1 joshthejest   User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-February 08

Problem with odd runtime error

Posted 26 February 2008 - 01:46 AM

   1.
	  #include <iostream>
   2.
	   
   3.
	  using namespace std;
   4.
	   
   5.
	  struct nodeType
   6.
	  {
   7.
		  short stats[3];
   8.
		  nodeType *oneMiss;
   9.
		  nodeType *oneCan;
  10.
		  nodeType *twoMiss;
  11.
		  nodeType *twoCan;
  12.
		  nodeType *oneEach;
  13.
	  };
  14.
	   
  15.
	  class willowTreeType
  16.
	  {
  17.
		  public:
  18.
			  const willowTreeType& operator= (const willowTreeType&);
  19.
			  bool isEmpty(); //RETURN true if tree is empty
  20.
			  void burnTree(); //DESTROY tree
  21.
			  void deleteNode(short deleteItem[]);
  22.
	   
  23.
			  willowTreeType(); //Default Constructor
  24.
			  ~willowTreeType(); //Destructor
  25.
		  protected:
  26.
			  nodeType *root;
  27.
	   
  28.
		  private:
  29.
			  void destroyNode(nodeType *p);
  30.
			  bool checkDup(nodeType *p);
  31.
			  bool checkDeath(nodeType *p);
  32.
			  bool possible(nodeType *p);
  33.
			  void populateTree(nodeType *p);
  34.
				  //Recursive function where if the node meets the criteria it
  35.
				  //gets inserted and then goes to its 5 sub nodes
  36.
			  bool test(nodeType *p);
  37.
	  };
  38.
	   
  39.
	  willowTreeType::willowTreeType()
  40.
	  {
  41.
		  //create a new node and set it equal to the root
  42.
		  nodeType *newNode = new nodeType;
  43.
		  newNode->stats[0] = 0;
  44.
		  newNode->stats[1] = 0;
  45.
		  newNode->stats[2] = 1;
  46.
		  root = newNode;
  47.
		  populateTree(newNode);
  48.
	  }
  49.
	   
  50.
	  willowTreeType::~willowTreeType()
  51.
	  {
  52.
		  burnTree();
  53.
	  }
  54.
	   
  55.
	  void willowTreeType::burnTree()
  56.
	  {
  57.
		  destroyNode(root);
  58.
	  }
  59.
	   
  60.
	  void willowTreeType::destroyNode(nodeType *p)
  61.
	  {
  62.
		  if(p != NULL)
  63.
			  {
  64.
					  destroyNode(p->oneCan);
  65.
					  destroyNode(p->oneMiss);
  66.
					  destroyNode(p->twoCan);
  67.
					  destroyNode(p->twoMiss);
  68.
					  destroyNode(p->oneEach);
  69.
					  delete p;
  70.
					  p = NULL;
  71.
			  }
  72.
	  }
  73.
	   
  74.
	  bool willowTreeType::isEmpty()
  75.
	  {
  76.
			  return(root == NULL);
  77.
	  }
  78.
	   
  79.
	  void willowTreeType::populateTree(nodeType *p)
  80.
	  {
  81.
		  //if node makes then populate that node further
  82.
		  //if node does not make do nothing it is already null from test
  83.
		  p->oneCan = new nodeType;
  84.
		  p->oneCan->stats[0] = p->stats[0] + p->stats[2];
  85.
		  p->oneCan->stats[1] = p->stats[1];
  86.
		  p->oneCan->stats[2] = p->stats[2]*-1;
  87.
	   
  88.
		  if(test(p->oneCan))
  89.
		  {
  90.
			  populateTree(p->oneCan);
  91.
		  }
  92.
		  p->oneMiss = new nodeType;
  93.
		  p->oneMiss->stats[0] = p->stats[0];
  94.
		  p->oneMiss->stats[1] = p->stats[1] + p->stats[2];
  95.
		  p->oneMiss->stats[2] = p->stats[2]*-1;
  96.
		  if(test(p->oneMiss))
  97.
		  {
  98.
			  populateTree(p->oneMiss);
  99.
		  }
 100.
		  p->twoCan = new nodeType;
 101.
		  p->twoCan->stats[0] = p->stats[0] + 2*p->stats[2];
 102.
		  p->twoCan->stats[1] = p->stats[1];
 103.
		  p->twoCan->stats[2] = p->stats[2]*-1;
 104.
		  if(test(p->twoCan))
 105.
		  {
 106.
			  populateTree(p->twoCan);
 107.
		  }
 108.
		  p->twoMiss = new nodeType;
 109.
		  p->twoMiss->stats[0] = p->stats[0];
 110.
		  p->twoMiss->stats[1] = p->stats[1] + 2*p->stats[2];
 111.
		  p->twoMiss->stats[2] = p->stats[2]*-1;
 112.
		  if(test(p->twoMiss))
 113.
		  {
 114.
			  populateTree(p->twoMiss);
 115.
		  }
 116.
		  p->oneEach = new nodeType;
 117.
		  p->oneEach->stats[0] = p->stats[0] + p->stats[2];
 118.
		  p->oneEach->stats[1] = p->stats[1] + p->stats[2];
 119.
		  p->oneEach->stats[2] = p->stats[2]*-1;
 120.
		  if(test(p->oneEach))
 121.
		  {
 122.
			  populateTree(p->oneEach);
 123.
		  }
 124.
	   
 125.
	  }
 126.
	  //return 1 if the value exists
 127.
	  //return 0 if the value does not exist
 128.
	  bool willowTreeType::checkDup(nodeType *p)
 129.
	  {
 130.
		  return 0;
 131.
	  }
 132.
	  //return 1 if death
 133.
	  //return 0 if no death
 134.
	  bool willowTreeType::checkDeath(nodeType *p)
 135.
	  {
 136.
		  //  0 1
 137.
		  //0 3 2
 138.
		  //1 3 1
 139.
		  //2 2 1
 140.
		  //3 0 1
 141.
		  //4 0 2
 142.
		  //5 1 2
 143.
		  short deathStates[6][2] = {{3,2},{3,1},{2,1},{0,1},{0,2},{1,2} };
 144.
		  for(int x=0; x<6; x++)
 145.
		  {
 146.
			  if(p->stats[0] == deathStates[x][0] && p->stats[1] == deathStates[x][1])
 147.
			  {
 148.
				  return 0;
 149.
			  }
 150.
		  }
 151.
	   
 152.
		  return 1;
 153.
	   
 154.
	  }
 155.
	  //return 1 if possible
 156.
	  //return 0 if not possible
 157.
	  bool willowTreeType::possible(nodeType *p)
 158.
	  {
 159.
		  if (p->stats[0] <= 2 && p->stats[0] >= 0)
 160.
			  if (p->stats[1] <= 2 && p->stats[1] >= 0)
 161.
				  if (p->stats[2] == -1 || p->stats[2] == 1)
 162.
					  return 1;
 163.
		  return 0;
 164.
	  }
 165.
	  //return 1 if node makes
 166.
	  //return 0 if node becomes NULL
 167.
	  bool willowTreeType::test(nodeType *p)
 168.
	  {
 169.
		  if(!possible(p) || checkDeath(p) || !checkDup(p))
 170.
		  {
 171.
			  delete p;
 172.
			  p = NULL;
 173.
			  return 0;
 174.
		  }
 175.
		  else
 176.
		  {
 177.
			  cout<<"Value: " <<p->stats[0] <<p->stats[1] <<p->stats[2];
 178.
			  return 1;
 179.
		  }
 180.
	  }

Pastebin::Color Coded and Neat


*** glibc detected *** ./a.out: double free or corruption (fasttop): 0x000000000
0502050 ***
======= Backtrace: =========
/lib/libc.so.6[0x2b956d837a7d]
/lib/libc.so.6(__libc_free+0x76)[0x2b956d8390a6]
./a.out(__gxx_personality_v0+0x323)[0x400a3b]
./a.out(__gxx_personality_v0+0x2d6)[0x4009ee]
./a.out(__gxx_personality_v0+0x34a)[0x400a62]
./a.out(__gxx_personality_v0+0x361)[0x400a79]
./a.out[0x400e8e]
/lib/libc.so.6(__libc_start_main+0xf4)[0x2b956d7ea374]
./a.out(__gxx_personality_v0+0x71)[0x400789]
======= Memory map: ========
00400000-00402000 r-xp 00000000 03:03 5702411 /home/j
oshthejest/CannibalsProblem/a.out
00501000-00502000 rw-p 00001000 03:03 5702411 /home/j
oshthejest/CannibalsProblem/a.out
00502000-00523000 rw-p 00502000 00:00 0 [heap]
2b956d24f000-2b956d26a000 r-xp 00000000 03:03 5095557 /lib64/
ld-2.5.so
2b956d26a000-2b956d26c000 rw-p 2b956d26a000 00:00 0
2b956d36a000-2b956d36b000 r--p 0001b000 03:03 5095557 /lib64/
ld-2.5.so
2b956d36b000-2b956d36c000 rw-p 0001c000 03:03 5095557 /lib64/
ld-2.5.so
2b956d36c000-2b956d44f000 r-xp 00000000 03:03 5213462 /usr/li
b64/gcc/x86_64-pc-linux-gnu/4.1.2/libstdc++.so.6.0.8
2b956d44f000-2b956d54f000 ---p 000e3000 03:03 5213462 /usr/li
b64/gcc/x86_64-pc-linux-gnu/4.1.2/libstdc++.so.6.0.8
2b956d54f000-2b956d555000 r--p 000e3000 03:03 5213462 /usr/li
b64/gcc/x86_64-pc-linux-gnu/4.1.2/libstdc++.so.6.0.8
2b956d555000-2b956d558000 rw-p 000e9000 03:03 5213462 /usr/li
b64/gcc/x86_64-pc-linux-gnu/4.1.2/libstdc++.so.6.0.8
2b956d558000-2b956d56a000 rw-p 2b956d558000 00:00 0
2b956d56a000-2b956d5be000 r-xp 00000000 03:03 5095571 /lib64/
libm-2.5.so
2b956d5be000-2b956d6bd000 ---p 00054000 03:03 5095571 /lib64/
libm-2.5.so
2b956d6bd000-2b956d6bf000 rw-p 00053000 03:03 5095571 /lib64/
libm-2.5.so
2b956d6bf000-2b956d6cb000 r-xp 00000000 03:03 5160963 /lib64/
libgcc_s.so.1
2b956d6cb000-2b956d7cb000 ---p 0000c000 03:03 5160963 /lib64/
libgcc_s.so.1
2b956d7cb000-2b956d7cc000 rw-p 0000c000 03:03 5160963 /lib64l
ibgcc_s.so.1
2b956d7cc000-2b956d7cd000 rw-p 2b956d7cc000 00:00 0
2b956d7cd000-2b956d8fe000 r-xp 00000000 03:03 5095562 /lib64/
libc-2.5.so
2b956d8fe000-2b956d9fe000 ---p 00131000 03:03 5095562 /lib64/
libc-2.5.so
2b956d9fe000-2b956da01000 r--p 00131000 03:03 5095562 /lib64/
libc-2.5.so
2b956da01000-2b956da03000 rw-p 00134000 03:03 5095562 /lib64/
libc-2.5.so
2b956da03000-2b956da09000 rw-p 2b956da03000 00:00 0
2b9570000000-2b9570021000 rw-p 2b9570000000 00:00 0
2b9570021000-2b9574000000 ---p 2b9570021000 00:00 0
7fff3d846000-7fff3d85b000 rw-p 7ffffffea000 00:00 0 [stack]
7fff3d9fe000-7fff3da00000 r-xp 7fff3d9fe000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsysca
ll]
Aborted

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with odd runtime error

#2 AmitTheInfinity   User is offline

  • C Surfing ∞
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,565
  • Joined: 25-January 07

Re: Problem with odd runtime error

Posted 26 February 2008 - 01:58 AM

do you have any control logic for function populateTree ? You are creating a node and calling the function again to populate that child node too. This might be causing excess recursion and leading to stack overflow. I am not sure about it, but from first view it seems that your populateTree function does not have control on it's recursive call.
Was This Post Helpful? 0
  • +
  • -

#3 joshthejest   User is offline

  • New D.I.C Head
  • member icon

Reputation: 1
  • View blog
  • Posts: 32
  • Joined: 26-February 08

Re: Problem with odd runtime error

Posted 26 February 2008 - 07:20 AM

View PostAmitTheInfinity, on 26 Feb, 2008 - 01:58 AM, said:

do you have any control logic for function populateTree ? You are creating a node and calling the function again to populate that child node too. This might be causing excess recursion and leading to stack overflow. I am not sure about it, but from first view it seems that your populateTree function does not have control on it's recursive call.


I think you may be right... what a long error for something so simple...
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1