| | |
strcmp seg faulting in a loop
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2008
Posts: 62
Reputation:
Solved Threads: 0
Hi,
Here's my code
my featFile is of the form
and my trainFIle is of the form
so i basically have to read the training file and replace the first name with an array index...the array index is formed using the featFile...the call to cIndex keeps seg faulting after some iterations.
Here's my code
C++ Syntax (Toggle Plain Text)
#include<iostream> #include<fstream> #include<string.h> #include<math.h> #include<stdlib.h> #include<stdio.h> using namespace std; int cIndex(char *x,char **featlist,int len) { int i; for(i=0;i<len;i++) if(strcmp(x,featlist[i])==0) return i; } int main(int argc,char *argv[]) { std::ifstream featFile(argv[1],ios::in); std::ifstream trainFile(argv[2],ios::in); // std::ofstream svmFile(argv[3],ios::out); char line[20000]; char feat[100]; char **featlist; int i=0,len=0,fl=0,j; char* x; int valnum; while(featFile.good()) { featFile.getline(feat,100,'\n'); if(featFile.eof()) break; len++; } featFile.clear(); featFile.seekg(0,ios::beg); // cout<<len<<endl; featlist=new char*[len]; for (i = 0; i < len; ++i) featlist[i] = new char[100]; i=0; while(featFile.good()) { featFile.getline(feat,100,'\n'); if(featFile.eof()) break; strcpy(featlist[i],feat); i++; } while(trainFile.good()) { trainFile.getline(line,20000,'\n'); x=strtok(line," "); valnum=cIndex(x,featlist,len); cout<<valnum<<";"; } trainFile.close(); }
my featFile is of the form
C++ Syntax (Toggle Plain Text)
yasuhiro ali mdbl consum dollar unrest protest mile mill ventur semiconductor heller authoris
and my trainFIle is of the form
C++ Syntax (Toggle Plain Text)
acq 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0 24:0 25:0 26:0 27:0 28:0 29:0 30:0 31:0 32:0 33:0 34:0 acq 1:0 2:0 3:0 4:0 5:0 6:0 7:0 8:0 9:0 10:0 11:0 12:0 13:0 14:0 15:0 16:0 17:0 18:0 19:0 20:0 21:0 22:0 23:0 24:0 25:0 26:0 27:0 28:0 29:0 30:0 31:0 32:0 33:0 34:0
so i basically have to read the training file and replace the first name with an array index...the array index is formed using the featFile...the call to cIndex keeps seg faulting after some iterations.
> The function
> Your code compiled and ran fine for me (what OS and compiler are you using?)
> Running your code gave me the following output:
> I can't seem to find any place in your code where you're deallocating the assigned memory to
...
cIndex isn't always returning a value: C++ Syntax (Toggle Plain Text)
int cIndex(char *x,char **featlist,int len) { int i; for(i=0;i<len;i++) if(strcmp(x,featlist[i])==0) return i; /* So what happens when featlist[i] and x aren't equal ? */ /* You should add a return code to check whether they were equal or not */ /* Let's say if they weren't equal you return -1 for example */ }
> Your code compiled and ran fine for me (what OS and compiler are you using?)
> Running your code gave me the following output:
-7;-7; , is that correct? (I'm using the same file contents for featFile and trainFile as you provided)> I can't seem to find any place in your code where you're deallocating the assigned memory to
featlist , that's a serious memory leak
... Last edited by tux4life; Apr 26th, 2009 at 6:06 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
To release the allocated memory (of your
So, no more memory leaks
featlist ): C++ Syntax (Toggle Plain Text)
for(int i = 0; i < len; i++) delete[] ptr[i];
Last edited by tux4life; Apr 26th, 2009 at 6:20 am.
"Never argue with idiots, they just drag you down to their level and then beat you with experience."
![]() |
Other Threads in the C++ Forum
- Previous Thread: reading a mathematical expression from a file
- Next Thread: problem c++ (My Pi)
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char class classes code compile compiler console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray encryption error file forms fstream function functions game getline givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number output parameter pointer problem program programming project proxy python random read recursion recursive reference return rpg string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






