我正在努力尝试加速C中的一些通用数据处理.我已经编写了几种形式的子程序:
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;
}
Run Code Online (Sandbox Code Playgroud)
我喜欢这种风格,因为它易于阅读和使用,但我经常把它称为:
array = do_something(array,...);
Run Code Online (Sandbox Code Playgroud)
如果我改为使用void子函数,它是否会产生更快的代码(并且可能防止内存泄漏):
void do_something(double *arr_in, ...) {
for (...) {
arr_in = do that something;
}
return;
}
Run Code Online (Sandbox Code Playgroud)
更新1:我在可执行文件上运行了valgrind --leak-check = full,并且看起来使用第一种方法没有内存泄漏.但是,可执行文件链接到一个库,该库包含我使用此表单创建的所有子例程,因此它可能无法捕获库中的泄漏.
我很好奇我将如何编写包装来释放内存以及**真正做什么,或指针指针是什么,所以我避免使用**路径(也许我做了它错了,因为它没有在我的mac上编译).
这是一个当前子例程:
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 = …Run Code Online (Sandbox Code Playgroud)