sven
sven

Reputation: 1121

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. That includes some variables like char array, int. 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. I wonder if I should use malloc for the all variables on that function then free them? would it help to use the resources efficiently or could it arise some serious problems (if it is the case, what might happen)?

Upvotes: 3

Views: 98

Answers (1)

user4815162342
user4815162342

Reputation: 154906

Using malloc would be less efficient than implicit stack allocation. 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.

Upvotes: 9

Related Questions