algorithms and data structures

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

Upload: quinlan-kennedy

Post on 03-Jan-2016

26 views

Category:

Documents


2 download

DESCRIPTION

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: - PowerPoint PPT Presentation

TRANSCRIPT

Algorithms and data structuresAlgorithms and data structures

Protected by http://creativecommons.org/licenses/by-nc-sa/3.0/hr/20.04.23

Creative CommonsCreative Commons

You are free to:You are free to: shareshare — copy and redistribute the material in any medium or format — copy and redistribute the material in any medium or format adaptadapt — remix, transform, and build upon the material — remix, transform, and build upon the material

Under the following terms: Under the following terms: Attribution Attribution — You must give— You must give appropriate credit, appropriate credit, provide a link to the license, and provide a link to the license, and

indicate if changes were madeindicate if changes were made. You may do so in any reasonable manner, but not . You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use. in any way that suggests the licensor endorses you or your use.

NonCommercial NonCommercial — You may not use the material for— You may not use the material for commercial purposes commercial purposes. . ShareAlike ShareAlike — If you remix, transform, or build upon the material, you must — If you remix, transform, or build upon the material, you must

distribute your contributions under the same license as the original.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 http://creativecommons.org/licenses/by-nc-sa/3.0/

20.04.23Algorithms 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

20.04.23

Function callFunction call

Memory assignmentMemory assignmentFunction call mechanismFunction call mechanismSystem stackSystem stack

Algorithms and data structures, FER 20.04.23

Virtual memoryVirtual memory

Process: a created program instanceProcess: a created program instance When a process is activated, a When a process is activated, a part of physical memorypart of physical memory is assigned is assigned

to itto it It isIt is virtual memoryvirtual memory – the process – the process feelsfeels like having assigned to it the whole like having assigned to it the whole

computer memorycomputer memory– The process is The process is not awarenot aware of how that virtual memory is mapped into the physical one – of how that virtual memory is mapped into the physical one –

only the size and the beginning address are known – 0x00000000 for 32-bit only the size and the beginning address are known – 0x00000000 for 32-bit architecturearchitecture

– The process is The process is not awarenot aware of virtual memories of other processes of virtual memories of other processes– Even ifEven if it knew it knew, it cannot access them physically (prevented by the OS), it cannot access them physically (prevented by the OS)

At each memory access (read, write), At each memory access (read, write), mapping mapping between between virtual and physical virtual and physical addresses is performedaddresses is performed

– The The mapping mapping must be fast, because it occurs very oftenmust be fast, because it occurs very often

4 / 21

Algorithms and data structures, FER 20.04.23

Memory layoutMemory layout

Depending on the operating systemDepending on the operating system TEXTTEXT

– The stored programThe stored program DATA DATA

– Initialised global and static local variablesInitialised global and static local variables BSSBSS

– Uninitialised global and static local variablesUninitialised global and static local variables HeapHeap

– Dynamically allocated memory (malloc)Dynamically allocated memory (malloc) StackStack

– Local variables of functions and stack framesLocal variables of functions and stack frames– It is on the bottom (highest addresses)It is on the bottom (highest addresses)

DATADATA

BSSBSS

HEAPHEAP

STACKSTACK

Lower addressesLower addresses

Higher addressesHigher addresses

TEXTTEXT

5 / 21

Algorithms and data structures, FER 20.04.23

Storage for execution Storage for execution code and constantscode and constants

Memory segmentsMemory segments - - TEXT TEXT

TEXTTEXT

DATADATA

BSSBSS

HEAPHEAP

STACKSTACK

char *char *wordword = = ““HelloHello"";;

int iSize;int iSize;

char *func() {char *func() {

char *p;char *p;

iSize = 8;iSize = 8;

p = malloc(iSize);p = malloc(iSize);

return p;return p;

}}

6 / 21

Algorithms and data structures, FER 20.04.23

Memory segmentsMemory segments - DATA i BSS - DATA i BSS

Global variables, static local variablesGlobal variables, static local variables DATA: initialised in codeDATA: initialised in code BSS: uninitialised in codeBSS: uninitialised in code

TEXTTEXT

DATADATA

BSSBSS

HEAPHEAP

STACKSTACK

char *char *wordword= "Zdravo";= "Zdravo";

intint iSizeiSize;;

char *func() {char *func() {

char *p;char *p;

iSize = 8;iSize = 8;

p = malloc(iSize);p = malloc(iSize);

return p;return p;

}}

7 / 21

Algorithms and data structures, FER 20.04.23

Memory segments - heapMemory segments - heap

Dynamic memoryDynamic memory mallocmalloc,, realloc realloc

TEXTTEXT

DATADATA

BSSBSS

HEAPHEAP

STACKSTACK

char *word= “Hello";char *word= “Hello";

int iSize;int iSize;

char *func() {char *func() {

char *p;char *p;

iSize = 8;iSize = 8;

p = p = malloc(iSize);malloc(iSize);

return p;return p;

}}

8 / 21

Algorithms and data structures, FER 20.04.23

Memory segments - stackMemory segments - stack

Temporary memory, while a function is executedTemporary memory, while a function is executed grows upwards (to lower addresses)grows upwards (to lower addresses)

TEXTTEXT

DATADATA

BSSBSS

HEAPHEAP

STACKSTACK

char *char *wordword= “= “HelloHello";";

int iSize;int iSize;

char *func() {char *func() {

char *p;char *p;

iSize = 8;iSize = 8;

pp = malloc(iSize); = malloc(iSize);

return return pp;;

}}

9 / 21

Algorithms and data structures, FER 20.04.23

Program sequence at function call

int main () {

...

y1 = f(x1);

...

y2 = f(x2);

...

y3 = f(x3);

...

}

float f (float x) {

...

return y;

}

How How x x is transferredis transferred??

Which way to returnWhich way to return??

10 / 21

Algorithms and data structures, FER 20.04.23

System stackSystem stack

Temporary storage of variables and return addressesTemporary storage of variables and return addresses Data structure of type LIFO (Last In First Out) Data structure of type LIFO (Last In First Out)

Newer elements are stored on Newer elements are stored on lower memory addresseslower memory addresses Putting on stack: Putting on stack: pushpush Taking from stack: Taking from stack: poppop

AA AABB

AABBCC

AABB

AA

11 / 21

Algorithms and data structures, FER 20.04.23

Stack frameStack frame

Stack is a collection of Stack is a collection of stack framesstack frames A stack frame contains:A stack frame contains:

Return addressReturn address to go to after to go to after the the execution of the called functionexecution of the called function Local variablesLocal variables of the function of the function ArgumentsArguments (parameters) of the function (parameters) of the function Processor registers (depending on the compiler and its options)Processor registers (depending on the compiler and its options)

The stack also contains the The stack also contains the base pointerbase pointer Starting address for allocation of arguments and local variables for their Starting address for allocation of arguments and local variables for their

easier handlingeasier handling In figures, address above which upon return everything may be clearedIn figures, address above which upon return everything may be cleared

For generality, For generality, stack framestack frame and and base pointerbase pointer shall not be considered shall not be considered herehere

12 / 21

Algorithms and data structures, FER 20.04.23

Stack frame of the function Stack frame of the function mainmain

When a program is started, there is only one stack frameWhen a program is started, there is only one stack frame on the on the stack - the one belonging to the function stack - the one belonging to the function mainmain In the following examples this frame In the following examples this frame shall be omittedshall be omitted

For simplicity reasons, in the stack For simplicity reasons, in the stack there there shall be presentedshall be presented:: Function argumentsFunction arguments Return addressReturn address Local variablesLocal variables

mainmain

13 / 21

Algorithms and data structures, FER 20.04.23

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;

}

x1x1a)a)

x2x2b)b)

x3x3c)c)

14 / 21

Algorithms and data structures, FER 20.04.23

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;

}

x1x1a)a)xxc)c)

x2x2b)b)

15 / 21

Algorithms and data structures, FER 20.04.23

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;

}

x1x1Ret.addr.Ret.addr.

zz

x1x1Ret.addr.Ret.addr.

zzxx

Ret.addr.Ret.addr.yy

x1x1Ret.addr.Ret.addr.

zz

16 / 21

Algorithms and data structures, FER 20.04.23

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;

}

x2x2Ret.addr.Ret.addr.

yy

int main () {

...

y1 = f(x1);

...

y2 = g(x2);

...

}

int main () {

...

y1 = f(x1);

...

y2 = g(x2);

...

}

x1x1Ret.addr.Ret.addr.

zz

17 / 21

Algorithms and data structures, FER 20.04.23

Function call by valueFunction call by value

#include <stdio.h>#include <stdio.h>

int x;int x;

void f (int y) {void f (int y) {

y = 2;y = 2;

}}

intint main () { main () {

xx = 1 = 1;;

f(x);f(x);

......

return 0;return 0;

}}

main stack

11yRet.addr.Ret.addr.

11Ret.addr.Ret.addr.

y

1x

x

ffunction callunction call

eexecution xecution y=2y=2

1x

aafter returnfter return

22

11

18 / 21

Algorithms and data structures, FER 20.04.23

Function call by reference 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

0x1000x100y0x1020x102

a

ffunction callunction call

executionexecution auxaux=*x=*x

b0x1020x100

Ret.addr.Ret.addr.??

x

aux

0x1000x100y0x1020x102

3a5b

0x1020x100

Ret.addr.Ret.addr.??

x

aux 33

3355

19 / 21

Algorithms and data structures, FER 20.04.23

Function call by referenceFunction 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

0x1000x100y0x1020x102

3a

executionexecution *y=aux*y=aux

5b0x1020x100

Ret.addr.Ret.addr.33

x

aux

0x1000x100y0x1020x102

5a5b

0x1020x100

Ret.addr.Ret.addr.33

x

aux

eexecution xecution *x=*y*x=*y

55

33

20 / 21

Algorithms and data structures, FER 20.04.23

Function call by referenceFunction 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

0x1000x100y0x1020x102

5a3b

0x1020x100

Ret.addr.Ret.addr.33

x

aux

rreturn to maineturn to main

5a3b

0x1020x100

aafter returnfter return

21 / 21