Note that there are some explanatory texts on larger screens.

plurals
  1. POC: Returning a void versus returning a double * from a subfunction
    text
    copied!<p>I'm working on trying to speed up some general data processing in C. I've written several subroutines of the form:</p> <pre><code>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; } </code></pre> <p>I like this style because it's easy to read and use, but often I call it as:</p> <pre><code> array = do_something(array,...); </code></pre> <p>Would it make for faster code (and maybe prevent memory leaks) if I were to instead use void subfunctions as:</p> <pre><code>void do_something(double *arr_in, ...) { for (...) { arr_in = do that something; } return; } </code></pre> <p>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. </p> <p>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).</p> <p>Here's one current subroutine:</p> <pre><code>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&lt;npts; i++) { if (i&lt;M) { cos_taper[i] = .5 * (1-cos(i * PI / (M + 1))); } else if (i&lt;npts - M - 2) { cos_taper[i] = 1; } else if (i&lt;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; } </code></pre> <p>From the advice I've gotten here, it sounds like a better method would be:</p> <pre><code>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&lt;npts; i++) { if (i&lt;M) { cos_taper[i] = .5 * (1-cos(i * PI / (M + 1))); } else if (i&lt;npts - M - 2) { cos_taper[i] = 1; } else if (i&lt;npts) { cos_taper[i] = .5 * (1-cos((npts - i - 1) * PI / (M + 1))); } arr_out[i] = arr_in[i] * cos_taper[i]; } return } </code></pre> <p>call:</p> <pre><code>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; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload