CS 1371 Introduction to Computing for Engineers - PowerPoint PPT Presentation

1 / 30
About This Presentation
Title:

CS 1371 Introduction to Computing for Engineers

Description:

Unfortunately, in the interests of efficiency and understanding some language ... the return value result which is passed back to the calling function 'underneath. ... – PowerPoint PPT presentation

Number of Views:54
Avg rating:3.0/5.0
Slides: 31
Provided by: david3049
Category:

less

Transcript and Presenter's Notes

Title: CS 1371 Introduction to Computing for Engineers


1
CS 1371Introduction to Computing for Engineers
  • Dynamic Memory Allocation

2
Dynamic Memory Allocation
  • Computer Mechanics
  • Memory concepts
  • Stack

Learning Objectives Understanding Function Calling
3
Background
  • Languages like Matlab that implement logical
    ideas avoid the need to explain computer
    specifics
  • Unfortunately, in the interests of efficiency and
    understanding some language features, we need to
    take a peek at the guts

4
Computing Environment
Gateway
Internet
Processor
Storage
Servers
Local Area Network (LAN)
5
Physical Components of the Computer
Interface
Interface
Interface
Data Bus
Interface
Interface
Local Area Network (LAN)
Interface
6
CPU - Logical Operations
CPU
Arithmetic and Logical Unit (ALU)
Commands
Decoder
Registers
Data
Instructions
Data Bus
Programs
Data
Both
Memory
7
Typical Commands
  • Fetch a value from memory address FC38D
  • Add this value to Register 3
  • Send this value to Device 4E0
  • Test this value against the contents of Register
    4
  • If the result is greater, fetch the next
    instruction from memory address 129CCE

Hexadecimal numbers (easily converted from binary)
Hexadecimal numbers (easily converted from binary)
Registers are temporary storage locations within
the CPU (used for testing and arithmetic
operations)
8
Do We Care About the Details? NO!
  • In the early days of programming, we literally
    programmed the 1s and 0s in each memory
    location
  • Assembly language enabled us to assign names to
    memory locations and single instructions
  • Languages like C grouped tens of these operations
    into higher level lines of code
  • Other languages like C, Java and Matlab group
    hundreds of instructions into abstract ideas
  • We still need to understand some fundamental
    logical ideas.

9
Logical Memory Components
Memory area for storing a program (application)
Memory area private to each program for storing
local variables
Memory area available to all programs for dynamic
data structures
Heap
Stack A
Stack B
Stack C
Operating System
10
Questions?
11
Once Upon a Time
  • Many years ago programmers realized that
    programming a complex task would be simpler if
    they could break the big job down into pieces

12
Once Upon a Time
  • This was the birth of modular programming where
    big programs get broken down into smaller pieces
    called modules, methods, subroutines, procedures
    or in the case of Matlab, functions.

13
Once Upon a Time
  • Each of these functions has some executable code
    and usually some local data like the parameters
    which are passed in.

14
Once Upon a Time
  • Initially each function had its own little area
    for data called its activation record. Thus that
    was where the data was stored when the function
    was activated or running.

Activation Record for f x
f(x)
Activation Record for g y
Can you see any problems with this approach?
g(y)
Activation Record for h z
h(z)
15
Solution
  • Problems
  • Memory was allocated for a function whether or
    not it is being used
  • Accidental or deliberate re-use of the same
    function would over-write the original local
    variables
  • Solutions
  • Dont give each function its own unique
    activation record
  • Every time a function is called, give it a fresh
    activation record on a stack called the
    activation stack.
  • We call each of these records on the activation
    stack a stack frame

16
So What is a Stack?
  • A pile or collection or set of items.
  • Items can only be added to the top
  • Items can only be taken off the top
  • Last-in-first-out (LIFO)

Push
Pop
Thing 3
Thing 2
Thing 1
17
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

18
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

19
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

20
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

21
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

22
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

23
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

24
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

25
What is a Stack?
  • A data structure for storing items which are
    to be accessed in last-in first-out order (LIFO).

26
The Activation Stack
  • The box represents this programs share of
    computer memory (its activation stack).
  • The function foo being executed gets the first
    frame at the bottom of the stack.
  • Data which are used within a function reside in
    that functions stack frame.

foo
27
Adding Modules to the Stack
  • When foo calls another function bar, the new
    function gets its own frame pushed on the
    stack.
  • The new functions variables live in that new
    frame.
  • ONLY the top frame is active. All programs with
    frames underneath it are stopped.

foo
var1
var2
var3
28
Removing Modules from the Stack
  • When the active function completes its
    instructions, its frame is popped off the stack
    and all of its data dies.
  • The only thing that survives is the return value
    result which is passed back to the calling
    function underneath.

foo
var1
var2
var3
29
Removing Modules from the Stack
  • When the active function completes its
    instructions, its frame is popped off the stack
    and all of its data dies.
  • The only thing that survives is the return value
    result which is passed back to the calling
    function underneath.

foo
var1
var2
var3
30
How Parameters Work
  • Formal parameters get a copy of the matching
    actual parameter.
  • In the calling function
  • bar (var1, var3)
  • In the called function
  • function ans bar(this_var, that_var)
  • ...

31
How Parameters Work
  • Formal parameters receive a copy of the matching
    actual parameter.

4
4
7
9
this_var
that_var
other_var
32
OPTIONAL SLIDES
  • We have inserted a number of optional slides that
    trace the behavior of the activation stack for an
    example.
  • You can safely skip over these next 8 slides (and
    their animations) and continue with the rest of
    the presentation. In fact, if you are in
    SLIDESHOW mode, this should happen automatically,
    and you will never see these hidden slides
    anyway!
  • If you persist, you will gain a good
    understanding of exactly what is going on in
    Matlab and many other situations when functions
    or methods are invoked and how parameters are
    passed back and forth between the calling and the
    called programs. This may go a long way to
    explain some of the behavior you have noticed.

33
Stack Trace Example
Function pos neg roots( A, B, C ) discrim
B2 4 A C pos (-B
sqrt(discrim)) / (2A) neg (-B -
sqrt(discrim)) / (2A)
34
Abstraction
roots
pos
math
neg
35
roots
36
roots
37
roots
38
Summary
  • You should now know
  • Memory consists of a Heap and a Stack for each
    program running
  • Stack as an Abstract Data Type
  • Nature of the Activation Stack
  • OPTIONAL Tracing program execution using the
    Activation Stack

39
Questions?
40
(No Transcript)
Write a Comment
User Comments (0)
About PowerShow.com