The code compiles but I get a checksum error for freed pointer when I run.
Just a clue as to what's going on or how to debug this.
Thanks.
You debug this by testing it.
That means writing test programs that call the function being tested, and then do some checks to confirm it isn't overrunning anything or otherwise malfunctioning.
You should also write tests that confirm the function is doing its calculation correctly, for known-good inputs and outputs. That may be difficult for large data-sets, but you should be able to come up with smaller data-sets that define known inputs and outputs.
Example of a simple test program. I have no idea if the input data is meaningful or not. It's just to show some of the things you need to do when writing tests.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//#include <math.h>
void spline(const float x[], const float y[], int n, float yp1, float ypn, float y2[]);
// Any finite easily recognizable value unlikely to occur naturally.
static const float sentinel = -6.022141e23f;
static float*
floats( int count )
{
// allocates count + 1 floats, puts sentinel in last position.
float *floats = (float *) calloc( count + 1, sizeof(float) );
if ( floats )
floats[ count ] = sentinel;
return floats;
}
static void
floatCheck( float* floats, int count, char *msg )
{
if ( floats )
{
if ( floats[ count ] != sentinel )
fprintf( stderr, "damaged %s\n", msg );
}
}
static void
freeSafe( void * ptr )
{ if ( ptr ) free( ptr ); }
static float*
xlinear( int count )
{
float *array = floats( count );
if ( array )
{
int i;
for ( i = 0; i < count; ++i )
{ array[ i ] = (float) i; }
}
return array;
}
static float*
xsquared( int count )
{
float *array = floats( count );
if ( array )
{
int i;
for ( i = 0; i < count; ++i )
{ array[ i ] = (float) i * i; } // i squared
}
return array;
}
int main (int argc, const char * argv[])
{
const int N = 20;
float * xx = xlinear( N );
float * yy = xsquared( N );
float * yy2 = floats( N ); // zeros
if ( xx && yy && yy2 )
{
spline( xx, yy, N, 20.0f, 30.0f, yy2 );
floatCheck( xx, N, "xx" );
floatCheck( yy, N, "yy" );
floatCheck( yy2, N, "yy2" );
}
else
printf( "Something wasn't allocated.\n" );
freeSafe( xx );
freeSafe( yy );
freeSafe( yy2 );
return 0;
}
By the way, running this test program doesn't crash or malfunction, so my first guess is that you're doing something else wrong, probably in the unposted code that leads up to calling spline().
If you have more plausible sample data for inputs, I urge you to write a suitable test program and test your spline function yourself.
Your goal isn't just to write code, it's to demonstrate that the code you write works as expected.
I can tell you from experience that sometimes just
the process of writing tests is enough to find bugs; running the test just confirms that the bug was fixed.
If writing tests seems like a waste of time, consider how much time you waste debugging faulty code by posting here and asking others to find your bugs. Not just your time, but lots of other people's, too. Personally, I may stop commenting on your posts, even when I know the answer, unless you provide at least a first attempt at a test program.
Unless something changes, nothing will change how you produce code. Realistically, this can't go on forever.