Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static vs. Malloc

What is the advantage of the static keyword in block scope vs. using malloc?

For example:

Function A:

f() {
    static int x = 7;
}

Function B:

f() {
    int *x = malloc(sizeof(int));
    if (x != NULL)
        *x = 7;
}

If I am understanding this correctly, both programs create an integer 7 that is stored on the heap. In A, the variable is created at the very beginning in some permanent storage, before the main method executes. In B, you are allocating the memory on the spot once the function is called and then storing a 7 where that pointer points. In what type of situations might you use one method over the other? I know that you cannot free the x in function A, so wouldn't that make B generally more preferable?

like image 366
CowZow Avatar asked Mar 06 '13 04:03

CowZow


People also ask

Is malloc static or dynamic?

In Dynamic Memory Allocation memory is allocated at runtime using calloc(), malloc(), once the memory is allocated, the memory size can be changed.

Is static memory allocation faster?

Static memory allocation provides faster execution, as at the time of execution it doesn't have to waste time in allocation memory to the program.

What is the difference between static and dynamic memory?

When the allocation of memory performs at the compile time, then it is known as static memory. When the memory allocation is done at the execution or run time, then it is called dynamic memory allocation. The memory is allocated at the compile time. The memory is allocated at the runtime.

Should I avoid using malloc?

The primary reason for not using malloc in some particular cases is probably the fact that it employs a generic, one-size-fits-all approach to memory allocation. Other approaches, such as memory pools and slab allocation may offer benefits in the case of having well-known allocation needs.


1 Answers

Both programs create an integer 7 that is stored on the heap

No, they don't.
static creates a object with static storage duration which remains alive throughout the lifetime of the program. While a dynamically allocated object(created by malloc) remains in memory until explicitly deleted by free. Both provide distinct functionality. static maintains the state of the object within function calls while dynamically allocated object does not.

In what type of situations might you use one method over the other?

You use static when you want the object to be alive throughout the lifetime of program and maintain its state within function calls. If you are working in a multithreaded environment the same static object will be shared for all the threads and hence would need synchronization.

You use malloc when you explicitly want to control the lifetime of the object.for e.g: Making sure the object lives long enough till caller of function accesses it after the function call.(An automatic/local object will be deallocated once the scope{ } of the function ends). Unless the caller explicitly calls free the allocated memory is leaked until the OS reclaims it at program exit.

like image 136
Alok Save Avatar answered Oct 12 '22 04:10

Alok Save