Understanding Stack Behavior in Programming
140 likes | 235 Vues
Learn about the classical semantics of stacks, behavioral methods, program execution using stacks, activation records, JVM stack settings, heap settings, recursion, and boundary fill algorithms in computer science.
Understanding Stack Behavior in Programming
E N D
Presentation Transcript
Recursion CS-2851Dr. Mark L. Hornick
Review: Stacks • Classical semantics (behavior) • Elements can only be inserted and removed from the front of the collection • This is known as Last-In, First-Out (LIFO) • Random-access is not defined CS-2851Dr. Mark L. Hornick
Stack – behavioral methods • The naming for the structure and the methods is an analogy for how the data elements within the structure are accessed • Principal methods that define behavior: • push() – place an element on (top of) the stack • pop() – return and remove an element from the (top of the) stack • peek() – return the top element of the stack • Without removing (popping) it CS-2851Dr. Mark L. Hornick
Program execution and the Stack • Whenever a method is called, information related to the method call is stored • within a data structure that exhibits behavior of a Stack • This information is referred to as an activation record CS-2851Dr. Mark L. Hornick
The program execution Stack • Stack frame/activation record • Each activation record contains: • A variable that contains the return address in the calling method • For each parameter in the called method, a variable that contains a copy of the corresponding argument • For each local variable declared in the called method, a variable that contains a copy of that declared variable. • For a method A calling a method B • The activation record for A is pushed before the call to B • Popped when the method B completes execution CS-2851Dr. Mark L. Hornick
Stack settings for JVM Each thread in the JVM uses a stack for storing activation records The –Xss<nk> JVM setting • sets the maximum stack size that can be used by Java code in each thread to n kilobytes. • The default units for n are bytes. n must be > 1k bytes. • The default stack size is 400 kilobytes ("-Xss400k") • "k" for kilobytes or "m" for megabytes CS-2851Dr. Mark L. Hornick
Heap settings for JVM Each thread in the JVM uses the heap for creating new objects The –Xms<n>m JVM setting • sets the initial heap size that is used by Java code in each thread to m megabytes. • The default units for n are bytes. • The initial heap size is 128MB ( same as "-Xms128m“) The –Xmx<n>m JVM setting • sets the maximumheap size • The default max heap size is 128MB ( same as "-Xmx128m“) CS-2851Dr. Mark L. Hornick
Factorial of x What algorithm would you use to compute x! ?
Recursive definition of x! Is there a way to make use of recursion?
Another Recursion Example:Graphics Fill Algorithm The “Bucket” tool of MS Paint • Drag the bucket to an interior point of a graphical shape • Paint interior outward toward boundary • Stop when all interior pixels are filled CS-2851Dr. Mark L. Hornick
Boundary Fill Patterns 4-connected 8-connected CS-2851Dr. Mark L. Hornick
Boundary Fill Algorithm details Return if current position is: • Boundary color • Fill color • Otherwise/else • Set fill color • Recursively try neighbors • North, East, South, West (arbitrary) • Each neighbor recursively performs algorithm until “return” • 8-connected also tries NE, NW, SW, SE CS-2851Dr. Mark L. Hornick
Boundary fill exercise CS-2851Dr. Mark L. Hornick