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 (Serial USB) electrical signal Bus hardware “interrupt” CPU Operating System “oooh, a keypress event” event handler Application
Event Loop Modern graphical programs wait in an infinite “event loop,” waiting for some events to happen. update(); draw(); update(); draw(); update(); draw();
Event Handling When an event occurs, call a function. an “event handler”
OF Event Handlers • keyPressed • keyReleased • mouseMoved • mouseDragged • mousePressed • mouseReleased • windowResized
Memory & Functions • Stack • Stack Frames • The Heap
The Stack LIFO
? int main() { 5 int x = 2; 2 int y = 5; int z = sum(x, y); }
int sum(int x, int y) { return x + y; } 5 local variables 2 ? int main() { 5 int x = 2; 2 int y = 5; int z = sum(x, y); }
int sum(int x, int y) { return x + y; } 7 stack frame 5 2 ? int main() { 5 int x = 2; 2 int y = 5; int z = sum(x, y); }
int sum(int x, int y) { return x + y; } 5 int main() { 5 int x = 2; 2 int y = 5; int z = sum(x, y); }
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.
Problem Things created within functions “go away” after the function returns... ... and sometimes, we want those “things” to stick around after the function returns.
Problem Storing too much on the stack can cause “stack overflow” http://stackoverflow.com
Problem In many cases, we do not know how much memory to allocate at compile time. We must allocate memory dynamically, during run-time.
Is there another place we can store stuff?
The Heap aka “the free store” A pool of unused memory, for your programs to use.
Stack vs. Heap organized unorganized efficient less efficient accessed “directly” (but we don’t care) storage of “things” dynamic storage known in advance and more... and more...
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?
How can your code access data whose locations are not known in advance? Pointers (to be continued)
Homework Choose final project partners and send your instructor an email.
Recommend
More recommend