Thread: Malloc converted to a normal array

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    204

    Malloc converted to a normal array

    hi Guys
    I am using an algorithm called nelder mead that I have found online. It works as expected however I am trying to port it to an electronics chip. The point is allocating memory when you are talking about a chip with 128KB of space is not a good idea.
    The algorithm calls
    Code:
    #include <malloc.h>
    however I understand it is stdlib that holds to functions of malloc???
    and it is used here:
    Code:
    /* allocate the rows of the arrays*/
    	v =  (double **) malloc ((n+1) * sizeof(double *));
    	f =  (double *) malloc ((n+1) * sizeof(double));
    	vr = (double *) malloc (n * sizeof(double));
    	ve = (double *) malloc (n * sizeof(double));
    	vc = (double *) malloc (n * sizeof(double));
    	vm = (double *) malloc (n * sizeof(double));
    
    	/* allocate the columns of the arrays*/
    	for (i=0;i<=n;i++) {
    		v[i] = (double *) malloc (n * sizeof(double));
    Please can someone help me adjust this so that it is no longer allocating specific memory for these arrays.
    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you can convert it to arrays, but you need to know the value for 'n' at compile time.

    That, and whether you actually have enough memory for all those arrays.
    I mean, at n=100, you're looking at nearly 90KB.
    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.

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    n is 3

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well that's easy enough then
    Code:
    	double v[n+1][n];
    	double f[n+1];
    	double vr[n];
    	double ve[n];
    	double vc[n];
    	double vm[n];
    Are v and f really just off square?
    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.

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    This is currently how it is:
    Code:
    	double **v;     /* holds vertices of simplex */
    	double pn,qn;   /* values used to create initial simplex */
    	double *f;      /* value of function at each vertex */
    	double fr;      /* value of function at reflection point */
    	double fe;      /* value of function at expansion point */
    	double fc;      /* value of function at contraction point */
    	double *vr;     /* reflection - coordinates */
    	double *ve;     /* expansion - coordinates */
    	double *vc;     /* contraction - coordinates */
    	double *vm;     /* centroid - coordinates */
    	double min;
    
    	double fsum,favg,s,cent;
    
    	/* dynamically allocate arrays */
    
    	/* allocate the rows of the arrays*/
    	v =  (double **) malloc ((n+1) * sizeof(double *));
    	f =  (double *) malloc ((n+1) * sizeof(double));
    	vr = (double *) malloc (n * sizeof(double));
    	ve = (double *) malloc (n * sizeof(double));
    	vc = (double *) malloc (n * sizeof(double));
    	vm = (double *) malloc (n * sizeof(double));
    
    	/* allocate the columns of the arrays*/
    	for (i=0;i<=n;i++) {
    		v[i] = (double *) malloc (n * sizeof(double));
    	}

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    What is the ** on the v array?
    Cos that is where the problem lies currently?

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    double is the type.
    * is a pointer.
    * is a pointer.
    v is the variable name.

    You don't need those casts.


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by quzah View Post
    You don't need those casts.
    .... unless your C compiler is really a C++ compiler.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    204
    Oh yeah of course I dont, I am re defining them as arrays. Stupid me.
    however what about this malloc call?
    Code:
    for (i=0;i<=n;i++) {
    		v[i] = (double *) malloc (n * sizeof(double));
    	}
    Whats it doing here, or do i not need this also?

  10. #10
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Well you don't need any of the malloc calls, if you just copy/paste the arrays I've already posted.
    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.

  11. #11
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by a.mlw.walker View Post
    Oh yeah of course I dont, I am re defining them as arrays. Stupid me.
    however what about this malloc call?
    Code:
    for (i=0;i<=n;i++) {
    		v[i] = (double *) malloc (n * sizeof(double));
    	}
    Whats it doing here, or do i not need this also?
    Arrays and Pointers, section 2.14

    What do you hope to gain by changing malloc calls to fixed-sized arrays? You'll need the space to hold the data in either case. Do you feel that the overhead of an extra few pointers is what's keeping the data from fitting in memory or is there some other problem?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. changing normal array to pointer
    By liukinhei in forum C Programming
    Replies: 1
    Last Post: 03-17-2008, 03:01 PM
  2. Replies: 19
    Last Post: 12-17-2007, 02:57 AM
  3. Replies: 12
    Last Post: 06-24-2005, 04:27 PM
  4. Assigning the value of a const array to a normal array
    By Accident Prone in forum C++ Programming
    Replies: 6
    Last Post: 08-11-2003, 10:40 PM
  5. Can a string be converted to an array of characters?
    By supaben34 in forum C++ Programming
    Replies: 8
    Last Post: 12-08-2002, 12:18 PM