Results 1 to 7 of 7
Hi All,
I'm a beginner in C and C++, and I get a "segmentation error" problem when I run my code.
My code uses some library, that has some header ...
- 07-30-2009 #1Just Joined!
- Join Date
- May 2009
- Posts
- 14
Segmentation Error
Hi All,
I'm a beginner in C and C++, and I get a "segmentation error" problem when I run my code.
My code uses some library, that has some header files, and some .cpp files. The library basically helps me in getting a random number from a uniform discrete distribution.
My code (a file called BigInstance.cpp) and the .cpp and .h files of the library I'm using are all included in the attached .zip file.
Also, the commands I use for compilation and running my project are:
When I compile it, I get some warnings of converting some floats to integers, which, I think, is not a problem for me!Code:g++ -g extreal.cpp hist.cpp myexcept.cpp newran.cpp BigInstance.cpp -o myprogram ./myprogram
And for convenience, the contents of my main code are:
Any help will be really appreciated!Code:#include <fstream> #include <iostream> #include <stdio.h> #include <string> #include <cstdlib> using namespace std; #define WANT_STREAM #define WANT_MATH #define WANT_TIME #include "include.h" #include "newran.h" #include "tryrand.h" #ifdef use_namespace using namespace NEWRAN; #endif #define N 3499 #define start 0 int main (void) { //int tmp; //int s; //int count; ofstream fout("input.dat"); Real probs[N]; Real val[N]; for(int i=0;i<N;i++) { probs[i]=1/(float)N; val[i]=start+i; } DiscreteGen discrete(N,probs,val); srand ( time(NULL) ); double mySeed=(double)rand()/RAND_MAX; //cout<<mySeed<<endl; discrete .Set (mySeed); //Sleep(2000); int a[4000][3900]; //for (ttt=0;ttt<100;ttt++){ //fout<<"["; ////discrete.Set(0.02); //for (uuu=0; uuu<1000; uuu++){ //fout << discrete.Next() <<" "; //} //fout<<"]"; //} //fout<<"];"; int i=0; while(i<1000){ fout<<"["; int k=discrete.Next(); int j=discrete.Next(); int p=discrete.Next(); int l=discrete.Next(); int v=discrete.Next(); for (int e=0;e<3500;e++){ if(e==k || e==j || e==p ||e==l ||e==v){ a[i][e]=1; } else { a[i][e]=0; } fout<<a[i][e]; fout<<" "; } fout<<"] "; fout<<"["; int t=discrete.Next(); int y=discrete.Next(); int q=discrete.Next(); int s=discrete.Next(); int z=discrete.Next(); int w=discrete.Next(); for (int e=0;e<3500;e++){ if(e==t || e==y || e==q ||e==s ||e==z || e==q){ a[i+1][e]=1; } else { a[i+1][e]=0; } fout<<a[i][e]; fout<<" "; } fout<<"] "; fout<<"["; int mm=discrete.Next(); int jj=discrete.Next(); int pp=discrete.Next(); int ll=discrete.Next(); int vv=discrete.Next(); int gg=discrete.Next(); for (int e=0;e<3500;e++){ if(e==mm || e==jj || e==pp ||e==ll ||e==vv || e==gg){ a[i+2][e]=1; } else { a[i+2][e]=0; } fout<<a[i][e]; fout<<" "; } fout<<"]"; i=i+3; } return 0; }
Thanks in advance!
Aly
- 07-30-2009 #2
Segmentation fault means your application tries to access a memory address it isn't allow to access. Maybe you try to write or read from an array with the wrong index.
For example when the array has 1000 entries but you try to access the 1001th.
In order to debug the program you have to single out the offending line of code.
Use text outputs at strategic places to trace the execution flow of the program.
Like
Compile, start and see how far your program executes. Then iterate until you have enclosed the offending line.for(int i=0;i<N;i++)
{
cerr << "Entering for loop with i = " << i << endl;
...
cerr << "Leaving for loop with i = " << i << endl;
}Debian GNU/Linux -- You know you want it.
- 07-30-2009 #3Just Joined!
- Join Date
- May 2009
- Posts
- 14
Thanks for your reply.
Actually, it seems like it breaks from the beginning of it!! I don't know of any wrong array indices that I try to access!
Which part of the code, do you think, is the problem?
Aly
- 07-30-2009 #4
That is just one example of a segmentation fault. Other examples are attempting to dereference a NULL pointer, using a C string that isn't properly terminated, or basically anything that accesses memory that was not allocated.
Segmentation faults are difficult to debug. One more advanced way to do this is to obtain a core dump of the program, and run it through gdb. However, the simplest way is what GNU-Fan suggested: add cerr statements to your program at several points and see if the line prints successfully. If it does, you know the segmentation fault occurs after that. If it doesn't, you know the segfault occurs before that.DISTRO=Arch
Registered Linux User #388732
- 08-07-2009 #5Linux Guru
- Join Date
- Apr 2009
- Location
- I can be found either 40 miles west of Chicago, or in a galaxy far, far away.
- Posts
- 8,974
1. Drop the boolean.h header - no c++ compiler worthy of the name these days will lack the bool type. Defining boolean type info would only be necessary for compilers older than about 10 or more years, so unless you plan on porting this to a now antique system, don't bother.
2. In your ExtReal class, you are not initializing the member 'v' in your constructors - this is very bad. Also, you are initializing the 'c' member inside the body of the constructor instead of the initializer list. This is not good practice.
3. You should define a copy constructor, assignment operator, and destructor for class ExtReal.
I haven't looked at the .cpp files for type conversion or other issues due to the time required to analyze this (clients pay me $200 USD / hour for the privilege). However, I have very briefly looked at the test application you provided and there are a number of type conversion problems where you are using integer values for elements that should be floats. IE:In any case, there are a number of other issues here. You need to review your code with a much more critical eye in order to catch the more egregious issues to be found. I haven't analyzed the code in any detail beyond what I mention, but I have to believe that these are only the tip of the iceberg.Code:#define start 0 // This should be #define start 0.0 // or #define start ((float)0) // and for(int i=0;i<N;i++) { probs[i]=1/(float)N; val[i]=start+i; } // should be for(int i=0;i<N;i++) { probs[i]=1.0/(float)N; val[i]=start+(float)i; }Sometimes, real fast is almost as good as real time.
Just remember, Semper Gumbi - always be flexible!
- 08-07-2009 #6Just Joined!
- Join Date
- Jul 2009
- Posts
- 2
You should also correct the declaration of main. It should be
int main(int argc, char *argv[])
- 08-08-2009 #7


Reply With Quote
