1 / 54

VCE Software Development Theory Slideshows

Stacks. VCE Software Development Theory Slideshows. By Mark Kelly mark@vceit.com Vceit.com. Contents. What are stacks? Logical stacks Implementing stacks Visualising stacks Stack pointers Other stack commands Common stack errors. What are they?.

jagger
Download Presentation

VCE Software Development Theory Slideshows

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Stacks VCE Software Development Theory Slideshows By Mark Kelly mark@vceit.com Vceit.com

  2. Contents • What are stacks? • Logical stacks • Implementing stacks • Visualising stacks • Stack pointers • Other stack commands • Common stack errors

  3. What are they? • Simple, sequential, temporary data storage structures • Used by CPUs to store things like procedure calls and data on recursive function calls. • Used in low-level programming • Related to: queues, buffers • First proposed in 1955, and patented in 1957 by the German Friedrich L. Bauer.

  4. Coin stacks You PUSH coins into the holder. You POP coins from the holder. The first coin added is the last coin out. In a stack, this is called FIRST IN LAST OUT (FILO) or LAST IN FIRST OUT (LIFO) Coins can only be pushed or popped from the top.

  5. Magazine stacks You PUSH shells into the magazine at its only opening at the top. You POP shells from the top of the magazine. The first shell added is the last shell out. Shells can only be added or removed from the top.

  6. Book stacks • Think of a data stack like a stack of books. The oldest items added to the stack are at the bottom, the newest additions at the top. • Books can only be added or removed at the top of the stack.

  7. Important • Only the top item on the stack is accessible at any given time.

  8. Example • To store 2 numbers temporarily… • The PUSH command adds an item to the stack • PUSH 11 …

  9. Example • PUSH 22 … • Notice how the most recently added number is at the top? • Values can only be added or removed from the top of the stack

  10. POP • To retrieve a value from the stack, use the POP command. • E.g. POP x takes the next value from the stack and stores it in variable x….

  11. Example • And the stack now look like this…

  12. Visualising stacks • Stacks can be visualised as top-down or bottom up. • You will see both versions around, so be flexible. <<Top Bottom-up visualisation Top-down visualisation Top >>

  13. Implementing a stack • While a logical (theoretical)stack can happily shuffle existing stacks items along to make room for a new item, this is inefficient to do in real life • Such memory manipulation is slow, processor-intensive and unnecessary.

  14. Stack pointers • In a real, working stack, instead of moving stack items up and down, you just change a stackpointer. • The value stored in the stack pointer refers to the address of top of the stack. • Existing stack items stay where they are.

  15. Stack Pointers • The stack pointer (SP, or “top”) is a value that points to the location (in RAM or in an array or linked list) of the top of the stack. • The top of the stack is where the next item will be pushed.

  16. Implementing stacks • Stacks often implemented in arrays or linked lists (which also use pointers instead of physically moving data around) • A CPU’s stack is implemented in RAM locations.

  17. Consider a stack created with an array of 5 elements. • Let’s call it “bottom up” since we’ll visualise the top of the stack being at the bottom. • This stack ‘slots’ are numbered 0 to 4. More on this later. • Note: some stacks’ numbering starting at 1.

  18. An empty stack The arrow points to the top of the stack TOP = 1 (the array index where the next push will be stored)

  19. PUSH 11 In this stack implementation, the stack pointer is updated after an operation and points to the slot to use for the next operation. TOP = 1 (the array index where the next item will be pushed)

  20. PUSH 11 In other stack implementations, the pointer points to the current top value and is updated before the next stack operation. TOP=0 (the array index where the current top item is)

  21. Be careful • Since stacks are implemented with different basic behaviours, you need to approach a stack problem carefully. • Some stacks are shown filling from the bottom, others from the top. • In some, the first element is 0; in others it’s 1. • Some update the stack pointer after each push or pop. Other update before an operation. • Some refer to the stack pointer as ‘Top’.

  22. PUSH 22 TOP = 2 (the array index where the next push will be made)

  23. PUSH 33 TOP = 3 (the array index where the next push will be made)

  24. PUSH 33 Notice that the existing stack items stay where they are. Only the stack pointer changes. But “33” is at the top of the stack. Last in, first out. TOP = 3 (the array index where the next push will be made)

  25. POP TOP = 2 (the array index where the next push will be made)

  26. POP POP retrieved the item at the top of the stack (index 3) and decremented TOP to point to the next available item. TOP = 2 (the array index where the next push will be made)

  27. Other stack commands • In addition to PUSH and POP, some stacks have extra commands they understand • PEEK – fetch the item at the top of the stack but leave it on the stack. Don’t change the SP. • SWAP – swap the top 2 items on the stack. Equivalent to: • POP x (x is a variable where the popped item is put) • POP y • PUSH x • PUSH y

  28. Other stack commands • ROTATE – rolls the stack items around by 1 place. Some implementations have flavours: rotate left, and rotate right. Before rotate After rotate

  29. Common Stack Errors • “Stack Empty” – when you try to POP when the stack is empty. • “Stack overflow” – when you try to PUSH a value into a stack that has no more free space available.

  30. From VCAA’s sample exam questions* *http://www.vcaa.vic.edu.au/vcaa/vce/studies/infotech/softwaredevel/IT-softwaredev-samp-w.pdf

  31. Decoding the depiction of the stack • There are no real standards for depicting stacks. • You have to determine things that the question writer assumed when they show their stack’s behaviour. • First: is the stack top-down or bottom up? • Is the top of the stack at the 92 end (top-down) or the 52 end (bottom-up)?

  32. Let’s assume that the events in the table are in chronological order. Push 23 occurred before push 18. Let’s also assume that “Top” is a variable containing the value of the stack pointer: the current active stack position. How does the table relate to the stack? We can only assume that the stack is shown as it is after the events in the table have been carried out.

  33. ??? • We can only wonder why the bottom 2 stack items are bolded. • The question does not explain this convention.

  34. Let’s see if we can read it. According to the table, after the PUSH 23, the stack pointer (“Top”) is 2. In the stack, ’23’ appears in slot 3 (counting from both the top and the bottom). Therefore if top’s value is 2 and it’s in position 3, we know the index number of the stack’s first slot is zero. So the stack’s slots are numbered zero to 4, not 1 to 5.

  35. Relearning how to count • In computing, counting often starts with item 0 instead of item 1. • E.g. array indexes (and Visual Basic listboxes) often begin with 0, so an array with 100 slots would be numbered 0 to 99. • Forgetting this and trying to count to the number of items (e.g. 100) is a common error.

  36. If Top=1, and the stack is bottom-up, the current stack value is 83. The next free slot for a push is Top + 1 (i.e. slot 2). Apparently this stack updates the pointer immediately before a push/pop, rather than after it. Other stacks update the SP immediately after an operation.

  37. CONFUSED BY THIS STACK? (as I was) The 23,75 and 92 shown in the stack don’t actually exist yet if you’re following the history of operations in the table. At the start of the table’s events - before the push 23 - the stack actually looks like this: But it may finally explain the mysterious bolding of the 2 values in the question!

  38. If we push 23, it goes into stack slot numbered 2 (the one physically third from the bottom). TOP is updated to equal 2. In the table, you can see the stack’s state after the push: top=2 and the output is “Item added (23)”.

  39. In the next operation, PUSH 18, the value 18 is put on the stack in slot 3. So in the table you fill in “3” into the empty cell. 3

  40. The next operation is a POP so 18 is removed from the stack (which explains why the displayed stack does not show it) and the pointer is decremented (reduced by 1) to 2. 3

  41. The next operation is a PUSH 75, so ‘75’ goes into slot 3 and the pointer is incremented to 3 again. So we put “3” into the empty ‘TOP’ and the output would be “Item added (75)” 3 3 Item Added 75

  42. The next operation is a PUSH 92, so ‘92’ goes into slot 4 and the pointer is incremented to 4. So the output to add to the table would be “Item added (92)” 3 3 Item Added 75 Item Added 92

  43. The final operation, Push 47 causes a problem. The stack pointer is already at its maximum value. It cannot be incremented – in other words the stack is full. 3 3 Item Added 75 Item Added 92 3 Stack full

  44. Why check for stack overflow? Checking for stack overflow is an essential part of a stack implementation. Failing to check for overflow could lead to serious problems.

  45. Why check for stack overflow? If the stack is implemented in an array, overflow would create a runtime error when undeclared elements of an array were accessed.

  46. Why check for stack overflow? If the stack is in RAM, writing outside the stack’s permitted area could overwrite and corrupt program code or other data. (This is deliberately done in some code injection hacking exploits to gain unauthorised entry into some software)

  47. Why check for stack overflow? If the stack is in RAM, writing outside the stack’s permitted area could overwrite and corrupt program code or other data.

  48. Why check for stack overflow? Tip: If your language supports dynamic arrays (which can be resized during runtime) this is a neat way to create resizable stacks and avoid overflow. In VB, use REDIM PRESERVE.

  49. The attempt to push 47 would, according to the explanation, result in the output “Stack full”. The stack pointer remains unchanged. 3 3 Item Added 75 Item Added 92 3 Stack full

  50. A lot of thinking for 6 marks! One hopes the real exam won’t have such a question with so many mysteries and vagaries that the poor student has to interpret before attempting to answer the question. 3 3 Item Added 75 Item Added 92 3 Stack full

More Related