Solved

Does function created in C take some of memory?

Posted on 1998-09-06
3
184 Views
Last Modified: 2010-04-10
Hello experts,

As I am novice to C, so I would like to ask if a recursive function created in MSVC++ occupy some of the memory spaces. If yes, how woul I clear out this function after I have use it.
By the way, does recursive function able to cause the exhaustive memory in computer..?

Thank You for the help...
0
Comment
Question by:dekoay
3 Comments
 
LVL 3

Accepted Solution

by:
LucHoltkamp earned 30 total points
ID: 1172113
In fact it does..
When the function is executed it's returnadress is saved on stack, also room for it's parameters is reserved on stack.
Because the stack is normally not as large as main memory (1 MB in win95) a stack overflow is possible, if your recursive function calls itself many times (10 of thousends in win95, but it depends on the size of your parameters).
Notice that under DOS (16 bits) the stack size is normally not larger then 64 k. So a stackoverflow is much more likely.
luc.
0
 
LVL 1

Expert Comment

by:newexpert
ID: 1172114
Any code will take up memory spaces.  Code memory is managed by the operating system therefore you don't need to worry about it.

Any function will use stack and/or heap for working.  Stack is automatically cleaned up by the function after exiting, dynamic heap needs to be freed by you (just use delete operator on anything that was created by new operator).

If you have a run-away recursive function the memory (probably on the stack) will quickly run out.  In some circumstances you get a segment violation. You can also get a stack-heap collision, and corrupt heap data, if stack and heap shares the same memory segment.
0
 

Author Comment

by:dekoay
ID: 1172115
Is there a way I can delete certain functions from the stack so there I can reuse the free space.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now