Lab Assignment--Process Memory

The purpose of this lab is to explore the memory associated with the user process: text, data, and stack.
  1. Copy and paste the following source code: memory_segments.cpp

  2. Compile and run the program.

    Several addresses are output from the program including:

    You may want to use a redirect to save the output for later.

  3. Your task is to fill in addresses of the variables, and functions to provide at least TWO examples of: Fill in the following template: memtemplate.txt

    To get an idea of what is expected, the following is an example run with addresses of these components: memdiagram.txt

    Please remember to include the "etext", "edata", and "end" addresses

  4. Before continuing, run the program again and confirm that the output is the same.

  5. As variables are added to the stack, do the addresses get smaller or larger?

  6. Do variables stored on the stack ever have the same address as other variables? Why or why not?

  7. Where would you expect variables (or arguments) in recursive functions to be stored (stack, heap, or other data segment)? When you are finished step 8 below, comment on whether your expectations were correct or not.

  8. Test your expectation by adding a recursive factorial function. For instance, the factorial of 5 is represented by 5! and is calculated as 5x4x3x2x1=120.

    In the factorial function, you will print the address of the factorial function and the address of the argument passed to it.

    In main, you will prompt the user for what factorial they will want to calculate and send that input as an argument to the function. In main, you will also print the value returned from the factorial function.

    The idea behind the recursive factorial function is the following:
    Fn = {
    1 if n = 0 (base case)
    n*Fn-1       if n >= 1 (recursive step)


Submit 2 files to URCourses

  1. Answers to questions 5, 6, and 7 (as comments at the top of your code file)
  2. The memory diagram (as comments immediately below the answers in your code file)
  3. The code with the new recursive factorial function
  4. Two sample runs of the recursive factorial function showing: