Algorithms and data structures

Slides:



Advertisements
Similar presentations
HardBound: Architectural Support for Spatial Safety of the C Programming Language Joe Devietti *, Colin Blundell, Milo Martin, Steve Zdancewic * University.
Advertisements

Algorithms and data structures Protected by
Dynamic Memory Allocation in C.  What is Memory What is Memory  Memory Allocation in C Memory Allocation in C  Difference b\w static memory allocation.
The University of Adelaide, School of Computer Science
Run-time Environment for a Program different logical parts of a program during execution stack – automatically allocated variables (local variables, subdivided.
Computer Architecture CSCE 350
CPS3340 COMPUTER ARCHITECTURE Fall Semester, /17/2013 Lecture 12: Procedures Instructor: Ashraf Yaseen DEPARTMENT OF MATH & COMPUTER SCIENCE CENTRAL.
Procedures II (1) Fall 2005 Lecture 07: Procedure Calls (Part 2)
The University of Adelaide, School of Computer Science
Algorithms and data structures Protected by
Chapter 8 Runtime Support. How program structures are implemented in a computer memory? The evolution of programming language design has led to the creation.
1 Storage Registers vs. memory Access to registers is much faster than access to memory Goal: store as much data as possible in registers Limitations/considerations:
Digression on Stack and Heaps CS-502 (EMC) Fall A Short Digression on Stacks and Heaps CS-502, Operating Systems Fall 2009 (EMC) (Slides include.
CS 536 Spring Run-time organization Lecture 19.
Run-Time Storage Organization
Run time vs. Compile time
C and Data Structures Baojian Hua
Run-time Environment and Program Organization
Stacks and HeapsCS-502 Fall A Short Digression Stacks and Heaps CS-502, Operating Systems Fall 2007 (Slides include materials from Operating System.
Memory Layout C and Data Structures Baojian Hua
LUNG HEALTH IN EUROPE FACTS AND FIGURES. KEY AIMS ‘Lung Health in Europe – Facts and Figures’ is a concise version of the European Respiratory Society.
Chapter 8 :: Subroutines and Control Abstraction
Chapter 7: Runtime Environment –Run time memory organization. We need to use memory to store: –code –static data (global variables) –dynamic data objects.
Security Exploiting Overflows. Introduction r See the following link for more info: operating-systems-and-applications-in-
Algorithms and data structures Protected by
Runtime Environments What is in the memory? Runtime Environment2 Outline Memory organization during program execution Static runtime environments.
13/02/2009CA&O Lecture 04 by Engr. Umbreen Sabir Computer Architecture & Organization Instructions: Language of Computer Engr. Umbreen Sabir Computer Engineering.
Runtime Environments Compiler Construction Chapter 7.
The Animated Sequence Chapter 5.1 in Sketching User Experiences: The Workbook.
Copyright © 2005 Elsevier Chapter 8 :: Subroutines and Control Abstraction Programming Language Pragmatics Michael L. Scott.
RUN-Time Organization Compiler phase— Before writing a code generator, we must decide how to marshal the resources of the target machine (instructions,
Algorithms and data structures Protected by
ITCS 3181 Logic and Computer Systems 2015 B. Wilkinson Slides4-2.ppt Modification date: March 23, Procedures Essential ingredient of high level.
Processes CS 6560: Operating Systems Design. 2 Von Neuman Model Both text (program) and data reside in memory Execution cycle Fetch instruction Decode.
Design of Everyday Things Part 2: Useful Designs? Lecture /slide deck produced by Saul Greenberg, University of Calgary, Canada Images from:
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
CSC 8505 Compiler Construction Runtime Environments.
RUNTIME ENVIRONMENT AND VARIABLE BINDINGS How to manage local variables.
Sven Koerber-Abe, 2013 Begrüßen und Verabschieden Begrüßen und Verabschieden.
Int main( ) { x = a(); } int a() { y = b(); } int b() { z = c(); } int c() { } 1.
Variables Bryce Boe 2012/09/05 CS32, Summer 2012 B.
Grammatik: sollen Sven Koerber-Abe, 2015.
Grammatik: nehmen, essen, möchten
Grammatik: wohnen mögen
Procedures (Functions)
Chapter 9 :: Subroutines and Control Abstraction
Training of Trainers Workshop
Writing the Methods Section
Einkaufen in Deutschland
Chap. 8 :: Subroutines and Control Abstraction
Chap. 8 :: Subroutines and Control Abstraction
Writing the Results Section
Training of Trainers Workshop
Memory Allocation CS 217.
Preparing Tables and Figures: Some Basics
The University of Adelaide, School of Computer Science
Understanding Program Address Space
The Structure of Journal Articles
Grammatik: Das ist ein …
Barbara Gastel INASP Associate
Ja / Doch Sven Koerber-Abe, 2013.
Grammatik: wohnen mögen
Writing the Introduction
C (and C++) Pointers April 4, 2019.
Grammatik: ich, mich Sven Koerber-Abe, 2014.
The C Language: Intro.
Runtime Environments What is in the memory?.
Where is all the knowledge we lost with information? T. S. Eliot
Grammatik: nehmen, essen, möchten
Run-time environments
Presentation transcript:

Algorithms and data structures 10.4.2017. Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/

Creative Commons You are free to: Under the following terms: share — copy and redistribute the material in any medium or format adapt — remix, transform, and build upon the material Under the following terms: Attribution — You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. NonCommercial — You may not use the material for commercial purposes. ShareAlike — If you remix, transform, or build upon the material, you must distribute your contributions under the same license as the original. No additional restrictions — You may not apply legal terms or technological measures that legally restrict others from doing anything the license permits. Riječ-dvije o velikim slovima na početku bulleta: Ako je u pitanju rečenica, slovo je veliko. rečenicu prepoznajete po posljednjem znaku (npr. točka, uskličnik, upitnik; ne i dvotočka!)  inače je malo Notices: You do not have to comply with the license for elements of the material in the public domain or where your use is permitted by an applicable exception or limitation. No warranties are given. The license may not give you all of the permissions necessary for your intended use. For example, other rights such as publicity, privacy, or moral rights may limit how you use the material. Text copied from http://creativecommons.org/licenses/by-nc-sa/3.0/ Algorithms and data structures, FER 10.4.2017.

Memory assignment Function call mechanism System stack 10.4.2017.

Process: a created program instance Virtual memory Process: a created program instance When a process is activated, a part of physical memory is assigned to it It is virtual memory – the process feels like having assigned to it the whole computer memory The process is not aware of how that virtual memory is mapped into the physical one – only the size and the beginning address are known – 0x00000000 for 32-bit architecture The process is not aware of virtual memories of other processes Even if it knew, it cannot access them physically (prevented by the OS) At each memory access (read, write), mapping between virtual and physical addresses is performed The mapping must be fast, because it occurs very often Algorithms and data structures, FER 10.4.2017.

Depending on the operating system Memory layout Lower addresses Depending on the operating system TEXT The stored program DATA Initialised global and static local variables BSS Uninitialised global and static local variables Heap Dynamically allocated memory (malloc) Stack Local variables of functions and stack frames It is on the bottom (highest addresses) TEXT DATA BSS HEAP STACK Higher addresses Algorithms and data structures, FER 10.4.2017.

Storage for execution code and constants Memory segments - TEXT Storage for execution code and constants TEXT DATA BSS char *word = “Hello"; int iSize; char *func() { char *p; iSize = 8; p = malloc(iSize); return p; } HEAP STACK Algorithms and data structures, FER 10.4.2017.

Memory segments - DATA i BSS Global variables, static local variables DATA: initialised in code BSS: uninitialised in code TEXT DATA BSS char *word= "Zdravo"; int iSize; char *func() { char *p; iSize = 8; p = malloc(iSize); return p; } HEAP STACK Algorithms and data structures, FER 10.4.2017.

Memory segments - heap Dynamic memory malloc, realloc TEXT DATA BSS char *word= “Hello"; int iSize; char *func() { char *p; iSize = 8; p = malloc(iSize); return p; } HEAP STACK Algorithms and data structures, FER 10.4.2017.

Memory segments - stack Temporary memory, while a function is executed grows upwards (to lower addresses) TEXT DATA BSS char *word= “Hello"; int iSize; char *func() { char *p; iSize = 8; p = malloc(iSize); return p; } HEAP STACK Algorithms and data structures, FER 10.4.2017.

Program sequence at function call How x is transferred? int main () { ... y1 = f(x1); y2 = f(x2); y3 = f(x3); } float f (float x) { ... return y; } Which way to return? Algorithms and data structures, FER 10.4.2017.

Temporary storage of variables and return addresses System stack Temporary storage of variables and return addresses Data structure of type LIFO (Last In First Out) Newer elements are stored on lower memory addresses Putting on stack: push Taking from stack: pop C B B B A A A A A Algorithms and data structures, FER 10.4.2017.

Stack is a collection of stack frames A stack frame contains: Return address to go to after the execution of the called function Local variables of the function Arguments (parameters) of the function Processor registers (depending on the compiler and its options) The stack also contains the base pointer Starting address for allocation of arguments and local variables for their easier handling In figures, address above which upon return everything may be cleared For generality, stack frame and base pointer shall not be considered here Algorithms and data structures, FER 10.4.2017.

Stack frame of the function main When a program is started, there is only one stack frame on the stack - the one belonging to the function main In the following examples this frame shall be omitted For simplicity reasons, in the stack there shall be presented: Function arguments Return address Local variables main Algorithms and data structures, FER 10.4.2017.

Program sequence and stack at function call int main () { ... y1 = f(x1); a) y2 = f(x2); b) y3 = f(x3); c) } x1 float f (float x) { ... return y; } b) x2 c) x3 Algorithms and data structures, FER 10.4.2017.

Program sequence and stack at function call – a more complex example float f (float x) { ... g(x); c) return z*z; } int main () { ... y1 = f(x1); a) y2 = g(x2); b) } c) x void g (float x) { ... return; } a) b) x1 x2 Algorithms and data structures, FER 10.4.2017.

Program sequence and stack at function call – even more complex example float f (float x) { float z; ... z = g(x); return z*z; } float g (float w) { float y; ... return y; } float f (float x) { float z; ... z = g(x); return z*z; } int main () { ... y1 = f(x1); y2 = g(x2); } y Ret.addr. x z z z Ret.addr. Ret.addr. Ret.addr. x1 x1 x1 Algorithms and data structures, FER 10.4.2017.

Program sequence and stack at function call – a more complex example float f (float x) { float z; ... z = g(x); return z*z; } int main () { ... y1 = f(x1); y2 = g(x2); } float g (float w) { float y; ... return y; } int main () { ... y1 = f(x1); y2 = g(x2); } z y Ret.addr. Ret.addr. x2 x1 Algorithms and data structures, FER 10.4.2017.

Function call by value x 1 y 1 x 1 y 1 2 x 1 #include <stdio.h> int x; void f (int y) { y = 2; } int main () { x = 1; f(x); ... return 0; main stack function call Ret.addr. x 1 y 1 execution y=2 x 1 Ret.addr. y 1 2 after return x 1 Algorithms and data structures, FER 10.4.2017.

Function call by reference – 1 #include <stdio.h> void exchange (short *x, short *y) { short aux; aux= *x; *x = *y; *y = aux; } short a, b; int main () { a = 3; b = 5; exchange(&a, &b); return 0; main stack function call aux ? 3 Ret.addr. 0x102 a 5 x 0x102 0x100 b y 0x100 execution aux=*x aux ? 3 3 Ret.addr. 0x102 a 5 x 0x102 0x100 b y 0x100 Algorithms and data structures, FER 10.4.2017.

Function call by reference – 2 #include <stdio.h> void exchange (short *x, short *y) { short aux; aux = *x; *x = *y; *y = aux; } short a, b; int main () { a = 3; b = 5; exchange(&a, &b); return 0; main stack execution *x=*y aux 3 5 3 Ret.addr. 0x102 a 5 x 0x102 0x100 b y 0x100 execution *y=aux aux 3 5 Ret.addr. 0x102 a 3 5 x 0x102 0x100 b y 0x100 Algorithms and data structures, FER 10.4.2017.

Function call by reference – 3 #include <stdio.h> void exchange (short *x, short *y) { short aux; aux = *x; *x = *y; *y = aux; } short a, b; int main () { a = 3; b = 5; exchange(&a, &b); return 0; main stack return to main aux 3 5 Ret.addr. 0x102 a 3 x 0x102 0x100 b y 0x100 after return 5 0x102 a 3 0x100 b Algorithms and data structures, FER 10.4.2017.