Memory Allocation

We say stack is allocated to the local variables ,formal arguments and return addresses of a function for manipulating function calls?
but the question arises when does the stack allocation to above things take place?at compile time or run time.And by the way what about automatic memory allocation.what is it?

Questions by shiffoni gupta

Showing Answers 1 - 3 of 3 Answers

coolquasar

  • Sep 15th, 2008
 

Memory is allocated only during the runtime, and compiler just decides the amount of memory needed by each variable... It just adds the displacement value to be added to the Base Stack pointer for "Local Variables" Allocation.

kbjarnason

  • Jul 1st, 2010
 

C neither requires nor defines a stack; at most it requires, in some cases, behaviour which is stack-like, but could be provided by any number of means.

As to when the allocation is done, again, this is implementation-defined.  One could posit an optimizing compiler which notes a maximum recursion depth of 4 for a function, then simply pre-allocates a buffer large enough to hold the data and relevant operations for accessing it, if that were more efficient that other methods.

The conventional method, in most cases, however is to allocate locals and similar data at runtime.  Note that even here, this can be done several ways, such as each call requiring an allocation, or having a single memory manager which allocates "chunks" then doles them out as needed, allocating new chunks only when the existing regions are exhausted.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions