segfault

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • haniep
    New Member
    • Mar 2012
    • 1

    segfault

    i dont know what should i do whit this segmentation fault.....plz help me!!!

    Code:
    #include <cstdlib>
    #include <ctime>
    #include<iostream>
    #include<fstream>
    using namespace std;
    int random(bool[][1000],int , int);
    int main()
    {
    	int n;
    	int counter=2;
    	cout<<"enter your number"<<endl;
    	//getline(cin , n);
    	bool array [1000][1000];
    	cin >> n;
    	for(int i=0; i<1000; i++){
    		for(int p=0; p<1000; p++){
    			array[i][p]=false;
    			//cout<<"ss";
    		}
    	}
    	cout << "inadlkv";
    	cout<<"chand ta jaygasht(bishtar az n! nabayd bashe!!!)";
    	for(int k=0; k<counter; k++){
    		cout<< "jaygashte  ";
    		for(int i=0; i<n; i++){
    			cout<<random(array , n, i)<<"   ";
    		}
    	}
    			return 0;
    }
    int random(bool array[][1000] , int n , int i) {
    	srand(time(NULL));
    	int a=rand() % 1000 ;
    	for(int j=0; j<n ; j++){
    		if( (!array[i][j])&&(j==a) ){
    			array[i][j]=true;
    			return(a);
    		}
    		else
    			return(random (array , n , i));
    	}
    }
    Last edited by Stewart Ross; Mar 10 '12, 06:37 PM. Reason: Removed zip file and transferred code from the file to the question
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You are accessing memory that does not belong to your variable. You have to fix it. Most likely this is an access outside an array due ti an error in your code or to using C-strings. Be sure all your buffers are large enough. You may have to step through the code using your debugger.

    Comment

    Working...