VCE Software Development Theory Slideshows

Post on 12-Jan-2016

22 views 0 download

description

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?. - PowerPoint PPT Presentation

Transcript of VCE Software Development Theory Slideshows

VCE Software Development Theory Slideshows

By Mark Kellymark@vceit.com

Vceit.com

Stacks

Contents

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

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.

Coin stacksYou 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.

Magazine stacksYou 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.

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.

Important

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

Example

• To store 2 numbers temporarily…

• The PUSH command adds an item to the stack

• PUSH 11 …

11

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

2211

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….

2211

Example

• And the stack now look like this…

11

Visualising stacks• Stacks can be visualised as top-

down or bottom up.• You will see both versions

around, so be flexible.

2211

1122

Top-down visualisation

Bottom-up visualisation

Top >>

<<Top

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.

Stack pointers

• In a real, working stack, instead of moving stack items up and down, you just change a stack pointer.

• The value stored in the stack pointer refers to the address of top of the stack.

• Existing stack items stay where they are.

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.

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.

• 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.

index value43210

An empty stack

index value43210

TOP 0

The arrow points to the top of the

stack

TOP = 1 (the array index where the next push will be stored)

PUSH 11

index value43210 11

TOP = 1 (the array index where the next item will be pushed)

TOP 1

In this stack implementation,

the stack pointer is updated after an

operation and points to the slot to

use for the next operation.

PUSH 11

index value43210 11

TOP 0

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)

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’.

PUSH 22

index value4321 220 11

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

TOP 2

PUSH 33

index value432 331 220 11

TOP = 3 (the array index where the next push will be made)

TOP 3

PUSH 33

index value432 331 220 11

TOP = 3 (the array index where the next push will be made)

TOP 3

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.

POPindex value

4321 220 11

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

TOP 2

POPindex value

4321 220 11

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

TOP 2

POP retrieved the item at the top of the stack (index 3) and decremented TOP to point to the next available item.

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

Other stack commands• ROTATE – rolls the stack items around by 1 place. Some

implementations have flavours: rotate left, and rotate right.

index value4 553 442 331 220 11

index value4 443 332 221 110 55

Before rotate After rotate

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.

From VCAA’s sample exam questions*

*http://www.vcaa.vic.edu.au/vcaa/vce/studies/infotech/softwaredevel/IT-softwaredev-samp-w.pdf

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)?

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.

???

• We can only wonder why the bottom 2 stack items are bolded.

• The question does not explain this convention.

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.

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.

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.

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:

Stack

83

52

But it may finally explain the mysterious bolding of the 2 values in the question!

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)”.

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

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

3

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 Item Added 75

3

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 Item Added 75Item Added 92

3

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 Item Added 75Item Added 92

3 Stack full

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.

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.

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)

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.

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.

3

The attempt to push 47 would, according to the explanation, result in the output “Stack full”.

The stack pointer remains unchanged.

3 Item Added 75Item Added 92

3 Stack full

3

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 Item Added 75Item Added 92

3 Stack full

To summariseStack Starts at Push 23 Push 18 Pop Push 75 Push 92 Push 47

4 92 92

3 18 75 75 75

2 23 23 23 23 23 23

1 83 83 83 83 83 83 83

0 53 53 53 53 53 53 53

TOP= 1 2 3 2 3 4 4

Homework• Write a program that simulates the behaviour

of the stack in the sample exam question, including its behaviour when it’s full or empty.

• It can be top-down if that’s easier for you.• Test its behaviour with the data in the sample

question.• Add further test data to verify its ‘stack

empty’ behaviour.• Create a testing table to record your testing.

Extension tasks

• Make the stack’s contents & SP visible onscreen.

• Add the PEEK command.• Add the SWAP command.• Add the ROTATE command.

By Mark Kellymark@vceit.comvceit.com

These slideshows may be freely used, modified or distributed by teachers and students anywhere on the planet (but not elsewhere).

They may NOT be sold. They must NOT be redistributed if you modify them.

VCE IT THEORY SLIDESHOWS