|
| |||||||||||||||||||||||||
| vvarma (1) | |
|
I have a piece of code whose skeleton is as follows: //all necessary includes size_t fact(int i){ if (i==0) return(1); else return(i*fact(i-1)); } main(){ //some definitions int* R; const size_t N = 8; const size_t Cmbnts = fact(2*N-1)/((fact(N))*(fact(N-1))); //Call this line Line-ABC vector< vector<int> > basis(Cmbnts, vector<int>(N,0)); // Call this line Line-XYZ for(;;){ //call to function next_combo and a necessary break, which uses neither basis nor Cmbnts //code lines ... R = next_combo(//relevant arguments, none including Cmbnts or basis) cout<<"Checkpoint 2"<<endl; //...more code lines ... } } Now when I comment Line-ABC and Line-XYZ, the program compiles and runs and gives me the output as expected. But if I uncomment either Line-ABC or Line-XYZ (in the former case, I take care to replace Cmbnts appearing in Line-XYZ by a number), the program compiles but gives me a Segmentation Fault, even though neither Cmbnts nor basis are used by the function next_combo. I have included Cmbnts and basis for further expanding the code, but am not currently using them anywhere (that they are not used anywhere is guaranteed because if I simply comment Line-ABC and Line-XYZ, I get the right output). With either line uncommented, the program calculates the value Cmbnts correctly, runs till a single call of next_combo, and throws out SegmentationFault. "Checkpoint 2" does not get printed, but "Checkpoint 1" which is within the last few lines of next_combo does get printed. Also if I remove the empty-for-loop completely, the program runs without any SegmentationFault (without giving me the output that I want also, of course). Any ideas will be appreciated; I have exhausted myself of ideas. | |
|
|
|
| jsmith (5804) | |
|
fact(2*N-1) for N=8 overflows the size_t type. | |
|
|
|