Thread: Return of the multiplication function

  1. #1
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10

    Return of the multiplication function

    Hello, I'm beginner in C, use kdevelop -> Qt: 3.3.6 KDE: 3.5.4-Red Hat 25.el5.centos.1 KDevelop: 3.3.4
    and the compiler is gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)

    My doubt is that I made a function (mult_mat_vet) to multiply a matrix (9x9) and a vector (9) and I couldn't return the resulting vector multiplication, within the function I can visualize the result of multiplying, but when the program exits the function tells me that great message: "segmentation fault", I need the y vector to make other important things, sending down the algorithm, summarized, and look forward to your help. Thanks in advance.

    Code:
    #include<stdio.h>
    #include<math.h>
    #include<stdlib.h>
    
    float *allocate_vector (int n)
    {
    	float *v; 
    	if (n<1) {
    	printf("ERROR INVALID PARAMETER!!!");
    	return (NULL);
    	}
    	v=(float *)malloc(n*sizeof(float));
    	if (v==NULL){
    	printf("ERROR - NO MEMORY");
    	return(NULL);
    	}
    	else //printf("\nOK!!!\n");
    	return (v);
    }
    
    float **allocate_matrix(int m,int n)
    {
    	float **v; 
    	int i;	
    	if(m<1||n<1)
    	{	
    		printf("\nINVALID PARAMETER\n");
    		return(NULL);
    	}
    	//aloca as linhas da matriz
    	v=(float **)calloc(m,sizeof(float *));
    	if(v==NULL)
    	{
    		printf("ERROR - NO MEMORY!!");
    		return(NULL);
    	}
    	for (i=0;i<m;i++)
    	{
    		v[i]=(float *)calloc(n,sizeof(float)); 
    		if (v[i]=NULL)
    		{
    			printf("ERROR - NO MEMORY!!");
    			return(NULL);
    		}
    	}
    	return(v); 
    }
    
    float **read_matrix(int *nlin, int *ncol) {
      FILE  *fr;
      float **matriz;
      int i, j;
    
      fr = fopen ("ridge.txt", "r");
    
      if (fr == NULL) {
        perror("ridge.txt: ");
        exit(-1);  
      }
      
      matriz = (float **) calloc (*nlin, sizeof(float*));
      
      for (i = 0; i < *nlin; i++)
        matriz[i] = (float *) calloc(*ncol, sizeof(float));
    
      for (i = 0; i < *nlin; i++)
        for (j = 0; j < *ncol; j++)
        fscanf(fr, "%f", &matriz[i][j]);
    
      fclose(fr);
    
    
      return matriz;
    
    }
    
    
    float *mult_mat_vet(int lin,int col,int linvet, float **mat,float *vetin)
    {
    	int i,j,k;
    	float *vetout;
    	vetout=allocate_vector(9);
    	for(i=0;i<lin;i++)
    	{
    		for(j=0;j<col;j++)
    			vetout[i]=vetout[i]+mat[i][j]*vetin[j];
    	printf("\nvector %f",vetout[i]); //JUST TO TEST THE MULTIPLICATION!!
    	}
    	return(vetout) ;
    }
    
    
    main()
    {
    	
    	float *y,**A;
    	int nlin,ncol;
    	/*SYNTHECT VECTOR*/
    	float Ap[9]={1,0.7,0.2,0.62,1.2,0.9,1,1.08,1.2};
    	
    	nlin=9;
    	ncol=9;
    
          A = read_matrix(&nlin, &ncol);
    
    	p=Ap;
    	
    	y=mult_mat_vet(9,9,9,A,p);
    	printf("\n Don't get here!!!!!!!!!");
    
    //...
    //..
    //. 
    //should continue with others commands
    return
    }

  2. #2
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Code:
    p=Ap;
    I don't see where 'p' is defined. You should have gotten a compiler error there.
    Also you're expecting Ap to be a pointer like any other. This may be a problem because it's not allocated in the same heap space as those which used malloc. But maybe I'm just guessing.

    In mult_mat_vet(), vetout has not been initialized. So when you start summing in values, the original element may be garbage.
    Last edited by nonoob; 10-08-2010 at 12:47 PM.

  3. #3
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10
    Sorry, this isn't the problem, in the original code p is defined like float *p, with *y and **A in the main, I just forgot to put here.

    I'd allocated vetout in the begin of the function mult_mat_vet(), could you explain more the second statement?
    Last edited by figura; 10-08-2010 at 12:51 PM.

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    After you allocate memory for vetout, its elements are some unpredictable values. Summing into them is dangerous. The results could be anything. This wouldn't normally account for segmentation faults though. So let's fix one problem at a time.

  5. #5
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10
    When I printed the vector vetout I could see it.

    So what should I do?

  6. #6
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Hmmm, then I have to compile this myself. I will try if I don't get distracted with work.

  7. #7
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10
    Please, help me if you can.

  8. #8
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Funny, my compiled code runs perfectly. I don't understand it.

    However in your function allocate_matrix(), which is not called in your example, you have
    Code:
    if (v[i]=NULL)
    . You meant to use '=='. Otherwise, you're assigning NULLs and wrecking your allocated pointers.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    I think the control is getting to back the main function. Something is not write after that i think. Is it possible to show us a bit more snap shot of your main function?

    I tried compiling as well. Seems to work fine! :-/

    ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,663
    > and the compiler is gcc version 4.1.2 20080704 (Red Hat 4.1.2-48)
    If you use something like
    gcc -W -Wall -ansi -pedantic -O2 prog.c

    You would have got an error message for if (v[i]=NULL), and a few other things besides.

    Compile for MAXIMAL warnings, and don´t even think of running code until you´ve understood and fixed all the warnings.
    And I do mean fix, rather than "fix by adding some hack just to shut the compiler up".

    Some warnings you can ignore, with experience, for a short period of time (for one test say). But generally, you should try to achieve a long term zero warnings in your code.
    If for no other reason that it´s easy to spot when the new warning you should be paying attention to suddenly appears!


    > v=(float *)malloc(n*sizeof(float));
    See the FAQ on casting malloc
    Cprogramming.com FAQ > Casting malloc
    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.

  11. #11
    Registered User
    Join Date
    Oct 2010
    Location
    Niterói - Brasil
    Posts
    10
    The code already complies before but gave problems when running, but now WORKED !!!!!

    I changed two things:
    when I was allocating the matrix was

    Code:
    if (v[i]=NULL)
    and now is:

    Code:
    if (v[i]==NULL)
    And the other thing was not pass nlin and ncol as pointers and only as integers, these two things have changed and now the code is ok, someone explains to me why?

    Grateful

  12. #12
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,834
    Since you never called allocate_matrix() in the program you posted, fixing the v[i]=NULL problem shouldn't have mattered. I also don't see where you passed nlin and ncol erroneously. You must be working from another version of the code.

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You're not freeing what you allocate...
    Plus main is lacking a return type.
    SourceForge.net: Implicit main - cpwiki
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 01:49 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM