CSCI261 Lecture 27: Events, Interrupts & Event Handling Memory, - - PowerPoint PPT Presentation

csci261
SMART_READER_LITE
LIVE PREVIEW

CSCI261 Lecture 27: Events, Interrupts & Event Handling Memory, - - PowerPoint PPT Presentation

CSCI261 Lecture 27: Events, Interrupts & Event Handling Memory, Stack, Stack Frames, The Heap Attribution: http://123rf.com keypress Keyboard Interface (Serial USB) Bus CPU Operating System Application keypress Keyboard Interface


slide-1
SLIDE 1

CSCI261

Lecture 27: Events, Interrupts & Event Handling Memory, Stack, Stack Frames, The Heap

slide-2
SLIDE 2

Attribution: http://123rf.com

slide-3
SLIDE 3

Keyboard Interface (Serial USB) Bus CPU Operating System Application

keypress

slide-4
SLIDE 4

Keyboard Interface (Serial USB) Bus CPU Operating System Application

keypress electrical signal hardware “interrupt”

“oooh, a keypress event” event handler

slide-5
SLIDE 5

Event Loop

Modern graphical programs wait in an infinite “event loop,” waiting for some events to happen. update(); draw(); update(); draw(); update(); draw();

slide-6
SLIDE 6

Event Handling

When an event occurs, call a function. an “event handler”

slide-7
SLIDE 7

OF Event Handlers

  • keyPressed
  • keyReleased
  • mouseMoved
  • mouseDragged
  • mousePressed
  • mouseReleased
  • windowResized
slide-8
SLIDE 8
slide-9
SLIDE 9

Memory & Functions

  • Stack
  • Stack Frames
  • The Heap
slide-10
SLIDE 10

The Stack

LIFO

slide-11
SLIDE 11

int main() { int x = 2; int y = 5; int z = sum(x, y); }

2 5

?

slide-12
SLIDE 12

int main() { int x = 2; int y = 5; int z = sum(x, y); }

2 5

?

int sum(int x, int y) { return x + y; }

2 5

local variables

slide-13
SLIDE 13

int main() { int x = 2; int y = 5; int z = sum(x, y); }

2 5

?

int sum(int x, int y) { return x + y; }

2 5 7

stack frame

slide-14
SLIDE 14

int main() { int x = 2; int y = 5; int z = sum(x, y); }

2 5

int sum(int x, int y) { return x + y; }

5

slide-15
SLIDE 15

Stack Frames

One aspect of where “scope” comes from. Contains parameter values, local variable values, and “bookkeeping” information. Function call? New stack frame. Function return? Pop the entire stack frame.

slide-16
SLIDE 16

Problem

Things created within functions “go away” after the function returns... ... and sometimes, we want those “things” to stick around after the function returns.

slide-17
SLIDE 17

Problem

Storing too much on the stack can cause “stack overflow” http://stackoverflow.com

slide-18
SLIDE 18

Problem

In many cases, we do not know how much memory to allocate at compile time. We must allocate memory dynamically, during run-time.

slide-19
SLIDE 19

Is there another place we can store stuff?

slide-20
SLIDE 20
slide-21
SLIDE 21

The Heap

A pool of unused memory, for your programs to use.

aka “the free store”

slide-22
SLIDE 22

Stack vs. Heap

  • rganized

efficient accessed “directly” storage of “things” known in advance and more... unorganized less efficient (but we don’t care) dynamic storage and more...

slide-23
SLIDE 23

Heap

The exact location of things on the heap is not known in advance. How can your code access data whose locations are not known in advance?

slide-24
SLIDE 24

How can your code access data whose locations are not known in advance?

Pointers

(to be continued)

slide-25
SLIDE 25

Homework

Choose final project partners and send your instructor an email.