944,101 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 6367
  • C RSS
Aug 31st, 2006
0

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

Expand Post »
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.
Similar Threads
Reputation Points: 16
Solved Threads: 0
Newbie Poster
saishn is offline Offline
13 posts
since Aug 2006
Aug 31st, 2006
0

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

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.
Reputation Points: 16
Solved Threads: 0
Newbie Poster
saishn is offline Offline
13 posts
since Aug 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: SOS help
Next Thread in C Forum Timeline: <map> find function





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC