Heap problem at run time

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 40
Reputation: coolmel55 is an unknown quantity at this point 
Solved Threads: 1
coolmel55 coolmel55 is offline Offline
Light Poster

Heap problem at run time

 
0
  #1
Oct 3rd, 2004
:eek: I have revised my program and am getting a run time heap error. Any ideas on what I need to fix?

  1. #include <iostream>
  2. #include <assert.h>
  3. #include "rectangle.h"
  4. using namespace std;
  5.  
  6.  
  7. CRectangle::CRectangle()
  8. {
  9. size = 0;
  10. pointArray = new CPoint[size];
  11. }
  12.  
  13. CRectangle::CRectangle(CRectangle& R1)
  14. {
  15. size = R1.getSize();
  16. pointArray = new CPoint[size];
  17.  
  18. for (int i = 0; i < size; i++)
  19. {
  20. pointArray[i] = R1.getValue(i);
  21. }
  22. }
  23.  
  24. CRectangle::CRectangle(CPoint P1, CPoint P2)
  25. {
  26. unsigned int width = 0;
  27. unsigned int height = 0;
  28.  
  29. width = (P1.x - P2.x) + 1;
  30. height = (P1.y - P2.y) + 1;
  31.  
  32. size = width * height;
  33.  
  34. pointArray = new CPoint[size];
  35.  
  36. unsigned int curLocation = 0;
  37. for (int i = P2.x; i <= P1.x; i++)
  38. {
  39. for (int j = P2.y; j <= P1.y; j++)
  40. {
  41. CPoint curPoint (i, j);
  42. pointArray[curLocation] = curPoint;
  43. curLocation++;
  44. }
  45. }
  46. }
  47.  
  48. CRectangle::~CRectangle()
  49. {
  50. delete pointArray;
  51. pointArray = NULL;
  52. }
  53.  
  54. unsigned int CRectangle::getSize()
  55. {
  56. return size;
  57. }
  58.  
  59. CPoint CRectangle::getValue(unsigned int location)
  60. {
  61. return pointArray[location];
  62. }
  63.  
  64. void CRectangle::test()
  65. {
  66. }
  67.  
  68. void intersect()
  69. {
  70. //Friend Function to construct intersection of two CRectangles
  71. //The intersection of two CRectangles is the set of points that are
  72. //in both CRectangles.
  73. //intersection R1 R2 = {(x,y)|(x,y) in R1 and (x,y) in R2}
  74. }
  75.  
  76. void union_function()
  77. {
  78. //Friend Function to determine the union of the two CRectangles.
  79. //The union of two CRectangles is the set of points in both
  80. //CRectangles. union R1 R2 = {(x,y)|(x,y) in R1 or (x,y) in R2)}
  81. }
  82.  
  83. void diff()
  84. {
  85. //Friend Function to determine the difference of two CRectangles.
  86. //A difference of two retangles is the set of points that are in the
  87. //first CRectangle but not in the second.
  88. //difference R1 R2 = {(x,y)|(x,y) in R1 and (x,y) not in R2)}
  89. }
  90.  
  91. void concat()
  92. {
  93. //Member Function to concatenate another CRectangle on the
  94. //right. So R1.concatright(R2) will form the union of all the points
  95. //in R1 and a modification of all the points in R2 The x-coordinate of
  96. //all of the points in R2 are increased by one plus the
  97. //maximum x-coordinate of all of the points in R1.
  98. //R1.concatright(R2) = {(x,y)|(x,y) in R1 or (x',y)
  99. //in R2 and x'= x+1+maxX(R2)}
  100. //where maxX(R2) is the maximum of all the x values of the points
  101. //in R2. It may notactually be a CRectangle as defined, but we still
  102. //call the result a "CRectangle".
  103. }
  104.  
  105. void another()
  106. {
  107. //Member Function to concatenate another rectagle above.
  108. //So R1.concatabove(R2) will form the union of all the points
  109. // in R1 and modification of all the points in R2. The
  110. //y-coordinate of all the points in R2 are increased by one plus the
  111. //maximum y-coordinate of all of the points in R1. It may not
  112. //actually be a CRectangle as defined, but we
  113. //still call the resule a "CRectangle".
  114. //R1.concatabove(R2) = {(x,y)|(x,y) in R1 or (x, y') in R2 and
  115. //y= y'+1+maxY(R2)} where maxY(R2) is the maximum of
  116. //all of the y values of the points in R2.
  117. }
  118.  
  119. void CRectangle::printRectangle(CPoint P1, CPoint P2)
  120. {
  121.  
  122. unsigned int printLocation = 0;
  123. for (int i = P1.x; i <= P1.y; i++)
  124. {
  125. for (int j = P2.x; j <= P2.y; j++)
  126. {
  127. CPoint printPoint (i, j);
  128. pointArray[printLocation] = printPoint;
  129. cout << "x";
  130. printLocation++;
  131. }
  132. }
  133. }
  134.  
  135. // Point.cpp: implementation of the CPoint class.
  136. //
  137. //////////////////////////////////////////////////////////////////////
  138.  
  139. #include "Point.h"
  140.  
  141. //////////////////////////////////////////////////////////////////////
  142. // Construction/Destruction
  143. //////////////////////////////////////////////////////////////////////
  144.  
  145. CPoint::CPoint()
  146. {
  147. x = 0;
  148. y = 0;
  149. }
  150.  
  151. CPoint::CPoint(unsigned int x1, unsigned int y1)
  152. {
  153. x = x1;
  154. y = y1;
  155. }
  156.  
  157. CPoint::~CPoint()
  158. {
  159.  
  160. }
  161.  
  162. #ifndef POINT_H
  163. #define POINT_H
  164. #include <iostream>
  165. using namespace std;
  166.  
  167. class CPoint
  168. {
  169. public:
  170. CPoint(); //constructor
  171. CPoint(unsigned int, unsigned int);
  172. ~CPoint(); //destructor
  173. unsigned int x;
  174. unsigned int y;
  175. };
  176. #endif
  177.  
  178. #ifndef RECTANGLE_H
  179. #define RECTANGLE_H
  180. #include <iostream>
  181. #include "Point.h"
  182. using namespace std;
  183.  
  184. class CRectangle
  185. {
  186. public:
  187. CRectangle(); //constructor
  188. CRectangle(CRectangle&); //copy constructor
  189. CRectangle(CPoint, CPoint);
  190. ~CRectangle(); //destructor
  191. unsigned int getSize();
  192. CPoint getValue(unsigned int);
  193. void test();
  194. friend void intersect();
  195. friend void unionFunction();
  196. friend void diff();
  197. friend void concat();
  198. friend void another();
  199. void printRectangle(CPoint, CPoint);
  200.  
  201. private:
  202. unsigned int size;
  203. CPoint* pointArray;
  204. };
  205. #endif
  206.  
  207. #include <iostream>
  208. #include <assert.h>
  209. #include <algorithm>
  210. #include "rectangle.h"
  211. #include "Point.h"
  212. using namespace std;
  213.  
  214. int main()
  215. {
  216. //Regression Testing
  217. CPoint testP1;
  218.  
  219. assert(testP1.x == 0);
  220. assert(testP1.y == 0);
  221.  
  222. CPoint testP2(3,7);
  223.  
  224. assert(testP2.x == 3);
  225. assert(testP2.y == 7);
  226.  
  227. CRectangle testR1;
  228.  
  229. assert(testR1.getSize() == 0);
  230. testR1.test();
  231.  
  232. //Executing Main Program Function NOW!
  233. CPoint P1(4,3);
  234. CPoint P2(1,1);
  235.  
  236. CRectangle R1(P1, P2);
  237.  
  238. return 0;
  239. }
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,622
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 713
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: Heap problem at run time

 
0
  #2
Oct 3rd, 2004
Problem:
  1. delete pointArray;
You allocate memory with new[], so you should release it with delete[]. Otherwise the behavior is undefined, and because the memory manager is usually very fragile, I wouldn't be surprised if that were your problem.
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC