Lecture Synopsis, UMBC CMSC 202, Spring 1998 (2024)

UMBC CMSC202, Computer Science II, Spring 1998, Sections 0101, 0102, 0103, 0104 and Honors

Assigned Reading:

  • A Book on C:
  • Programming Abstractions in C:

Handouts (available on-line): none

Topics Covered:

  • Announcements:
    • Project 1 will be graded late, because the @#!$#@ grader quit on me.
    • I will ask approximately 15 students (chosen randomly) to move from Section 0102 to 0104 for discussion. If you are chosen you will receive email. (Both sections are Wednesday, 10-10:50.)
    • Exam 1 review items.
  • Project 2 discussion.
  • We discussed how memory for local variables is allocated in a "stack". When a function is called, the "stack" grows. The top portion of the stack is used by the function which is most recently called. A "stack pointer" keeps track of the top of the stack. When a function returns, the stack shrinks. So, memory used by the function is freed up for later use. In the executable machine code, local variables are referenced using an offset from the stack pointer rather than an absolute memory address.

    Some examples:

    • In the first program we have 3 functions, each with a local variable called x. Here, main() calls func1(), which in turn calls func2(), which finally calls func3(). In each function, the address of x is printed out. Note that in the sample run the different x's have addresses that are evenly spaced --- they are exactly 40 bytes apart. Besides, the 8 bytes for the local variable x each function needs space on the stack to store the return address (to the function that called this function), the return value of the function and other internal information that we do not need to discuss.
    • In the second program, we add an additional local variable y to each function. Now, the sample run shows that the addresses of each x are 48 bytes apart. The stack must grow by an extra 8 bytes to accommodate the new variable y in each function.
    • In the third program, we add another local variable z to each function. Again, the spacing of the addresses of x grows by 8 bytes. (See sample run.) This time the stack grows by 56 bytes for each function call.

      In this program we also call func2() directly from main(). In this case, note that the local variables for func2() have exactly the same addresses as the local variables of func1() in the previous call.

      Finally, we call func3() directly from main(). Again, the addresses of the local variables for func3() take on the same addresses as the variables for func1() and func2() in previous calls.

      This example shows that the locations of the local variables in a function are not fixed and that the same memory locations on the stack are repeatedly used for different functions.

    • Our next program repeats this experiment using a recursive function. Nothing has changed. The sample run shows that memory for local variables of a recursive function is also allocated on the stack. The growing and shrinking stack is the mechanism used by most compilers to automatically allocate memory for functions. Note that memory for the parameter n is also on the stack.

      This example explains how each call to a recursive function allocates memory for a new set of local variables.

  • New topic: How do we go about writing recursive functions? We can practice on some simple examples. Here are three ways to use recursion to add the numbers in an array:
    • Method 1: break off the last element of the array and recursively sum the first part of the array.
    • Method 2: break off the first element of the array and recursively sum the last part of the array.
    • Method 3: divide the array into two halves and recursively sum the two halves.
    The sample runs show that the 3 methods yield the same results.

    You should practice writing simple recursive functions. Get used to arguing that the base case is correct. Get used to arguing that the recursive case is correct. Don't get too cute with the base case. It doesn't hurt to have some extra cases even if they don't get used!

  • We did not have time to finish off the discussion about finding the majority character in a string. We will continue with these examples in the next lecture. For now, you can look over the programs and sample runs.
    • A recursive function which counts the occurrences of a character in a string. Sample run.
    • A recursive function which finds the character which appears in a majority of the positions in a string. The function returns 0, if no such character exists. Sample run.
Last Modified:22 Jul 2024 11:27:46 EDTbyRichard Chang

Back upto Spring 1998 CMSC 202 Section Homepage
Lecture Synopsis, UMBC CMSC 202, Spring 1998 (2024)

References

Top Articles
Latest Posts
Article information

Author: Ms. Lucile Johns

Last Updated:

Views: 5267

Rating: 4 / 5 (61 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ms. Lucile Johns

Birthday: 1999-11-16

Address: Suite 237 56046 Walsh Coves, West Enid, VT 46557

Phone: +59115435987187

Job: Education Supervisor

Hobby: Genealogy, Stone skipping, Skydiving, Nordic skating, Couponing, Coloring, Gardening

Introduction: My name is Ms. Lucile Johns, I am a successful, friendly, friendly, homely, adventurous, handsome, delightful person who loves writing and wants to share my knowledge and understanding with you.