Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

farmerdoug

macrumors 6502a
Original poster
Sep 16, 2008
541
0
How do I fix this error or find out about setting the breakpoint?


getline(50051) malloc: *** error for object 0x1002002b8: incorrect checksum for free
d object - object was probably modified after being freed.
*** set a breakpoint in malloc_error_break to debug
Abort trap

The last printf("here") doesn't. The error only appears if int n is too large.

Code:
void spline(float x[], float y[], int n, float yp1, float ypn, float y2[])
/*Given arrays x[1..n] and y[1..n] containing a tabulated function, i.e., yi = f(xi), with
x1 <x2 < :: : < xN, and given values yp1 and ypn for the first derivative of the interpolating
function at points 1 and n, respectively, this routine returns an array y2[1..n] that contains
the second derivatives of the interpolating function at the tabulated points xi. If yp1 and/or
ypn are equal to 1 x 10^30 or larger, the routine is signaled to set the corresponding boundary
condition for a natural spline, with zero second derivative on that boundary. */
{
int i,k;
float p,qn,sig,un,*u;
u= (float *)calloc(n,sizeof (float));
printf("here\n");
if (yp1 > 0.99e30) //The lower boundary condition is set either to be "natural"
y2[0]=u[0]=0.0; 
else {				//or else to have a specified first derivative.
y2[0] = -0.5;
u[0]=(3.0/(x[1]-x[0]))*((y[1]-y[0])/(x[1]-x[0])-yp1);
}
for (i=1;i<=n-2;i++) {		//This is the decomposition loop of the tridiagonal algorithm.	y2 and u are used for temporary
							//storage of the decomposed factors.
sig=(x[i]-x[i-1])/(x[i+1]-x[i-1]);
p=sig*y2[i-1]+2.0;
y2[i]=(sig-1.0)/p;
u[i]=(y[i+1]-y[i])/(x[i+1]-x[i]) - (y[i]-y[i-1])/(x[i]-x[i-1]);
u[i]=(6.0*u[i]/(x[i+1]-x[i-1])-sig*u[i-1])/p;
}

n = n-1;
if (ypn > 0.99e30)     //The upper boundary condition is set either to be "natural"
qn=un=0.0;				//or else to have a specified first derivative.
else {					

qn=0.5;
un=(3.0/(x[n]-x[n-1]))*(ypn-(y[n]-y[n-1])/(x[n]-x[n-1]));
}
y2[n]=(un-qn*u[n-1])/(qn*y2[n-1]+1.0);
for (k=n-1;k>=1;k--)		//This is the backsubstitution loop of the tridiagonal algorithm.
y2[k]=y2[k]*y2[k+1]+u[k]; 

printf("here\n");

free (u);
printf("here\n");

}
 
How do I fix this error or find out about setting the breakpoint?

I see a big problem.

You do a "Malloc" and then never test the return value. What it the malloc failed?
alway, alway test thar every system call did not fail. It is bad to assume they worked. it is easy to run out of memory or fill up a disk or have a network'ed server go down. Alway check after every call.

Try something like this:

Code:
if ( !( u = (float *)calloc (....)) {
   printf(stderr, "calloc failed: %s\n", streror(errno));
   exit(1);
}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.