This is part two of our exploration of the underlying machine model. A primary focus will be on the way local objects and their underlying memory is managed by the function call stack. We'll also cover different mechanisms for parameter passing and returned results from functions.
1: Intro to Lobster
Before we start, let me cover a few basics for the Lobster program visualization tool, which we'll use throughout several lecture examples and exercises in the future.
|
2: Functions and The Call Stack
2.1
The memory allocated for each function is generally called an activation record or (more commonly) a stack frame. Each function takes up a certain amount of memory that depends on how many local variables it may need to store, and this memory is allocated and freed as needed during the program. Because of the way that functions call work (i.e. the called function has to finish and return before you can start back up in the original function), it's natural to use a stack to represent the memory frames for each function. Whichever function is called most recently is added to the top of the stack, and will always be removed before any other functions that were already on the stack (this is called the "Last In First Out" or "LIFO" property).
2.1 Exercise: Stack Frames
Consider the following code. Trace through the code either manually or using the Lobster simulation and answer the questions below. Which function has the largest stack frame (in terms of memory use)? How can you tell? Is this a compile-time property or a runtime property? What is the maximum amount of memory on the (call) stack needed by the program at any one given time? Assume an How many different stack frames are created for the You're also welcome to check out this walkthrough video where I talk through the questions. |
3: Mechanisms for Parameters/Returns
3.1
The two primary mechanisms for parameter passing are pass-by-value and pass-by-reference. A similar choice applies for returning results from a function. Let's take a look at the differences between the two, as well as how they relate to function stack frames.
3.1 Exercise: Parameter Passing
Consider this code that defines a function with both pass-by-value and pass-by-reference parameters. What are the values of each variable at the end of the main function? (You can also use the Lobster simulation to check.)
Sample solution…
|
4: Passing/Returning Pointers
4.1
We can achieve an effect similar to pass-by-reference by using a pointer instead. Here's the basic idea - just like with pass-by-reference, we want to work with the original object (e.g. in a It also turns out that the compiler offers pass-by-reference as feature of the C++ language, but it's ultimately turned into pass-by-pointer in the compiled machine code. Here's a brief explanation:
4.1 Exercise: Pass-by-Pointer
The code below contains a broken |
5: Null and Uninitialized Pointers
5.1
A regular pointer contains the address of some other object in your program, and will lead you to that object when you dereference it. But there are a few exceptional cases we should consider:
To recap:
Some more examples:
While uninitialized pointers are pretty much always bad, it's useful to have a nullable pointer to represent something "optional". But, to safely use pointer that might be null, you need to check the pointer before dereferencing it. For example:
You can also shorten that up by replying on an implicit conversion from pointer types to
5.1 Exercise: Null and Uninitialized Pointers
For each of the following code snippets, briefly describe what the last line of code does. (For example, "sets the value of a to 3" or "dereferences a null pointer - program crashes".)
You're welcome to check your solution with this walkthrough video: |
6: Dangling Pointers
Finally, let's take a look at the case of dangling pointers, which are pointers that used to point to a valid object, but the object's lifetime has since ended. The pointer still holds the same address and is still pointing at the memory location where it used to be, but the data there is no longer valid to use. |