Thread: Segmentation Fault?

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    45

    Segmentation Fault?

    I am writing some code that uses a function twice in order to create two matrices, each stored under a different variable name. However, when I try to use the function the second time, I get a segmentation fault. If I remove the first time the function is used, however, the code works fine. What can be wrong?

    The way I am approaching this is by using a structure and a double pointer for each variable. I am allocating memory for each double pointer and (one at a time) sending it to the second function. That function sends my pointer to another function and returns the new values stored in the double pointer to the original function. I tried using a pointer to my double pointer and reallocating memory in the second function as well as messing with how the pointer is being sent, but nothing is working. What could be a reason that the fault occurs only if the function is used twice?

    I can post code if the question can't be answered without it...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Post your code.
    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.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Almost anything can be wrong, the only way to tell what is wrong is by looking at the code. Since my clear plastic ball (cheaper, but not quite as good as a crystal ball) is not always accurate, I'm not going to guess what the problem is.

    --
    mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    It's a lot...

    Main Function
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include"corrheader.h"
    
    struct
    {
    	int PAS_type, cluster_number;
    	double *d_norm, *delta_phi_deg, *amplitude_cluster, *phi_deg, *AS_deg, *Q, *sigma_deg, **R;
    } Node_B, UE;
    
    
    int main()
    {
    
    int i,j, dlink=-1, Field=-1, M, N;
    
    
    /*---------------------------------------------------------------------------------*/
    
    *Node_B.R=malloc(M*sizeof(double*));
    
    	for(i=0;i<M;i++)
    	Node_B.R[i]=malloc(M*sizeof(double));
    
    
    *UE.R=malloc(N*sizeof(double*));
    
    	for(i=0;i<N;i++)
    	UE.R[i]=malloc(N*sizeof(double));
    
    
    
    /*****************THESE VALUES ARE ALL OBTAINED FROM ANOTHER FUNCTION, BUT POSTING ALL THAT WOULD BE EXCESSIVE**********/
    
    
    
    /*SECOND FUNCTION USED 1ST TIME, NO PROBLEMS OCCUR*/
    correlation(M, Node_B.PAS_type, Node_B.cluster_number, Field, Node_B.d_norm, Node_B.delta_phi_deg, Node_B.amplitude_cluster, Node_B.phi_deg, Node_B.AS_deg, 
    
    Node_B.Q, Node_B.sigma_deg, Node_B.R);
    
    
    /*FUNCTION USED SECOND TIME CREATES A PROBLEM*/
    correlation(N, UE.PAS_type, UE.cluster_number, Field, UE.d_norm, UE.delta_phi_deg, UE.amplitude_cluster, UE.phi_deg, UE.AS_deg, UE.Q, UE.sigma_deg, UE.R);
    
    
    printf("\n\nUE.R\n");
    for(i=0;i<N;i++)
    {for(j=0;j<N;j++)
    {
    	printf("&#37;.4lf\t", UE.R[i][j]);
    }printf("\n");
    }
    
    
    return 0;
    }
    Correlation (Second Function):
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    
    #define Pi 3.14159265
    
    
    
    double correlation(int M, int PAS_type, int cluster_number, int Field, double *d_norm, double *delta_phi_deg, double *amplitude_cluster, double *phi_deg, 
    
    double *AS_deg, double *Q, double *sigma_deg, double **R)
    {
    
    
    /* Normalization*/
    
    
    
    	int i, j, k;
    	
    	double *Rxx=malloc(M*sizeof(double));
    	double *Rxy=malloc(M*sizeof(double));
    	double *tmp=malloc(M*sizeof(double));
    
    	if( Rxx==NULL || Rxx==NULL || B==NULL)
    	printf("Mem. All. failed!");
    	
    
    	if (M>1)
    	{
    
    		for(i=0;i<M;i++)
    		{
           			Rxx[i] = bessel(B[i]);
           			Rxy[i] = 0;
    		}
    
    			printf("\ntmp:\n");
    		      	for(i=0;i<M;i++)
    			{
    				tmp[i] = (Rxx[i]*Rxx[i])+(Rxy[i]*Rxy[i]);
    				printf("%.4lf\t", tmp[i]);
    			}
    		        
          /*I BELIEVE THE PROBLEM OCCURS IN THE FOLLOWING FUNCTION*/
    		**R=toeplz_vec(M, tmp, R);
    	
    
    
    	}
    
    	free(tmp_real); free(Rxx); free(Rxy);
    	
    
    return 0;
    
    
    }
    toeplz_vec (Third Function):
    Code:
    #include<stdio.h>
    
    double toeplz_vec(int size, double *tmp, double **R)
    {
    		
    
    	int i, j, k;
    
    	/*******FROM WHAT I CAN TELL BY TESTING USING PRINTF, THE ERROR OCCURS SOMEWHERE WITHIN THIS LOOP******/
    	for(k=0;k<size;k++)
    	{
    		for(i=0;i<size;i++)
    		{
    			for(j=0;j<size;j++)
    			{
    				if(i==j)
    				R[i][j]=tmp[0];
    				else if(i==(j-k)||i==(j+k))
    				R[i][j]=tmp[k];
    						
    			}
    		}
    	}
    
    
    return **R;
    }

    Sorry for so much code.
    Last edited by magda3227; 07-08-2008 at 10:47 AM.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Have you been snipping the code?

    > *Node_B.R=malloc(M*sizeof(double*));
    Because the 1st line is a problem waiting to happen.
    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.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Yeah, sorry. I removed that. M is a user defined value returned from a previous function that I took out because this program is huge. At the time when I use malloc, M has a non-zero value.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well if the program is large, the problem could be anywhere (and probably is).

    Unfortunately for you, the code where it crashes looks OK (and probably will be when you find the real cause).

    Any mistake earlier on in the run-time of the program can have a delayed effect (from microseconds to years) before the problem shows up in a completely different part of the program.

    If you have tools like valgrind and electric fence to hand, I suggest you use them to get closer to the real cause of the problem.

    You could also try
    - assign all pointers to NULL when you declare them
    - assign all pointers to NULL as you free them
    - make sure all other variables are initialised.

    Compile with as many warnings enabled as possible.
    gcc -W -Wall -ansi -pedantic -O2
    would be a good set.
    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.

  8. #8
    Registered User
    Join Date
    Apr 2008
    Posts
    19
    Are you sure that free(tmp_real) is correct? It looks like it should be free(tmp).

  9. #9
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Thanks. I'll make sure to compile using -wall and make the changes you suggested.

  10. #10
    Registered User
    Join Date
    Jun 2008
    Posts
    45
    Not that anyone would care, but I found the error. it had to do with looping past a pointer. I was trying to access something that wasn't there. I'm not sure why it didn't show up until the second time around though.

    I wish there was a way for compilers to catch these kinds of mistakes. It's one of those hard to find kind of errors. I'm glad I caught it though.

    Thanks for all the help.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by magda3227 View Post
    I wish there was a way for compilers to catch these kinds of mistakes. It's one of those hard to find kind of errors. I'm glad I caught it though.
    You can do it yourself by knowing the size.
    Even better, in C++, you can write a container that does the check for you everytime.
    But in C, it's manual labor.
    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. Segmentation fault problem
    By odedbobi in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2008, 03:36 AM
  2. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  3. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  4. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM