week 5 - cs50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · week 5. title: microsoft powerpoint -...

13
0 Computer Science 50 Introduction to Computer Science I Harvard College David J. Malan [email protected] Week 5

Upload: others

Post on 22-Aug-2020

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

0

Computer Science 50Introduction to Computer Science I

Harvard College

David J. [email protected]

Week 5

Page 2: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

1

Dynamic Memory Allocationmalloc

// get line of text

printf("Say something: ");

char *s1 = GetString();

if (s1 == NULL)

return 1;

// allocate enough space for copy

char *s2 = malloc(strlen(s1) * sizeof(char) + 1);

if (s2 == NULL)

return 1;

seecopy2.c

Page 3: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

2

Memory ManagementRevisited

seecopy2.c

Page 4: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

3

CS 50’s LibraryRevisited

seescanf{1,2,3}.c, http://cs50.net/pub/releases/cs50/cs50.{c,h}

bool

string

char GetChar();

double GetDouble();

float GetFloat();

int GetInt();

long long GetLongLong();

string GetString();

Page 5: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

4

Dangerous Functionsgets → fgetcscanf → fgetc

strcpy → strncpystrcat → strncat

printffprintf

...

Page 6: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

5

Adversaries

Failed logins from:64.6.244.66: 476 times83.143.86.218: 29 times85.35.50.229 (host229-50-static.35-85-b.business.telecomitalia.it):

12 times

Illegal users from:64.6.244.66: 1688 times83.143.86.218: 5448 times85.35.50.229 (host229-50-static.35-85b.business.telecomitalia.it): 168 times216.75.30.127 (su1030127.aspadmin.net): 44 times218.158.134.43: 48 times

Page 7: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

6

Safe Codeint

main(int argc, char *argv[])

{

int i;

for (i = 1; i < argc; i++) {

printf("%s ", argv[i]);

}

printf("\n");

}

Page 8: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

7

Unsafe Codevoid echo_arg(const char s[]){

char buf[MAX_BUF_SIZE];strcpy(buf, s);printf("%s ", buf);

}

int main(int argc, char *argv[]){

int i;for (i = 1; i < argc; i++) {

echo_arg(argv[i]);}printf("\n");

}

Page 9: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

8

Unsafe Code (2)void gotcha(){

printf("\nGotcha!\n");}

void echo_arg(const char s[]){

char buf[MAX_BUF_SIZE];strcpy(buf, s);printf("%s ", buf);

}

int main(int argc, char *argv[]){

int i;for (i = 1; i < argc; i++) {

echo_arg(argv[i]);}printf("\n");

}

Page 10: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

9

struct(and header files)

seestructs1.{c,h}

typedef struct

{

int id;

char *name;

char *house;

}

student;

Page 11: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

10

File I/O

seestructs2.c

fopen/fclose

fscanf/fprintf

fread/fwrite

feof

...

Page 12: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

11

Page 13: Week 5 - CS50cdn.cs50.net/2008/fall/lectures/5/week5.pdf · Week 5. Title: Microsoft PowerPoint - week5.ppt Created Date: 10/22/2008 4:00:38 PM

12

Computer Science 50Introduction to Computer Science I

Harvard College

David J. [email protected]

Week 5