Thread: Segmentation Fault

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    1

    Question Segmentation Fault

    Hello.

    I apologize if this topic has been covered somewhere else, but I thought that it might be best to post my code. I was struggling with pointer types earlier, and this page http://cboard.cprogramming.com/756650-post4.html helped me to find a solution to that. But I am still getting a segmentation fault, and I don't know what's causing it. I am sort of new to C (I knew some C++, but I wasn't great at that either), and the whole double pointer thing is a bit confusing. I know that I'm trying to get to memory that I don't have, but I don't know where.

    Thanks

    This is the program, in its entirety:
    Code:
    /* This is a single body test simulation */
    
    #include <stdio.h>
    #include <math.h>
    #include <stdlib.h>
    
    #define G 6.7*pow(10,-11)
    #define m_s 2*pow(10,5)		/* Mass of the sun, in units of 10^25 kg */
    #define t_max pow(10,8)		/* stop time */
    #define h 1			/* stepsize */
    
    
    double xdot(double **arrayptr, double delta, short j, unsigned int i);  	/* an easy reference for the first order equations */ 
    void RungeKutta4(double**, unsigned int i);		     	/* iteration for forth order (4 step) method */
    
    
    int main()
    {
    
    const unsigned int n = t_max/h;			/* array size */
    
    double** data;				/* this holds the datas */
    
    unsigned int i;						/* loop index */
    
    FILE *fp;
    
    data = malloc(4*sizeof(double*));
    for(i=0; i<n; ++i)
    	data[i] = malloc( n*sizeof(*data[i]) );
    
    data[0][0] = 0;					/* initial earth x-position */
    data[1][0] = 1.5 * pow(10,10);			/* initial earth y-position */
    data[2][0] = 3 * pow(10,-6);			/* initial earth x-velocity */
    data[3][0] = 0;					/* initial earth y-velocity */
    
    
    for(i=0; i<n; ++i)
    	RungeKutta4(data, i);
    
    fp = fopen("single_body.txt", "w+");
    fwrite(data, sizeof(double), 4*n*sizeof(double)/sizeof(double), fp);
    
    return 0;
    }
    
    
    double xdot(double **data0, double delta, short j, unsigned int i)	/* returns a first order ODE from the system of ++j equations */
    {
    	if(j<2)
    		return (data0[j+2][i] + delta);
    	else 					/* |r|^3 is calculated twice, but it's a bit messy if you don't */
    		return G * m_s * (-(data0[j-2][i] + delta) )/pow(sqrt( pow(-(data0[j][i] + delta),2) + 
    		pow(-(data0[j][i] + delta), 2) ), 3);
    }
    
    void RungeKutta4(double **datai, unsigned int i)	/* a single 'four'-step iteration  */
    {
    	short j;
    	double s1, s2, s3, s4;
    	
    	for(j = 0; j<4; ++j)
    	{
    		s1 = xdot(datai, 0, j, i);
    		s2 = xdot(datai, h*s1/2, j, i);
    		s3 = xdot(datai, h*s2/2, j, i);
    		s4 = xdot(datai, h*s3, j, i);
    		
    		datai[j][i+1] = datai[j][i] + h*(s1 + 2*s2 + 2*s3 +s4)/6;
    	}
    }

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Code:
    data = malloc(4*sizeof(double*));
    for(i=0; i<n; ++i)
      data[i] = malloc( n*sizeof(*data[i]) );
    Here you allocate 4 pointers to double, but then you try to access from 0 to n (which is decidedly larger than 4). Presumably you meant to loop from 0 to 3.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    1. Learn to indent your code better. Frankly, this is a mess, and if it were any larger it would be completely unreadable. A consistent indent makes reading code, preventing bugs and finding bugs so much easier

    2. #define G 6.7*pow(10,-11)
    Try
    #define G 6.7E-11

    3. fwrite(data, sizeof(double), 4*n*sizeof(double)/sizeof(double), fp);
    You have 5 malloc calls, which means 5 discontinuous memory blocks.
    You can only save each malloc'ed pointer in a single fwrite call.

    4. data = malloc(4*sizeof(double*));
    n would appear to be a LOT larger than 4
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Segmentation fault
    By bennyandthejets in forum C++ Programming
    Replies: 7
    Last Post: 09-07-2005, 05:04 PM
  2. Segmentation fault
    By NoUse in forum C Programming
    Replies: 4
    Last Post: 03-26-2005, 03:29 PM
  3. Locating A Segmentation Fault
    By Stack Overflow in forum C Programming
    Replies: 12
    Last Post: 12-14-2004, 01:33 PM
  4. Annoying Segmentation Fault
    By Zildjian in forum C++ Programming
    Replies: 7
    Last Post: 10-08-2004, 02:07 PM
  5. Segmentation fault...
    By alvifarooq in forum C++ Programming
    Replies: 14
    Last Post: 09-26-2004, 12:53 PM

Tags for this Thread