#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Angry Problem Unknown with C


    Here is a code for calculating Convolution(maths). it perfectly works for the input sequence lengths of 3 and 2 for first two inputs...
    compile and try this:
    Input:
    1. 3
    2. 2
    3. 1 2 3
    4. 1 2

    Final Output:
    Convoluted
    1
    4
    7
    6

    here is the code:
    C Code:
    <div class="bbcode_container">
    	<div class="bbcode_description">Code:</div>
    	<pre class="bbcode_code"style="height:372px;">#include<stdio.h>
    #include<malloc.h>
     
    typedef struct _Matrix{
    	float **mat;
    	int m,n;
    }Matrix;
     
    Matrix* matrix_new_Matrix(int m, int n)
    {
    	Matrix *newMat = (Matrix*)malloc(sizeof(float));
    	newMat->mat = (float*)malloc(sizeof(float)*m);
    	int i = 0;
    	for(i =0 ; i < m ; i++){
    		newMat->mat[i] = (float*)malloc(sizeof(float)*n);
    	}
    	newMat->m = m;
    	newMat->n = n;
     
    	return newMat;
     
    }
     
    Matrix* matrix_multiply(Matrix *A, Matrix *B)
    {
    	if(A->n != B->m){
    		printf("\nMatrices cannot be multiplied");
    		return NULL;
    	}else{
    		Matrix *C = matrix_new_Matrix(A->m,B->n);
    		int i,j,k;
    		for(i = 0 ;i< A->m; i++){
    			for(j=0; j < B->n ; j++){
    				C->mat[i][j] = 0;
    				for( k =0;k < A->n;k++){
    					C->mat[i][j]+=A->mat[i][k] * B->mat[k][j];
    				}
    			}
    		}
    		return C;
    	}
     
    }
     
     
    int matrix_print(Matrix *matA, char* name)
    {
    	int i=0 , j=0;
    	printf("\n%s\n",name);
    	for(i = 0; i < matA->m; i++){
    		for(j = 0; j < matA->n; j++ ){
    			printf("%0.3f\t",matA->mat[i][j]);
    		}
    		printf("\n\n");
    	}
    	return 0;
    }
     
    Matrix *convolve(int *X,int Xn, int *Y,int Yn)
    {
    	int N = Xn+Yn-1;
    	Matrix *A = matrix_new_Matrix(N,Yn);
    	Matrix *B = matrix_new_Matrix(Yn,1);
     
    	matrix_print(A,"A");
     
    	int i,j;
     
    	printf("\nj\ti\t,Xn+i\tA->mat[j][i]\tYn\tN");
    	for(i=0 ; i < Yn; i++ ){
    		for(j=0; j<i; j++){
    			A->mat[j][i] = 0;
    			printf("\n%d\t%d\t%d\t%f\t%d\t%d",j,i,Xn+i,A->mat[j][i],Yn,N);
     
    		}
    		for(j =j; j< Xn+i; j++){
    			A->mat[j][i] = X[j-i];
    			printf("\n%d\t%d\t%d\t%f\t%d\t%d",j,i,Xn+i,A->mat[j][i],Yn,N);
    		}
    		for(j =j; j<N; j++){
    			A->mat[j][i] = 0;
    			printf("\n%d\t%d\t%d\t%f\t%d\t%d",j,i,Xn+i,A->mat[j][i],Yn,N);
    		}
    	}
     	matrix_print(A,"x(n)");
     
    	for(i=0; i< Yn; i++){
    		B->mat[i][0] = Y[i];
    	}
     	matrix_print(B,"h(n)");
     
    	Matrix *C = matrix_multiply(A,B);
    	return C;
    }
     
     
    int main()
    {
     
    	int m,n,i;
    	printf("\nEnter the number of points in first sequence: ");
    	scanf("%d",&m);
    	printf("\nEnter the number of points in second sequence: ");
    	scanf("%d",&n);
     
    	printf("\nEnter the first sequence: \n");
    	int *x = (int*)malloc(sizeof(int)*m);
    	for(i =0; i< m;i++){
    		scanf("%d",&x[i]);
    	}
     
    	printf("\nEnter the second sequence: \n");
    	int *h = (int*)malloc(sizeof(int)*n);
    	for(i =0; i< n;i++){
    		scanf("%d",&h[i]);
    	}
     
    /*	m=4 ; n=3;
    	int x[4] = { 1, 2, 3, 4 };	
    	int h[3] = { 1, 2, 3};	            */
    	Matrix *Y = convolve(x,m,h,n);
     
    	printf("\n\n\n\n");
    	matrix_print(Y, "Convoluted");
    }</pre>
    </div>


    link to download the source:
    https://docs.google.com/file/d/0B616lfmlormnN0hNOUhvOGlzZnc/preview
    Last edited by vanangamudi; November 7th, 2012 at 04:47 AM. Reason: formatting the code
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,417
    Rep Power
    1871
    > #include<malloc.h>
    malloc is declared in stdlib.h

    Code:
    	Matrix *newMat = (Matrix*)malloc(sizeof(float));
    	newMat->mat = (float*)malloc(sizeof(float)*m);
    	int i = 0;
    	for(i =0 ; i < m ; i++){
    		newMat->mat[i] = (float*)malloc(sizeof(float)*n);
    	}
    Well these are all screwed to begin with.
    1. Don't cast the return result of malloc in a C program. In fact, you've even got one of them wrong as well.
    2. The first two sizeof's are wrong as well (both allocate less than you need).

    I take it from your "it perfectly works for...." statement that it doesn't work at all when you increase the sizes. Which makes sense, given that small buffer overruns are masked by the internal padding and alignment that malloc usually uses. It's only when you allocate larger amounts that the flaws are revealed.

    Here, try this
    Code:
    	Matrix *newMat = malloc(sizeof(*newMat));
    	newMat->mat = malloc(sizeof(*newMat->mat)*m);
    	int i = 0;
    	for(i =0 ; i < m ; i++){
    		newMat->mat[i] = malloc(sizeof(*newMat->mat[i])*n);
    	}
    You also need to call free() at some point to release all the memory you allocated.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    2
    Rep Power
    0

    Well thank you


    Originally Posted by salem
    > #include<malloc.h>
    malloc is declared in stdlib.h

    Code:
    	Matrix *newMat = (Matrix*)malloc(sizeof(float));
    	newMat->mat = (float*)malloc(sizeof(float)*m);
    	int i = 0;
    	for(i =0 ; i < m ; i++){
    		newMat->mat[i] = (float*)malloc(sizeof(float)*n);
    	}
    Well these are all screwed to begin with.
    1. Don't cast the return result of malloc in a C program. In fact, you've even got one of them wrong as well.
    2. The first two sizeof's are wrong as well (both allocate less than you need).

    I take it from your "it perfectly works for...." statement that it doesn't work at all when you increase the sizes. Which makes sense, given that small buffer overruns are masked by the internal padding and alignment that malloc usually uses. It's only when you allocate larger amounts that the flaws are revealed.

    Here, try this
    Code:
    	Matrix *newMat = malloc(sizeof(*newMat));
    	newMat->mat = malloc(sizeof(*newMat->mat)*m);
    	int i = 0;
    	for(i =0 ; i < m ; i++){
    		newMat->mat[i] = malloc(sizeof(*newMat->mat[i])*n);
    	}
    You also need to call free() at some point to release all the memory you allocated.
    How did I missed it... thanks for pointing it out.

IMN logo majestic logo threadwatch logo seochat tools logo