Multi-dimensional Dynamic Arrays

This is a discussion on Multi-dimensional Dynamic Arrays within the C++ Programming forums, part of the General Programming Boards category; I'm trying to create a function that converts a custom structure - Spline - to a 2-dimension array of doubles. ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    19

    Multi-dimensional Dynamic Arrays

    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
    }

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,434
    You're not (or knot) returning a value to main (which is int main by the way)

    Easy way is
    knots = SplineToVector(example, n_knots);

    Where your function declares
    double **p
    locally, and finishes with a
    return p;
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User
    Join Date
    May 2004
    Posts
    19
    Worked, thnx.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Multi dimensional array
    By $l4xklynx in forum C Programming
    Replies: 7
    Last Post: 01-03-2009, 02:56 AM
  2. Pointers and multi dimensional arrays
    By andrea72 in forum C++ Programming
    Replies: 5
    Last Post: 01-23-2007, 03:49 PM
  3. Dynamic two dimensional arrays
    By ThWolf in forum C++ Programming
    Replies: 14
    Last Post: 08-30-2006, 02:28 PM
  4. Replies: 6
    Last Post: 04-26-2004, 10:02 PM
  5. Pointers to multi dimensional arrays.
    By bartybasher in forum C++ Programming
    Replies: 2
    Last Post: 08-25-2003, 02:41 PM

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