CS301-Lec09 handout - CS301 Data Structures Lecture No 09...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
CS301 – Data Structures Lecture No. 09 ___________________________________________________________________ Data Structures Lecture No. 09 Reading Material Data Structures and Algorithm Analysis in C++ Chapter. 3 3.3.3, 3.4.1, 3.4.2 Summary Memory Organization Stack Layout During a Function Call Queues Queue Operations Implementing Queue Queue using Array Use of Queues Memory Organization By the end of last lecture, we discussed the uses of stack to develop a process from an executable file and then in function calls. When you run an executable, the operating system makes a process inside memory and constructs the followings for that purpose. - A code section that contains the binary version of the actual code of the program written in some language like C/C++ - A section for static data including global variables - A stack and - Finally, a heap Page 1 of 13
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
CS301 – Data Structures Lecture No. 09 ___________________________________________________________________ Stack is used in function calling while heap area is utilized at the time of memory allocation in dynamic manner. Fig 1. Memory Organization Stack Layout during a Function Call The above diagrams depict the layout of the stack when a function F calls a function G . Here sp stands for stack pointer. At the very left, you will find the layout of the stack just before function F calls function G . The parameters passed to function F are firstly inserted inside the stack. These are followed by the local variables of the function F and finally the memory address to return back after the function F finishes. Just before function is made to the function G , the parameters being passed to the function G, are Page 2 of 13 Process 1 (Browser) Process 3 (Word) Process 4 (Excel) Process 2 (Dev-C++) Windows OS Code Static Data Stack Heap Parameters (F) Local variables (F) Return address (F) Parameters (G) Parameters (F) Local variables (F) Return address (F) Parameters (G) Local variables (G) Return address (G) At point of call During Execution of G sp sp Parameters (F) Local variables (F) Return address (F) After Call sp Fig 2: Stack Layout; When function F calls function G
Background image of page 2
CS301 – Data Structures Lecture No. 09 ___________________________________________________________________ inserted into the stack. In the next diagram, there is layout of the stack on the right side after the call to the function G . Clearly, the local variables of the function G are inserted into the stack after its parameters and the return address. If there are no local variables for a function, the return address is inserted (pushed) on to the stack. The layout of the stack, when the function G finishes execution is shown on the right. You can see that the local variables of function G are no more in the stack. They have
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 05/21/2010 for the course CS CS301 taught by Professor Dr.naveed malik during the Spring '10 term at Virtual University of Pakistan.

Page1 / 13

CS301-Lec09 handout - CS301 Data Structures Lecture No 09...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online