Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and data structures

Similar presentations


Presentation on theme: "Algorithms and data structures"— Presentation transcript:

1 Algorithms and data structures
Protected by

2 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 Algorithms and data structures, FER

3 Memory assignment Function call mechanism System stack

4 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 – 0x 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

5 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

6 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

7 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

8 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

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

11 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

12 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

13 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

14 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

15 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

16 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

17 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

18 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

19 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

20 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

21 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


Download ppt "Algorithms and data structures"

Similar presentations


Ads by Google