1 / 20

Day 8.

Day 8. Stacks 3: 1. Questions on program 3. 2. Computer arithmetic continued. 3. Intro to Dynamic memory. 1. Questions on program 3. Note:

nura
Télécharger la présentation

Day 8.

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. Day 8. Stacks 3: 1. Questions on program 3. 2. Computer arithmetic continued. 3. Intro to Dynamic memory.

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

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

  4. 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?]

  5. 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.”]

  6. 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?

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

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

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

  10. 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: a b c + * d – See the handout, work through.

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

  12. 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 Vole-Strangler (Mrs.) A pointer is a special variable that does not contain a data value but a dress.

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

  14. 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;

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

  16. 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;

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

  18. 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?

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

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

More Related