| | |
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:
Solved Threads: 0
Again, I don't know what I've done wrong.
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.
C Syntax (Toggle Plain Text)
#include <iostream> using std::cout; using std::endl; #include <cstring> using std::strcpy; using std::strcat; #include <cstdio> using std::sprintf; void destroyMultiCharArray(char * * anArray, int size) { for (int i = 0; i < size; i++) { delete [] anArray[ i ]; } delete [] anArray; anArray = 0; } class aClass { public: aClass(); ~aClass(); bool addPattern(char * type, char * ptrn); private: int numPtrns; char ** ptrnTypes; char ** ptrns; }; aClass::aClass() { this->numPtrns = 0; char u[] = "upatdjskoddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddddd2333333333333333333"; char i[] = "dsud8923333333333333duiuuuuuuuuuuuuut"; char q[] = "HDddddddddddd*@#(##########HDJD"; char p[] = "dJ(#&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&@******************************************DGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGGG*#######################"; char h[] = "#(((((((((((((((((((((((((DKKKKKKKKKKKKKKKKKKKKKKKKKK#(((((((((((((((((((((((((DKMMMMMMMMMMMMMMMMMMMMMMMMMM#IIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIIII"; this->addPattern("u", u); this->addPattern("i", i); this->addPattern("q", q); this->addPattern("p", p); this->addPattern("h", h); } aClass::~aClass() { destroyMultiCharArray(ptrnTypes, numPtrns); destroyMultiCharArray(ptrns, numPtrns); } bool aClass::addPattern(char * type, char * ptrn) { char * * typesHolder = 0; char * * ptrnsHolder = 0; if (this->numPtrns) { //holders point to the current array //which will be used to repopulate //the ptrn and types arrays typesHolder = this->ptrnTypes; ptrnsHolder = this->ptrns; } int newPtrnsNum = this->numPtrns + 1; //Making room for one more pattern this->ptrnTypes = new char * [newPtrnsNum]; this->ptrns = new char * [newPtrnsNum]; //now first adding the new pattern //and then repopulating using the //holders this->ptrnTypes[ 0 ] = new char [strlen(type) + 1]; strcpy(this->ptrnTypes[ 0 ], type); this->ptrns[ 0 ] = new char [strlen(ptrn) + 1]; strcpy(this->ptrns[ 0 ], ptrn); if (typesHolder) { int i; //Holds the current index of the ptrn //arrays int ptIndex; for (i = 0, ptIndex = 1 ; i < this->numPtrns; i++, ++ptIndex) { this->ptrnTypes[ ptIndex ] = new char [ (strlen(typesHolder[ i ] + 1)) ]; strcpy(this->ptrnTypes[ ptIndex ] , typesHolder[ i ]); this->ptrns[ ptIndex ] = new char [ (strlen(ptrnsHolder[ i ] + 1)) ]; strcpy(this->ptrns[ ptIndex ], ptrnsHolder[ i ]); } destroyMultiCharArray(typesHolder, this->numPtrns); destroyMultiCharArray(ptrnsHolder, this->numPtrns); typesHolder = 0; ptrnsHolder = 0; } this->numPtrns = newPtrnsNum; return true; } int main() { aClass aObj; return 0; }
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.
•
•
Join Date: Aug 2006
Posts: 13
Reputation:
Solved Threads: 0
Re: char ** s = new[], char s[i] = new [], for (i < size) delete [] s[i] = Segfault
0
#2 Aug 31st, 2006
Found the error
The strlen() lines should be
The closing ")" was surrounding the + 1 as well.
C Syntax (Toggle Plain Text)
this->ptrnTypes[ ptIndex ] = new char [ (strlen(typesHolder[ i ] + 1)) ]; strcpy(this->ptrnTypes[ ptIndex ] , typesHolder[ i ]); this->ptrns[ ptIndex ] = new char [ (strlen(ptrnsHolder[ i ] + 1)) ]; strcpy(this->ptrns[ ptIndex ], ptrnsHolder[ i ]);
C Syntax (Toggle Plain Text)
strlen(typesHolder[i]) strlen(ptrnsHolder[i])
Last edited by saishn; Aug 31st, 2006 at 6:50 pm.
![]() |
Similar Threads
- char * = "string" and strcpy = SegFault? (C++)
- Passing char * to function and populating inside (C++)
- void sort2(char* &p, char* &q) (C++)
- help on char pointers (C)
- How to delete a data structure in Builder 6.0? (C++)
- Weird segfault involving popen (C)
Other Threads in the C Forum
- Previous Thread: SOS help
- Next Thread: <map> find function
Views: 3692 | Replies: 1
| Thread Tools | Search this Thread |
Tag cloud for C
* api append array arrays bash binarysearch changingto char character cm copyanyfile copypdffile createcopyoffile createprocess() csyntax database directory drawing dynamic executable execv feet fgets file floatingpointvalidation fork frequency function getlogicaldrivestrin givemetehcodez global graphics gtkwinlinux histogram homework i/o ide include infiniteloop initialization input interest intmain() iso keyboard kilometer lazy license linked linkedlist linux list looping lowest matrix meter microsoft mqqueue mysql oddnumber odf open openwebfoundation overwrite pause pdf pointer pointers posix power program programming pyramidusingturboccodes read recursion recv recvblocked reversing scheduling segmentationfault send single socketprogramming spoonfeeding standard strchr string student suggestions system test testautomation testing unix urboc user whythiscodecausesegmentationfault win32api windowsapi





