1 / 21

Algorithms and data structures

Algorithms and data structures. Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/. Creative Commons. You are free to: share — copy and redistribute the material in any medium or format adapt — remix, transform, and build upon the material Under the following terms:

Download Presentation

Algorithms and data structures

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. Algorithms and data structures Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/

  2. Creative Commons • You are free to: • 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. 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 copiedfrom http://creativecommons.org/licenses/by-nc-sa/3.0/ Algorithms and data structures, FER

  3. Function call Memory assignment Function call mechanism System stack

  4. Virtual memory • Process: a created program instance • When a process is activated, a part of physical memory is assigned to it • It isvirtual 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

  5. 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. 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"; intiSize; 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"; intiSize; 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); returnp; } 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. 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 frame • 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 frameand 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 frameon 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 a) 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); ... } y z Ret.addr. Ret.addr. x2 x1 Algorithms and data structures, FER

  18. Function call by value #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. 1 x y 1 executiony=2 1 x Ret.addr. 2 y 1 after return 1 x 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 ? Ret.addr. 3 0x102 a 5 x 0x102 0x100 b y 0x100 executionaux=*x 3 aux ? 3 Ret.addr. 0x102 a x 0x102 5 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 x 0x102 5 0x100 b 0x100 y execution*y=aux aux 3 5 Ret.addr. 0x102 a x 0x102 3 5 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 x 0x102 3 0x100 b y 0x100 after return 5 0x102 a 3 0x100 b Algorithms and data structures, FER

More Related