3

我的 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
}

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

4

1 回答 1

9

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

于 2013-06-27T13:37:18.400 回答