I'm working on trying to speed up some general data processing in C. I've written several subroutines of the form:
double *do_something(double *arr_in, ...) {
double *arr_out;
arr_out = malloc(...)
for (...) {
do the something on arr_in and put into arr_out
}
return arr_out;
}
I like this style because it's easy to read and use, but often I call it as:
array = do_something(array,...);
Would it make for faster code (and maybe prevent memory leaks) if I were to instead use void subfunctions as:
void do_something(double *arr_in, ...) {
for (...) {
arr_in = do that something;
}
return;
}
update 1: I ran valgrind --leak-check=full on the executable and it appears there were no memory leaks using the first method. However, the executable links to a library which contains all the subroutines I made with this form, so it might not catch leaks from the library.
I'm curious as to how I would write the wrappers to free the memory and what the ** really does, or what a pointer to a pointer is, so I'm avoiding using the ** route (that and maybe I did it wrong because it didn't compile on my mac).
Here's one current subroutine:
double *cos_taper(double *arr_in, int npts)
{
int i;
double *arr_out;
double cos_taper[npts];
int M;
M = floor( ((npts - 2) / 10) + .5);
arr_out = malloc(npts*sizeof(arr_out));
for (i=0; i<npts; i++) {
if (i<M) {
cos_taper[i] = .5 * (1-cos(i * PI / (M + 1)));
}
else if (i<npts - M - 2) {
cos_taper[i] = 1;
}
else if (i<npts) {
cos_taper[i] = .5 * (1-cos((npts - i - 1) * PI / (M + 1)));
}
arr_out[i] = arr_in[i] * cos_taper[i];
}
return arr_out;
}
From the advice I've gotten here, it sounds like a better method would be:
void *cos_taper(double *arr_in, double *arr_out, int npts)
{
int i;
double cos_taper[npts];
int M;
M = floor( ((npts - 2) / 10) + .5);
for (i=0; i<npts; i++) {
if (i<M) {
cos_taper[i] = .5 * (1-cos(i * PI / (M + 1)));
}
else if (i<npts - M - 2) {
cos_taper[i] = 1;
}
else if (i<npts) {
cos_taper[i] = .5 * (1-cos((npts - i - 1) * PI / (M + 1)));
}
arr_out[i] = arr_in[i] * cos_taper[i];
}
return
}
call:
int main() {
int npts;
double *data, *cos_tapered;
data = malloc(sizeof(data) * npts);
cos_tapered = malloc(sizeof(cos_tapered) * npts);
//fill data
cos_taper(data, cos_tapered, npts);
free(data);
...
free(cos_tapered);
...
return 0;
}