一次使用的功能 - 我应该使用malloc吗?

sve*_*ven 3 c linux malloc memory-management

我有一个很长的C代码,有一个函数只能被调用一次.其中包括像一些变量char array,int.代码是这样的:

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
}
Run Code Online (Sandbox Code Playgroud)

我将在嵌入式Linux设备上运行该代码.我想知道我是否应该使用malloc该函数的所有变量然后free呢?它是否有助于有效地使用资源,或者是否会出现一些严重的问题(如果是这种情况,可能会发生什么)?

use*_*342 9

使用malloc较少比隐堆栈分配效率.堆栈是一种非常有效的分配机制,因为分配和释放都归结为堆栈指针的简单更新,不会产生碎片.