Thread: Malloc converted to a normal array

1. 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. 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.

3. n is 3

4. 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?

5. 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. What is the ** on the v array?
Cos that is where the problem lies currently?

7. double is the type.
* is a pointer.
* is a pointer.
v is the variable name.

You don't need those casts.

Quzah.

8. Originally Posted by quzah
You don't need those casts.
.... unless your C compiler is really a C++ compiler.

9. Oh yeah of course I dont, I am re defining them as arrays. Stupid me.
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. Well you don't need any of the malloc calls, if you just copy/paste the arrays I've already posted.

11. Originally Posted by a.mlw.walker
Oh yeah of course I dont, I am re defining them as arrays. Stupid me.
```for (i=0;i<=n;i++) {