简体   繁体   English

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

[英]one time used function - should I use malloc or not

I have a little bit long C code and there is one function that would be called only once. 我有一个很长的C代码,有一个函数只能被调用一次。 That includes some variables like char array , int . 这包括一些变量,如char arrayint The code is something like this: 代码是这样的:

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
}

I will run that code on an embedded linux device. 我将在嵌入式Linux设备上运行该代码。 I wonder if I should use malloc for the all variables on that function then free them? 我想知道是否应该将malloc用于该函数的所有变量然后free它们? would it help to use the resources efficiently or could it arise some serious problems (if it is the case, what might happen)? 它是否有助于有效地使用资源,或者是否会出现一些严重的问题(如果是这种情况,可能会发生什么)?

Using malloc would be less efficient than implicit stack allocation. 使用malloc会比隐含的堆栈分配效率较低 The stack is an extremely efficient allocation mechanism, as both allocation and deallocation boils down to a simple update of the stack pointer, leaving behind no fragmentation. 堆栈是一种非常有效的分配机制,因为分配和释放都归结为堆栈指针的简单更新,不会产生碎片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM