Geeks Talk

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.

Memory allocation

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

Go Back   Geeks Talk > Software Development > C and C++
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read
  #1 (permalink)  
Old 02-07-2009
Junior Member
 
Join Date: Feb 2009
Location: Allahabad
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
vivgrn is on a distinguished road
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?
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-08-2009
Junior Member
 
Join Date: Feb 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
clickankit4u is on a distinguished road
Lightbulb Re: Memory allocation

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.
Reply With Quote
  #3 (permalink)  
Old 02-09-2009
Junior Member
 
Join Date: Feb 2009
Location: Allahabad
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
vivgrn is on a distinguished road
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?
Reply With Quote
  #4 (permalink)  
Old 02-09-2009
Junior Member
 
Join Date: Feb 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
chaamurah is on a distinguished road
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
Reply With Quote
  #5 (permalink)  
Old 03-02-2009
Junior Member
 
Join Date: Mar 2009
Location: Washington, DC
Posts: 10
Thanks: 0
Thanked 3 Times in 1 Post
u6022 is on a distinguished road
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.
Reply With Quote
Reply

  Geeks Talk > Software Development > C and C++

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 07:10 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved