C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-13-2009, 01:22 PM   #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;
	}
}
a o destruction is offline   Reply With Quote
Old 08-13-2009, 01:31 PM   #2
cas
Registered User
 
Join Date: Sep 2007
Posts: 372
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.
cas is offline   Reply With Quote
Old 08-13-2009, 01:33 PM   #3
and the hat of vanishing
 
Salem's Avatar
 
Join Date: Aug 2001
Location: The edge of the known universe
Posts: 21,214
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.
Up to 8Mb PlusNet broadband from only £5.99 a month!
Salem is offline   Reply With Quote
Reply

Tags
array, pointer, segmentation fault

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Segmentation fault bennyandthejets C++ Programming 7 09-07-2005 05:04 PM
Segmentation fault NoUse C Programming 4 03-26-2005 03:29 PM
Locating A Segmentation Fault Stack Overflow C Programming 12 12-14-2004 01:33 PM
Annoying Segmentation Fault Zildjian C++ Programming 7 10-08-2004 02:07 PM
Segmentation fault... alvifarooq C++ Programming 14 09-26-2004 12:53 PM


All times are GMT -6. The time now is 12:12 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22