char ** s = new[], char s[i] = new [], for (i < size) delete [] s[i] = Segfault

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Aug 2006
Posts: 13
Reputation: saishn is an unknown quantity at this point 
Solved Threads: 0
saishn saishn is offline Offline
Newbie Poster

char ** s = new[], char s[i] = new [], for (i < size) delete [] s[i] = Segfault

 
0
  #1
Aug 31st, 2006
Again, I don't know what I've done wrong.
  1. #include <iostream>
  2. using std::cout;
  3. using std::endl;
  4.  
  5. #include <cstring>
  6. using std::strcpy;
  7. using std::strcat;
  8.  
  9. #include <cstdio>
  10. using std::sprintf;
  11.  
  12. void destroyMultiCharArray(char * * anArray, int size)
  13. {
  14. for (int i = 0; i < size; i++)
  15. {
  16. delete [] anArray[ i ];
  17. }
  18. delete [] anArray;
  19. anArray = 0;
  20. }
  21. class aClass
  22. {
  23. public:
  24. aClass();
  25. ~aClass();
  26. bool addPattern(char * type, char * ptrn);
  27.  
  28. private:
  29. int numPtrns;
  30. char ** ptrnTypes;
  31. char ** ptrns;
  32. };
  33. aClass::aClass()
  34. {
  35. this->numPtrns = 0;
  36.  
  37. char u[] = "upatdjskoddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd2333333333333333333";
  38.  
  39. char i[] = "dsud8923333333333333duiuuuuuuuuuuuuut";
  40.  
  41. char q[] = "HDddddddddddd*@#(##########HDJD";
  42.  
  43. char p[] = "dJ(#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&@******************************************DGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG*#######################";
  44.  
  45. char h[] = "#(((((((((((((((((((((((((DKKKKKKKKKKKKKKKKKKKKKKKKKK#(((((((((((((((((((((((((DKMMMMMMMMMMMMMMMMMMMMMMMMMM#IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII";
  46.  
  47.  
  48. this->addPattern("u", u);
  49. this->addPattern("i", i);
  50. this->addPattern("q", q);
  51. this->addPattern("p", p);
  52. this->addPattern("h", h);
  53. }
  54. aClass::~aClass()
  55. {
  56. destroyMultiCharArray(ptrnTypes, numPtrns);
  57. destroyMultiCharArray(ptrns, numPtrns);
  58. }
  59. bool aClass::addPattern(char * type, char * ptrn)
  60. {
  61.  
  62. char * * typesHolder = 0;
  63. char * * ptrnsHolder = 0;
  64. if (this->numPtrns)
  65. {
  66.  
  67. //holders point to the current array
  68. //which will be used to repopulate
  69. //the ptrn and types arrays
  70. typesHolder = this->ptrnTypes;
  71. ptrnsHolder = this->ptrns;
  72. }
  73.  
  74. int newPtrnsNum = this->numPtrns + 1;
  75. //Making room for one more pattern
  76. this->ptrnTypes = new char * [newPtrnsNum];
  77. this->ptrns = new char * [newPtrnsNum];
  78.  
  79. //now first adding the new pattern
  80. //and then repopulating using the
  81. //holders
  82. this->ptrnTypes[ 0 ] = new char [strlen(type) + 1];
  83. strcpy(this->ptrnTypes[ 0 ], type);
  84.  
  85. this->ptrns[ 0 ] = new char [strlen(ptrn) + 1];
  86. strcpy(this->ptrns[ 0 ], ptrn);
  87.  
  88. if (typesHolder)
  89. {
  90. int i;
  91.  
  92. //Holds the current index of the ptrn
  93. //arrays
  94. int ptIndex;
  95. for (i = 0, ptIndex = 1 ; i < this->numPtrns; i++, ++ptIndex)
  96. {
  97. this->ptrnTypes[ ptIndex ] = new char [
  98. (strlen(typesHolder[ i ] + 1))
  99. ];
  100. strcpy(this->ptrnTypes[ ptIndex ] , typesHolder[ i ]);
  101.  
  102. this->ptrns[ ptIndex ] = new char [
  103. (strlen(ptrnsHolder[ i ] + 1))
  104. ];
  105. strcpy(this->ptrns[ ptIndex ], ptrnsHolder[ i ]);
  106. }
  107.  
  108. destroyMultiCharArray(typesHolder, this->numPtrns);
  109. destroyMultiCharArray(ptrnsHolder, this->numPtrns);
  110. typesHolder = 0;
  111. ptrnsHolder = 0;
  112. }
  113.  
  114. this->numPtrns = newPtrnsNum;
  115. return true;
  116. }
  117.  
  118. int main()
  119. {
  120. aClass aObj;
  121.  
  122. return 0;
  123. }
As mentioned in the title, this Segfaults on me. I don't understand why. I've used gdb and it (this code which comes out of a larger snippet which fails inside of destroyMulti) fails in the addPattern function at "this->ptrnTypes[ ptIndex ] = new char [ ..." .

What's wrong?

If you could also give some tips on debugging memory issues that cause these problems please do. I've seen "valgrind" but haven't tried it yet.

As always, thanks for any help that you can provide.

Btw, I made the strings that long because it ran fine when I used a short string. I know(think) that I've mishandled the memory somehow, but don't know where.
Last edited by saishn; Aug 31st, 2006 at 5:43 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 13
Reputation: saishn is an unknown quantity at this point 
Solved Threads: 0
saishn saishn is offline Offline
Newbie Poster

Re: char ** s = new[], char s[i] = new [], for (i < size) delete [] s[i] = Segfault

 
0
  #2
Aug 31st, 2006
Found the error
  1. this->ptrnTypes[ ptIndex ] = new char [
  2. (strlen(typesHolder[ i ] + 1))
  3. ];
  4. strcpy(this->ptrnTypes[ ptIndex ] , typesHolder[ i ]);
  5.  
  6. this->ptrns[ ptIndex ] = new char [
  7. (strlen(ptrnsHolder[ i ] + 1))
  8. ];
  9. strcpy(this->ptrns[ ptIndex ], ptrnsHolder[ i ]);
The strlen() lines should be
  1. strlen(typesHolder[i])
  2. strlen(ptrnsHolder[i])
The closing ")" was surrounding the + 1 as well.
Last edited by saishn; Aug 31st, 2006 at 6:50 pm.
Reply With Quote Quick reply to this message  
Reply

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




Views: 3692 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC