lecture5 - cs50cdn.cs50.net/2017/fall/lectures/5/lecture5.pdf · #include void foo(char *bar) {...

Post on 28-Sep-2020

16 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

typedefstruct{stringname;stringdorm;}student;

environment variables

stack

uninitialized data

heap

environment variables

stack

uninitialized data

initialized data

text

heap

voidswap(inta,intb){inttmp=a;a=b;b=tmp;}

voidswap(int*a,int*b){inttmp=*a;*a=*b;*b=tmp;}

malloc

free

malloc,calloc,realloc

free

intmain(void){int*x;int*y;x=malloc(sizeof(int));*x=42;*y=13;y=x;*y=13;}

stack overflow

heap overflow

buffer overflow

#include<string.h>voidfoo(char*bar){charc[12];memcpy(c,bar,strlen(bar));}intmain(intargc,char*argv[]){foo(argv[1]);}

adapted from wikipedia.org/wiki/Stack_buffer_overflow

#include<string.h>voidfoo(char*bar){charc[12];memcpy(c,bar,strlen(bar));}intmain(intargc,char*argv[]){foo(argv[1]);}

adapted from wikipedia.org/wiki/Stack_buffer_overflow

#include<string.h>voidfoo(char*bar){charc[12];memcpy(c,bar,strlen(bar));}intmain(intargc,char*argv[]){foo(argv[1]);}

adapted from wikipedia.org/wiki/Stack_buffer_overflow

#include<string.h>voidfoo(char*bar){charc[12];memcpy(c,bar,strlen(bar));}intmain(intargc,char*argv[]){foo(argv[1]);}

adapted from wikipedia.org/wiki/Stack_buffer_overflow

#include<string.h>voidfoo(char*bar){charc[12];memcpy(c,bar,strlen(bar));}intmain(intargc,char*argv[]){foo(argv[1]);}

adapted from wikipedia.org/wiki/Stack_buffer_overflow

#include<string.h>voidfoo(char*bar){charc[12];memcpy(c,bar,strlen(bar));}intmain(intargc,char*argv[]){foo(argv[1]);}

adapted from wikipedia.org/wiki/Stack_buffer_overflow

adapted from wikipedia.org/wiki/Stack_buffer_overflow

adapted from wikipedia.org/wiki/Stack_buffer_overflow

adapted from wikipedia.org/wiki/Stack_buffer_overflow

adapted from wikipedia.org/wiki/Stack_buffer_overflow

valgrind

255216255

0xff0xd80xff

http://xkcd.com/138/

debug50

ddb50

environment variables

stack

uninitialized data

heap

https://cs.calvin.edu/activities/books/c++/ds/1e/

http://cs.calvin.edu/books/c++/ds/1e/

typedefstruct{stringname;stringdorm;}student;

typedefstructnode{intn;structnode*next;}node;

https://cs.calvin.edu/activities/books/c++/ds/1e/

push

pop

...

typedefstruct{intnumbers[CAPACITY];intsize;}stack;

typedefstruct{int*numbers;intsize;}stack;

enqueue

dequeue

...

typedefstruct{intfront;intnumbers[CAPACITY];intsize;}queue;

typedefstruct{intfront;int*numbers;intsize;}queue;

Figure by Larry Nyhoff.

tree

22 33 44 55 66 77 88

22 33 44 55 66 77 88

Figure from http://cs.calvin.edu/books/c++/ds/1e/.

binary search tree

typedefstructnode{intn;structnode*left;structnode*right;}node;

boolsearch(intn,node*tree){if(tree==NULL){returnfalse;}elseif(n<tree->n){returnsearch(n,tree->left);}elseif(n>tree->n){returnsearch(n,tree->right);}else{returntrue;}}

boolsearch(intn,node*tree){if(tree==NULL){returnfalse;}elseif(n<tree->n){returnsearch(n,tree->left);}elseif(n>tree->n){returnsearch(n,tree->right);}else{returntrue;}}

boolsearch(intn,node*tree){if(tree==NULL){returnfalse;}elseif(n<tree->n){returnsearch(n,tree->left);}elseif(n>tree->n){returnsearch(n,tree->right);}else{returntrue;}}

boolsearch(intn,node*tree){if(tree==NULL){returnfalse;}elseif(n<tree->n){returnsearch(n,tree->left);}elseif(n>tree->n){returnsearch(n,tree->right);}else{returntrue;}}

boolsearch(intn,node*tree){if(tree==NULL){returnfalse;}elseif(n<tree->n){returnsearch(n,tree->left);}elseif(n>tree->n){returnsearch(n,tree->right);}else{returntrue;}}

O(n2) O(n log n) O(n) O(log n) O(1) …

O(n2) O(n log n) O(n) O(log n) O(1) …

O(n2) O(n log n) O(n) O(log n) O(1) …

wikipedia.org

Figure from Lewis and Denenberg’s Data Structures & Their Algorithms.

typedefstructnode{boolword;structnode*children[27];}node;

top related