1

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?

5
  • When passing data around is needed, use heap, otherwise stack, just like the post answer in your link. In your example, there is no need to use heap, but when program grows larger, you will need store data in heap since it can travel anywhere. Commented Dec 13, 2013 at 9:49
  • You have yourself given the answer. Commented Dec 13, 2013 at 9:51
  • was doing that, but what made me doubt is a warning: "passing argument 1 of update from incompatible pointer type". Actually, should have write that in the post. Commented Dec 13, 2013 at 9:51
  • 2
    In your stack version, it's still update(p,size) since first argument is pointer. Commented Dec 13, 2013 at 9:55
  • indeed, sorry for the typo Commented Dec 18, 2013 at 9:22

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for the answer. But would you have an explanation about the warning "passing argument 1 of update from incompatible pointer type" ?
Since p is an array of double call your update function as update(p,size). This is because when you refer the array by its name, it will in turn decay to a pointer of the first element in the array, in your case it will be a double *.
1

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

Comments

1

"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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.