Runtime Error : load of misaligned address...


  • 0
    W

    I encounter this type problem many times, I don't know the reason, how to handle with it?
    My C code

    int b_search(int a[], int n, int key){
    	int r_l=0;
    	int r_r=n-1;
    	int r_m=(r_l+r_r)/2;
    	
    	if(key<a[r_l]||key>a[r_r]){
    		return 0;
    	}
    	while(r_l<r_r){
    		if(a[r_m]<key){
    			r_l=r_m+1;
    			r_m=(r_l+r_r)/2;
    		}
    		else if(a[r_m]>key){
    			r_r=r_m-1;
    			r_m=(r_l+r_r)/2;
    		}
    		else{
    			return 1;
    			break;
    		}
    	}
    	return 0;
    }
    int b_search_m(int **a, int m, int n, int key){
    	if(m==1){
    		return b_search(a[0],n,key);
    	}
    	else if(n==1){
    		int r_l=0;
    		int r_r=m-1;
    		int r_m=(r_l+r_r)/2;
    		int pos_r=-1;
    		if(key<a[r_l][0]||key>a[r_r][0]){
    			return 0;
    		}
    		while(r_l<r_r){
    			if(a[r_m][0]<key){
    				r_l=r_m+1;
    				r_m=(r_l+r_r)/2;
    			}
    			else if(a[r_m][0]>key){
    				r_r=r_m-1;
    				r_m=(r_l+r_r)/2;
    			}
    			else{
    				return 1;
    			}
    		}
    		if(pos_r==-1){
    			return 0;
    		}
    		return 0;
    	}
    	else{
    		int r_l=0;
    		int r_r=m-1;
    		int r_m=(r_l+r_r)/2;
    		int pos_r=-1;
    		if(a[r_l][0]>key||a[r_r][0]<key){
    			return 0;
    		}
    		while(r_l<r_r){
    			if(a[r_m][0]<key){
    				r_l=r_m+1;
    				r_m=(r_l+r_r)/2;
    			}
    			else if(a[r_m][0]>key){
    				r_r=r_m-1;
    				r_m=(r_l+r_r)/2;
    			}
    			else{
    				pos_r=r_m;
    				break;
    			}
    		}
    
    		if(pos_r!=-1){
    			return 1;
    		}
    		if(a[r_r][0]<key){
    			return b_search(a[r_r],n,key);
    		}
    		else{
    			return b_search(a[r_r-1],n,key);
    		}
    	}
    
    	return 0;
    }
    bool searchMatrix(int** matrix, int matrixRowSize, int matrixColSize, int target) {
       int ret=b_search_m(matrix,matrixRowSize,matrixColSize,target);
       if(ret){
       	return true;
       }
       return false;
    }
    
    

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.