I'm trying to create a function that converts a custom structure - Spline - to a 2-dimension array of doubles. The problem is that although the function works fine and stores the correct values in the 2-dimension array p, it seems to store them localy. That is, if i try to access for example p[0][0] within the funcion, i can do it, however outside the function i just get a memory error, as if i was accessing the outer space (which i probably am). Can anyone help me out here?
Example:
Code:
double **knots;
void SplineToVector(const Spline spline, double **p, int *size)
{
int i, j;
*size = spline.sections_list_size + 3;
p = new double*[*size];
for(i = 0; i < spline.sections_list_size; i++)
{
p[i] = new double[3];
for(j = 0; j < 3; j++)
p[i][j] = spline.sections_list[i].k_matrix[0][j];
}
for(i = 1; i < 4; i++)
{
p[spline.sections_list_size - 1 + i] = new double[3];
for(j = 0; j < 3; j++)
p[spline.sections_list_size - 1 + i][j] = spline.sections_list[spline.sections_list_size - 1].k_matrix[i][j];
}
printf("Sample Knot: %f\n", p[0][0]; // NO ERROR HERE
}
void main()
{
Spline example;
//Other stuff related to Spline
SplineToVector(example, knots, n_knots);
printf("Sample Knot: %f\n", knots[0][0]; // MEMORY ERROR
}