在制作大缓冲区时,使用malloc分配缓冲区是否比使用静态分配的缓冲区更快?
我正在尝试制作这样的数组:
char tmp[4][32768];
//code
这是在一个函数中,我发现使这个数组全局化将提高程序的速度(不幸的是我不能保持这样,因为函数在遇到某些数据时会递归调用自身)。我想让这个功能更快,但我读过它malloc可能很慢。那么,保持这样或这样做会更快吗?:
char** tmp;
tmp = malloc(4 * sizeof(char*));
tmp[0] = malloc(32768);
tmp[1] = malloc(32768);
tmp[2] = malloc(32768);
tmp[3] = malloc(32768);
//code
free(tmp[0]);
free(tmp[1]);
free(tmp[2]);
free(tmp[3]);
free(tmp);
回答
这不是快或慢的问题。这是你是否炸毁堆栈的问题。
您正在创建一个大小为 128K 的数组,这对于局部变量来说相当大,而局部变量通常位于堆栈中。如果您递归调用此函数,则每次调用的堆栈上还有 128K。只需几次递归调用就足以导致堆栈溢出,这可能会导致崩溃。
动态分配内存几乎是您唯一的选择。但是,您可以将其减少为单个分配:
char (*tmp)[32768] = malloc(4 * sizeof *tmp);
这种分配是有效的,因为类型的 2D 数组char [4][32768]衰减为char (*)[32768]与上述类型匹配的类型的指针tmp。返回的内存malloc足以容纳 4 个类型的对象char[32768]。该内存是连续的,与malloc为每个子数组分别调用 4 次不同。
- @PQCraft That sounds like a great use case for `goto`.
- @PQCraft That will make your array disjoint which could potentially affect the speed of accessing it. The method in the answer uses a single contiguous block of memory just as a 2D array would.
- Since `sizeof (char)` is guaranteed to be 1, I believe `malloc(4 * 32768)` is equivalent to `malloc(4 * sizeof (char [32768])`.
- @mediocrevegetable1 Did one better, using the dereferenced variable instead of the type.
- @MichaelDorgan it's not an array of pointers, it's a pointer to an array of 32768 `char`s, made possible thanks to the `()`s and operator precedence. Hopefully that clears things up (just noticed the answer edit, that explains it better).
- @JérômeRichard The operand of `sizeof` is not evaluated unless it is a VLA. It is just observed for its type and is computed at compile time.
THE END
二维码