Class array inside of a class array inside of a class problem

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jul 2008
Posts: 6
Reputation: mayoub is an unknown quantity at this point 
Solved Threads: 0
mayoub mayoub is offline Offline
Newbie Poster

Class array inside of a class array inside of a class problem

 
0
  #1
Jul 30th, 2008
Dear DaniWebbers,

If you would all be so kind to help me, I have a problem here and I am trying to know why this happens...

In a large application I am working on, I need to have a struct of some sort with a sub struct-array and the sub struct-array has its own sub struct-array. All of this is dynamically allocated. This was previously done in C and typedef structs and the like, and I am trying to make it slightly more manageable by moving it all to C++ and classes. I'll make use of constructors and destructors, and also help myself by allocating the functions to their representative classes...

The problem happens when I try to free the memory. The code below shows the exact error I am having, but in a simple example. The provided code compiles and runs, but when it gets to the third iteration of 'delete' for mySub, it crashes... it seems as if when the first subclass is deleted, it deletes all the child classes of all the others in the array. Could that be possible?

If there is a better way to implement this, please let me know.

Thanx in advance

  1. #include <string.h>
  2.  
  3. class SubSub {
  4. public:
  5. SubSub();
  6. ~SubSub();
  7.  
  8. char var1[30];
  9. char var2[30];
  10. int var3;
  11. double var4;
  12. };
  13.  
  14. class Sub {
  15. public:
  16. Sub();
  17. ~Sub();
  18.  
  19. SubSub * mySubSub;
  20.  
  21. char var1[30];
  22. char var2[30];
  23. int var3;
  24. double var4;
  25. int mySubSubCount;
  26. };
  27.  
  28. class Super {
  29. public:
  30. Super();
  31. ~Super();
  32.  
  33. Sub * mySub;
  34.  
  35. char var1[30];
  36. char var2[30];
  37. int var3;
  38. double var4;
  39. int mySubCount;
  40. };
  41.  
  42. //----Constructors
  43.  
  44. Super::Super()
  45. {
  46.  
  47. strcpy(var1, "");
  48. strcpy(var2, "");
  49. var3 = 0;
  50. var4 = 0.0;
  51. mySub = NULL;
  52. mySubCount = 0;
  53. }
  54.  
  55. Sub::Sub()
  56. {
  57.  
  58. strcpy(var1, "");
  59. strcpy(var2, "");
  60. var3 = 0;
  61. var4 = 0.0;
  62. mySubSub = NULL;
  63. mySubSubCount = 0;
  64. }
  65.  
  66. SubSub::SubSub()
  67. {
  68.  
  69. strcpy(var1, "");
  70. strcpy(var2, "");
  71. var3 = 0;
  72. var4 = 0.0;
  73. }
  74.  
  75. //----Destructors
  76.  
  77. Super::~Super()
  78. {
  79.  
  80. strcpy(var1, "");
  81. strcpy(var2, "");
  82. var3 = 0;
  83. var4 = 0.0;
  84. if (mySubCount > 0)
  85. {
  86. delete [] mySub;
  87. }
  88. mySubCount = 0;
  89. }
  90.  
  91. Sub::~Sub()
  92. {
  93.  
  94. strcpy(var1, "");
  95. strcpy(var2, "");
  96. var3 = 0;
  97. var4 = 0.0;
  98. if (mySubSubCount > 0)
  99. {
  100. delete [] mySubSub;
  101. }
  102. mySubSubCount = 0;
  103. }
  104.  
  105. SubSub::~SubSub()
  106. {
  107.  
  108. strcpy(var1, "");
  109. strcpy(var2, "");
  110. var3 = 0;
  111. var4 = 0.0;
  112. }
  113.  
  114. void main()
  115. {
  116. int a = 3;
  117. int b = 3;
  118. int c = 3;
  119. int i = 0;
  120. int j = 0;
  121. int k = 0;
  122. int l = 0;
  123. int m = 0;
  124. Super * mySuper = new Super;
  125. Sub * mySub;
  126. SubSub * mySubSub;
  127.  
  128. do
  129. {
  130.  
  131. for (i = 0; i < a; i++)
  132. {
  133. strcpy(mySuper->var1, "test1");
  134. strcpy(mySuper->var2, "test2");
  135. mySuper->var3 = 3;
  136. mySuper->var4 = 4;
  137. for (j = 0; j < b; j++)
  138. {
  139. if (mySuper->mySubCount > 0)
  140. {
  141. mySub = new Sub[mySuper->mySubCount + 1];
  142. for (l = 0; l < mySuper->mySubCount; l++)
  143. {
  144. mySub[l] = mySuper->mySub[l];
  145. }
  146.  
  147. delete [] mySuper->mySub;
  148. mySuper->mySub = mySub;
  149. delete mySub;
  150. mySuper->mySubCount++;
  151. }
  152. else
  153. {
  154. mySuper->mySub = new Sub[1];
  155. mySuper->mySubCount = 1;
  156. }
  157. strcpy(mySuper->mySub[j].var1, "test1");
  158. strcpy(mySuper->mySub[j].var2, "test2");
  159. mySuper->mySub[j].var3 = 3;
  160. mySuper->mySub[j].var4 = 4;
  161.  
  162. for (k = 0; k < c; k++)
  163. {
  164. if (mySuper->mySub[j].mySubSubCount > 0)
  165. {
  166. mySubSub = new SubSub[mySuper->mySub[j].mySubSubCount + 1];
  167. for (m = 0; m < mySuper->mySub[j].mySubSubCount; m++)
  168. {
  169. mySubSub[m] = mySuper->mySub[j].mySubSub[m];
  170. }
  171.  
  172. delete [] mySuper->mySub[j].mySubSub;
  173. mySuper->mySub[j].mySubSub = mySubSub;
  174. delete mySubSub;
  175. mySuper->mySub[j].mySubSubCount++;
  176. }
  177. else
  178. {
  179. mySuper->mySub[j].mySubSub = new SubSub[1];
  180. mySuper->mySub[j].mySubSubCount = 1;
  181. }
  182. strcpy(mySuper->mySub[j].mySubSub[k].var1, "test1");
  183. strcpy(mySuper->mySub[j].mySubSub[k].var2, "test2");
  184. mySuper->mySub[j].mySubSub[k].var3 = 3;
  185. mySuper->mySub[j].mySubSub[k].var4 = 4;
  186.  
  187. }
  188. }
  189. }
  190. a--;
  191. b--;
  192. c--;
  193. }
  194. while (a && b && c);
  195.  
  196. }
Last edited by mayoub; Jul 30th, 2008 at 6:22 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #2
Jul 30th, 2008
one problem:
  1. int* x = new int; //ok
  2. delete x; //ok
but
  1. int* x = new int;
  2. int* y = x;
  3. delete x;
  4. ....
  5. do something with y is wrong, because y is pointer to deleted object :}
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: mayoub is an unknown quantity at this point 
Solved Threads: 0
mayoub mayoub is offline Offline
Newbie Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #3
Jul 30th, 2008
ivailosp, thank you very much for the reply.

I do see that... and I understand that concept. I do not see where I would have been deleting something out of scope. When I am deleting and recreating the structure, I am refilling it all in with the same old information, except now the array is one bigger.

If I understand what's happening correctly (I hope), I am in fact filling in the second inner array (mySubSub) 3 times for every time that I am filling in the first array (mySub). The first 2 times this happens, and the first bunch of values are deleted and refilled using the new temporary array pointers, this works fine.

Tracing through the code I noticed that it breaks on the third iteration of delete [] mySubSub; .

If there is some way to make this work, please let me know.

Again, thank you all very much.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #4
Jul 31st, 2008
					mySuper->mySub = mySub;
					delete mySub;
					mySuper->mySubCount++;
				}
				else
				{
					mySuper->mySub = new Sub[1];
					mySuper->mySubCount = 1;
				}

				strcpy(mySuper->mySub[j].var1, "test1");
				strcpy(mySuper->mySub[j].var2, "test2");
				mySuper->mySub[j].var3 = 3;
				mySuper->mySub[j].var4 = 4;
Last edited by ivailosp; Jul 31st, 2008 at 5:20 am.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: mayoub is an unknown quantity at this point 
Solved Threads: 0
mayoub mayoub is offline Offline
Newbie Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #5
Jul 31st, 2008
ivailosp,

Again, thank you very much for the reply.

What I thought I was doing was that I was creating a new temporary array, filling it with the same old information, and then after I delete the old (smaller) array, I am copying the values back... do I have to manually do each and every value for this to work?

Thanx;
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #6
Jul 31st, 2008
yes, but you can try using std::vector
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: mayoub is an unknown quantity at this point 
Solved Threads: 0
mayoub mayoub is offline Offline
Newbie Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #7
Aug 1st, 2008
Hey... I had tried doing that... Interesting that you'd suggest it.

I did this:

  1. #include <vector>
  2. #include <string.h>
  3.  
  4. class SubSub {
  5. public:
  6. SubSub();
  7. ~SubSub();
  8.  
  9. char var1[30];
  10. char var2[30];
  11. int var3;
  12. double var4;
  13. };
  14.  
  15. class Sub {
  16. public:
  17. Sub();
  18. ~Sub();
  19.  
  20. std::vector<SubSub> mySubSub;
  21.  
  22. char var1[30];
  23. char var2[30];
  24. int var3;
  25. double var4;
  26. int mySubSubCount;
  27. };
  28.  
  29. class Super {
  30. public:
  31. Super();
  32. ~Super();
  33.  
  34. std::vector<Sub> mySub;
  35.  
  36. char var1[30];
  37. char var2[30];
  38. int var3;
  39. double var4;
  40. int mySubCount;
  41. };
  42.  
  43. //----Constructors
  44.  
  45. Super::Super()
  46. {
  47.  
  48. strcpy(var1, "");
  49. strcpy(var2, "");
  50. var3 = 0;
  51. var4 = 0.0;
  52. mySub.resize(1);
  53. mySubCount = 0;
  54. }
  55.  
  56. Sub::Sub()
  57. {
  58.  
  59. strcpy(var1, "");
  60. strcpy(var2, "");
  61. var3 = 0;
  62. var4 = 0.0;
  63. mySubSub.resize(1);
  64. mySubSubCount = 0;
  65. }
  66.  
  67. SubSub::SubSub()
  68. {
  69.  
  70. strcpy(var1, "");
  71. strcpy(var2, "");
  72. var3 = 0;
  73. var4 = 0.0;
  74. }
  75.  
  76. //----Destructors
  77.  
  78. Super::~Super()
  79. {
  80.  
  81. strcpy(var1, "");
  82. strcpy(var2, "");
  83. var3 = 0;
  84. var4 = 0.0;
  85. mySubCount = 0;
  86. }
  87.  
  88. Sub::~Sub()
  89. {
  90.  
  91. strcpy(var1, "");
  92. strcpy(var2, "");
  93. var3 = 0;
  94. var4 = 0.0;
  95. mySubSubCount = 0;
  96. }
  97.  
  98. SubSub::~SubSub()
  99. {
  100.  
  101. strcpy(var1, "");
  102. strcpy(var2, "");
  103. var3 = 0;
  104. var4 = 0.0;
  105. }
  106.  
  107. void main()
  108. {
  109. unsigned int a = 0;
  110. unsigned int b = 0;
  111. unsigned int c = 0;
  112. unsigned int i = 0;
  113. unsigned int j = 0;
  114. unsigned int k = 0;
  115. std::vector<Super> mySuper;
  116.  
  117. a = 30;
  118. b = 30;
  119. c = 30;
  120.  
  121. for (i = 0; i < a; i++)
  122. {
  123. if (i >= mySuper.capacity())
  124. {
  125. mySuper.resize(mySuper.capacity() + 5);
  126. }
  127. strcpy(mySuper.at(i).var1, "test1");
  128. strcpy(mySuper.at(i).var2, "test2");
  129. mySuper.at(i).var3 = 3;
  130. mySuper.at(i).var4 = 4;
  131. for (j = 0; j < b; j++)
  132. {
  133. if (j >= mySuper.at(i).mySub.capacity())
  134. {
  135. mySuper.at(i).mySub.resize(mySuper.at(i).mySub.capacity() + 5);
  136. }
  137. strcpy(mySuper.at(i).mySub.at(j).var1, "test11");
  138. strcpy(mySuper.at(i).mySub.at(j).var2, "test22");
  139. mySuper.at(i).mySub.at(j).var3 = 33;
  140. mySuper.at(i).mySub.at(j).var4 = 44;
  141.  
  142. for (k = 0; k < c; k++)
  143. {
  144. if (k >= mySuper.at(i).mySub.at(j).mySubSub.capacity())
  145. {
  146. mySuper.at(i).mySub.at(j).mySubSub.resize(mySuper.at(i).mySub.at(j).mySubSub.capacity() + 5);
  147. }
  148. strcpy(mySuper.at(i).mySub.at(j).mySubSub.at(k).var1, "test111");
  149. strcpy(mySuper.at(i).mySub.at(j).mySubSub.at(k).var2, "test222");
  150. mySuper.at(i).mySub.at(j).mySubSub.at(k).var3 = 333;
  151. mySuper.at(i).mySub.at(j).mySubSub.at(k).var4 = 444;
  152. }
  153. }
  154. }
  155.  
  156. mySuper.clear();
  157. }

Although, once I have the iterations at 3 it works flawlessly, but when I go up above 20, they crash! I will need this to expand to 100+ in a wost-case scenario, and 200+ for safety, and it can't seem to go past 20.

Thanx
Last edited by mayoub; Aug 1st, 2008 at 1:01 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: mayoub is an unknown quantity at this point 
Solved Threads: 0
mayoub mayoub is offline Offline
Newbie Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #8
Aug 1st, 2008
Up to 20 iterations, the code works...

At 21+, it errors here:

  1. void _Xran() const
  2. { // report an out_of_range error
  3. _THROW(out_of_range, "invalid vector<T> subscript");
  4. }

That code is part of the <vector> file.

Thanx
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 129
Reputation: ivailosp is an unknown quantity at this point 
Solved Threads: 22
ivailosp ivailosp is offline Offline
Junior Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #9
Aug 1st, 2008
Notice that, in vectors, the capacity is not necessarily equal to the number of elements that conform the underlying vector content (this can be obtained with member vector::size), but the capacity of the actual allocated space, which is either equal or greater than the content size.
use vector::size(), not vector::capacity()
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 6
Reputation: mayoub is an unknown quantity at this point 
Solved Threads: 0
mayoub mayoub is offline Offline
Newbie Poster

Re: Class array inside of a class array inside of a class problem

 
0
  #10
Aug 1st, 2008
THANX!!

It works!!

Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC