Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on Memory allocation within the C and C++ forums, part of the Software Development category; Hi The memory allocation for a 'static' object is at compile time. Say that i have a recursive function which can be called n times and having the following structure. ...
|
|||||||
|
|||
|
Memory allocation
Hi
The memory allocation for a 'static' object is at compile time. Say that i have a recursive function which can be called n times and having the following structure. int func( int var) { int temp=0; if(var==1) return 1; else return func(var-1); } How does the compiler know how much memory to be allocated to the temp variables for the instances of func? |
| Sponsored Links |
|
|||
|
hi
In this function temp u defined is a local variable of the function and not defined as static so it will be allocated memory as many no. of times fn() will be called and intialized with the value as zero evey time fn() will be called. |
|
|||
|
Re: Memory allocation
thanks for the reply
But my question was a little different. What I was asking was that how does the compiler know as to how many times the recursive function is going to run? Is this knowledge gained at compile time or run time? |
|
|||
|
Re: Memory allocation
in this program when u call the recursive function u will pass one argument i.e., some integer value.
and what ever u write the source code that will be converted in to assembly code.so, based on the argument u r passing for the function,assembly code is generated. generating the assembly code is a compile time process. so,definitely the knowledge is gained at compile time |
|
|||
|
Re: Memory allocation
To the best of my knowledge this is what happens. Every time the function is called the function arguments are "pushed to the stack" along with non-static local variables. They are all saved in memory allocated at run time. In addition to the function arguments a space is saved for the return value. The amount of space needed for each call is known at compile time but the number of times the function needs to be called is not. Every time the function is called in recursion a new set of the arguments plus one gets pushed onto the stack. After the last call the return value is past to the second to the last return storage space and the memory on the stack is reclaimed. The stack unwinds until finally the return value is returned by the first called function and the memory used for the first set of arguments is reclaimed.
If one of the variables declared in the function is static memory for it may be allocated at compile time. Because it is static no additional space is needed for it on the stack when recursion occurs. |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Physical Memory and Virtual Memory | blenda | Windows | 6 | 07-14-2009 03:22 PM |
| memory allocation for object | reess | C and C++ | 14 | 12-25-2008 02:15 PM |
| Memory allocation | sundarkms | OOPS | 4 | 10-02-2008 01:59 AM |
| MemoryCleaner performs fast and powerful memory sweep, removing wasted memory blocks, | JobHelper | Geeks Lounge | 0 | 10-06-2007 07:13 AM |
| C Programming - Dynamic Memory allocation | Lokesh M | C and C++ | 0 | 05-29-2006 03:35 AM |