The Program Life Cycle. - Concordia University Wisconsin

Download Report

Transcript The Program Life Cycle. - Concordia University Wisconsin

Day 8.
Stacks 3:
1. Questions on program 3.
2. Computer arithmetic continued.
3. Intro to Dynamic memory.
1. Questions on program 3.
Note:
(1) Using TEMPSTAK.H means that your
implementation file will be generated
automatically. So you only need 2 files,
TEMPSTAK.H and the driver. You do not
need to code a TEMPSTAK.CPP
implementation file.
Questions on program 3.
(2) GETTOKENS.CPP will give you a
starting point for your program, but you
will need to use a stack in both
ProcessOperator and ProcessOperand.
STAKDRV2.CPP shows how to pass an
instance of the Stack template class to a
function. You will need to pass this
parameter to ProcessOperator and
ProcessOperand.
2. Computer Arithmetic continued.
A. Outline.
Recall that the compiler uses stacks both to translate
and execute code.
Last time, we focused on the use of a stack to
execute arithmetic in postfix.
But, we write our C++ source code in infix notation.
[Recall: what is the difference between infix and
postfix?]
Computer Arithmetic continued
(cont.).
How, then, is our source code translated from infix to
postfix in the first place?
This also uses stacks.
[With the voice of Henry Kissinger: “Yeah, baby.”]
And why? “Because infix is…..but postfix is….”
[Select from the following list: “Ambiguous,”
“Yoda,” “Unambiguous,” “Yoder.”]
Computer Arithmetic continued
(cont.).
The idea is to avoid having to translate rules
of precedence into binary and having to do
multiple passes of the same expression,
making the machine code more compact
and faster to execute.
Note also that no parentheses are required in
postfix. Why not?
Computer Arithmetic continued
(cont.).
B. Translating from infix to postfix.
Idea: use 2 parallel stacks (or one could use a
stack of structs with 2 fields).
One stack will be used to store operators, the
other, parallel stack will hold their
associated precedence level (a number).
Recall that C++ has very simple precedence
rules:
Computer Arithmetic continued
(cont.).
1) *, / have the highest precedence;
+, - have lower precedence;
2) If 2 operators have equal precedence,
evaluate left to right;
3) Rules 1 and 2 can be overridden by the use
of parentheses.
Computer Arithmetic continued
(cont.).
To implement this, we assign * and / a
precedence of 2, and + and – a precedence
of 1. We will also allow precedence to be
promoted by parentheses, using a variable
PrecedenceProm.
If we encounter a left parenthesis, we add 3
to PrecedenceProm; if we encounter a
right parenthesis….
Computer Arithmetic continued
(cont.).
How does it all fit together?
Consider how we get from the infix input:
a * (b+c) – d
to the postfix output:
abc+*d–
See the handout, work through.
Intro to dynamic memory (cont).
Standard memory is referred to by an
identifier.
e.g. int Num; // reserves a location called Num
With dynamic allocation, the memory has no
assigned name, but we still have to access
it.
This can be done indirectly, through pointers.
Intro to dynamic memory (cont).
But what is a pointer?
It’s a sharp thing that…
“Stop, stop it, this is getting silly. I like a good
laugh too, but…..” Yours sincerely,
Brigadier General, Sir Lefty VoleStrangler (Mrs.)
A pointer is a special variable that does not
contain a data value but a dress.
Intro to dynamic memory (cont).
I mean, an address. That was a long time ago an
only a play…
The pointer contains the address of another location
in memory.
E.g. int *IntPointer; // does not declare an integer,
but a pointer that can be used to point to a
dynamically allocated integer.
To make sure the pointer does not accidentally refer
to an actual memory address (which might
already be reserved and/or protected), we can
initialize the pointer to NULL.
Intro to dynamic memory (cont).
Note that a pointer can only point to one
particular data type, because data types are
stored differently. If we need a pointer to a
character, we could not use an integer
pointer.
We would need to code:
char *Char_Pointer = NULL;
Intro to dynamic memory (cont).
How are pointers used?
For 3 main purposes:
1) Allocating dynamic memory;
2) Accessing a dynamic variable
[“De-referencing” the pointer];
3) De-allocating dynamic memory.
Intro to dynamic memory (cont).
1) Allocating dynamic memory.
This uses the new function.
Pointer = new data type
E.g.
IntPointer = new int;
CharPointer = new char;
Intro to dynamic memory (cont).
The new operation does 4 things:
1)Find the first available chunk of memory
big enough (may not be trivial);
2) Dynamically allocate variable;
3) Reserve that location for use;
4) Store the address in the pointer.
See diagram.
Intro to dynamic memory (cont).
2) Accessing a dynamic variable.
To do this we need to “de-reference” the pointer.
Analogy: while an Augustinian monk, Luther
walked to Rome. When he saw a sign (pointer)
to Rome, he de-referenced the sign by walking
to Rome (the dynamic variable).
I should NOT say IntPointer = 27; // WHY?
Intro to dynamic memory (cont).
This would be attempting to assign a data
value to a pointer, instead of an address.
Instead, code:
*IntPointer = 27;
In this case, the * means “de-reference the
pointer,” I.e. go from the pointer to the
thing pointed to.
Intro to dynamic memory (cont).
3. De-allocating dynamic memory.
When the dynamic variable is no longer we
needed, we can free it up for other use by
de-allocating it (garbage collection). This
is accomplished by
delete IntPointer;
Nothing is erased but that memory location is
marked as free in the memory map.