Presentation is loading. Please wait.

Presentation is loading. Please wait.

Algorithms and data structures Protected by 13.4.2015.

Similar presentations


Presentation on theme: "Algorithms and data structures Protected by 13.4.2015."— Presentation transcript:

1 Algorithms and data structures Protected by

2 Creative Commons n You are free to: share — copy and redistribute the material in any medium or format share — copy and redistribute the material in any medium or format adapt — remix, transform, and build upon the material adapt — remix, transform, and build upon the material n 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. 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. 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. 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. Text copied from Algorithms and data structures, FER 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. 2 / 21

3 Function call Memory assignment Function call mechanism System stack

4 Algorithms and data structures, FER Virtual memory n Process: a created program instance n 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 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 At each memory access (read, write), mapping between virtual and physical addresses is performed – The mapping must be fast, because it occurs very often 4 / 21

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

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

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

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

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

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

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

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

13 Algorithms and data structures, FER 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 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 In the following examples this frame shall be omitted n For simplicity reasons, in the stack there shall be presented: Function arguments Function arguments Return address Return address Local variables Local variables main 13 / 21

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

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

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

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

18 Algorithms and data structures, FER Function call by value #include #include int x; void f (int y) { y = 2; } int main () { x = 1; x = 1; f(x); f(x); return 0; return 0;} mainstack 1 y Ret.addr. 1 Ret.addr. y 1 x x function call execution y=2 1 x after return / 21

19 Algorithms and data structures, FER Function call by reference Function call by reference – 1 #include 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; } mainstack 0x100 y 0x102 a function call execution aux=*x b 0x102 0x100 Ret.addr. ? x aux 0x100 y 0x102 3 a 5 b 0x102 0x100 Ret.addr. ? x aux / 21

20 Algorithms and data structures, FER Function call by reference Function call by reference – 2 #include 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; } mainstack 0x100 y 0x102 3 a execution *y=aux 5 b 0x102 0x100 Ret.addr. 3 x aux 0x100 y 0x102 5 a 5 b 0x102 0x100 Ret.addr. 3 x aux execution *x=*y / 21

21 Algorithms and data structures, FER Function call by reference Function call by reference – 3 #include 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; } mainstack 0x100 y 0x102 5 a 3 b 0x102 0x100 Ret.addr. 3 x aux return to main 5 a 3 b 0x102 0x100 after return 21 / 21


Download ppt "Algorithms and data structures Protected by 13.4.2015."

Similar presentations


Ads by Google