Pergunta

I understand the answer given by this very similar question: When is it best to use the stack instead of the heap and vice versa?

But I would like to confirm:

Here is a very simple program:

void update_p(double * p, int size){
    //do something to the values refered to by p
}

void test_alloc(int size){
    double p[size];
    unsigned int i;
    for(i=0;i<size;i++)p[i]=(double)i;
    update(&p,size);
    for(i=0;i<size;i++)printf("%f\n",p[i]);
}

int main(){
    test_alloc(5);
    return 1;
}

and an alternative version of test_alloc:

void test_alloc(int size){
    double * p = calloc(sizeof(double),size);
    unsigned int i;
    for(i=0;i<size;i++)p[i]=(double)i;
    update(p,size);
    for(i=0;i<size;i++)printf("%f\n",p[i]);
    free(p);
}

It seems to be both version are valid (?). One is using the stack, the other is using heap (?). Are both approach valid? What are their advantages or issues?

If indeed the array is not to be used once exited from test_alloc, is the first approach preferable as not requiring memory management (i.e. forgetting to free the memory) ?

If test_alloc is called massively, is it more time consuming to reserve memory in the heap and might this have an impact on performance?

Would there be any reason to use the second approach?

Foi útil?

Solução

The question of allocation in stack (or) heap is dependent on the following parameters:

  • The scope and life time in which you want the variable to be available: Say in your program if you need to use members of the array of double after test_alloc in main, you should go for dynamic allocation .

  • The size of your stack: Say you are using the test_alloc function in a context of a thread which has a stack of 4K, and your array size which in in turn dependent on the size variable accounts to 1K, then stack allocation is not advisable, you should go for dynamic allocation.

  • Complexity of code: While using dynamic allocations, enusre to take care of avoiding memory leaks, dangling pointers etc. This adds some amount of complexity to the code and are more prone to bugs.

  • The frequency of allocation: Calling malloc and free in a loop may have lesser performance. This point is based on the fact that allocation in stack is just the matter of offseting the SP, on contrary while allocation in heap needs some more book keeping.

Outras dicas

In stack version, you can't allocate an array with 100000000 element. When allocate small array using stack, it's faster. When allocate very large array, using heap.

More information about stack, heap memory allocation, consider these writing: Stack-based memory allocation,Heap-based memory allocation

"If test_alloc is called massively, is it more time consuming to reserve memory in the heap and might this have an impact on performance?"

Well I guess you are looking at the performance in allocating from stack and heap. Always stack allocation is faster as all it does is move the stack pointer. Heap allocation definitely takes more time as it has to do the memory management. So if you dont have too many operations inside the above said function which will not cause a stack overflow(as creating too many objects on the stack will increase the chances of stack overflow) and provided you dont need the scope of this variable outside you can probably use the stack itself.

So to answer your question, it is not a bad manner to use the stack to avoid dynamic allocation if you do not use too many objects on the stack and cause a stack overflow.

Hope this helps.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top