gpt4 book ai didi

c - 曾经使用过的函数——我应该使用 malloc 还是不使用

转载 作者:IT王子 更新时间:2023-10-29 00:58:03 25 4
gpt4 key购买 nike

我的 C 代码有点长,有一个函数只能调用一次。这包括一些变量,如 char arrayint。代码是这样的:

void onetimefcn(){
char example_array1[20]="hello...";
//...
char example_array10[14]="hej...";
int x=3,y=432,z=321,d=4439;
//some arithmatic operation
//some char array operation: strcpy, strcmp
// some for loops and if else conditions
}

我将在嵌入式 Linux 设备上运行该代码。我想知道我是否应该为该函数的所有变量使用 malloc 然后 free 它们?它会有助于有效地使用资源还是会引起一些严重的问题(如果是这样,可能会发生什么)?

最佳答案

使用 malloc 比隐式堆栈分配效率。堆栈是一种极其有效的分配机制,因为分配和释放都归结为堆栈指针的简单更新,不会留下任何碎片。

关于c - 曾经使用过的函数——我应该使用 malloc 还是不使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17344632/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com