Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4033882
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:54:09+00:00 2026-05-20T11:54:09+00:00

Let us consider the following factorial example : #include <iostream.h> int factorial(int); void main(void)

  • 0

Let us consider the following factorial example :

#include <iostream.h>

int factorial(int);

void main(void) {
    int number;

    cout << "Please enter a positive integer: ";
    cin >> number;
    if (number < 0)
        cout << "That is not a positive integer.\n";
    else
        cout << number << " factorial is: " << factorial(number) << endl;
}

int factorial(int number) {
    int temp;

    if(number <= 1) return 1;

    temp = number * factorial(number - 1);
    return temp;
}

How can I compute the memory used the function factorial() ? To be more precise I want to know how much memory the function uses ?

EDIT:

This is just a sample program and the program I’m working on is lot different and has many functions and I actually want to calculate the memory usage of every functions.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T11:54:10+00:00Added an answer on May 20, 2026 at 11:54 am

    For my second answer (prompted by the comments to my first answer), you could use a function that computes how much of the stack has been touched. Here’s one that hopefully doesn’t make too many assumptions about the nature of the stack on a given architecture. It assumes a descending stack, which is a fairly safe bet for most people:

    #define LU_RAND_SEED 123456789LU
    #define LU_RAND(S) ((S) * 69069 + 362437 & 0XFFFFFFFFLU)
    
    int depth(int maxdepth)
    {
      unsigned long r = LU_RAND_SEED;
      int d = 0;
      unsigned long *stk = (unsigned long *)alloca(maxdepth);
      for (int i = maxdepth/sizeof(unsigned long); i--; )
        {
          r = LU_RAND(r);
          if (stk[i] != r)
            {
              stk[i] = r;
              d = i;
            }
        }
      return maxdepth - d*sizeof(unsigned long);
    }
    

    You call it once before the function you want to test, and once after. The second call will return how many bytes of the stack have been touched (minus some constant value that you’ll have to determine experimentally). You have to make sure that only the code you are testing runs between the two calls to depth():

    depth(512<<10);
    int f = factorial(number);
    int d = depth(512<<10);
    cout << ... f ...
    

    You also have to watch out for edge cases. For instance, if number is 0 or 1, the depth test fails for reasons I haven’t figured out yet. And goodness knows what will happen if the compiler starts reusing stack slots. In a nutshell: caveat emptor.

    Notes:

    1. It almost goes without saying that this technique is probabilistic. If your function happens to populate all or part of the stack with the same data as the PRNG, the result may be compromised, though the probability of being more than a few bytes off is vanishingly small.
    2. I lifted the PRNG from here. srand()/rand() or their thread-safe counterparts will probably work fine, but I wanted to avoid calling any function other than alloca().
    3. unsigned long r = (unsigned long)&r; avoids the constant seed for slightly more randomness, and it works since if the two calls to depth() are made at different stack depths, this technique will fail anyway. I just don’t know how safe the seeds thus generated will be.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Please let us consider following code: #include <iostream> using namespace std; union{ int i;
Let's consider the following code snippet void Test() { int x = 0; int&
Let's consider following, simplified example: We have 2 tabs withing <rich:tabPanel switchType=ajax> , each
Let's consider the below example. There, I have: target MAIN calls target t and
Let's consider the following simple expressions in Java. char c='A'; int i=c+1; System.out.println(i =
Let's consider the following code snippet in Java. package escape; final public class Main
Got really confused on how JavaScript/BackboneJS works. Let's consider the following example: window.MyView =
Consider the following sample program in which a loop is running; int main() {
Please consider the following fork() / SIGCHLD pseudo-code. // main program excerpt for (;;)
Are there really any significant advantages to overload methods? Let's consider the following example:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.