Presentation is loading. Please wait.

Presentation is loading. Please wait.

Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of email daily. If you email me and.

Similar presentations


Presentation on theme: "Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of email daily. If you email me and."— Presentation transcript:

1 Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of email daily. If you email me and don't hear back within a day or two, feel free to email again. I don't want to blow anyone off, but sometimes my messages get buried and forgotten. The output file name for the coverage target should be the same as for noncoverage. (e.g. dna and battleship) 1

2 Announcements When doing corrections, remember to change your code as little as possible to get as much possible partial credit as possible. Follow the spec! Use diff to check your answers against the supplied solutions. Remember with this assignment to include a Makefile, and a directory of test cases that "cover" your code sufficiently. 2

3 Make reminder Makefile isFib: isFib.c gcc -Wall -o isFib isFib.c clean: rm -f isFib *.o 3 1.Often a makefile will include a "clean" object to remove files associated with the project 2.Here typing make clean will cause the executable to be deleted. 3.Notice that if there is no file named "clean" in the current directory, the rm command will run everytime you type make clean.

4 Make reminder Makefile isFib: isFib.c gcc -Wall -o isFib isFib.c debug: gcc -Wall –g -o isFib isFib.c clean: rm -f isFib *.o 4 1.What happens when we type make debug followed by make?

5 Phony Targets A phony target is one that is not the name of a file, e.g.: “make clean” will remove a.out and *.o files 5 phony target clean: rm –f *.o a.out

6 Phony Targets This won’t work if we (accidentally) create a file named “clean” Fix:.PHONY: clean clean: rm *.o a.out 6 clean: rm –f *.o a.out cleanup actions will be executed even if there is a file named “clean”

7 Make reminder Makefile isFib: isFib.c gcc -Wall -o isFib isFib.c.PHONY: debug debug: gcc -Wall –g -o isFib isFib.c.PHONY: clean clean: rm -f isFib *.o 7 1.The.PHONY lines tell make to make the targets debug and clean even if files named debug and clean already exist. (In other words, ALWAYS run those commands.)

8 Two common pointer problems Uninitialized pointers – the pointer has not been initialized to point to a valid location Dangling pointers – the pointer points at a memory location that has actually been deallocated 8

9 Background: Runtime Memory Organization Layout of an executing process’s virtual memory: 9 code global data memory mapped files operating system stack (grows downwards) heap (grows upwards) high addresses low addresses 0xffffffff 0x00000000

10 Background: Runtime Memory Organization Runtime stack: 10 Code: stack growth p’s caller’s stack frame p’s stack frame top of stack p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } s(…) { … }

11 Background: Runtime Memory Organization Runtime stack: 11 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack s(…) { … }

12 Background: Runtime Memory Organization Runtime stack: 12 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame s(…) { … }

13 Background: Runtime Memory Organization Runtime stack: 13 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame r’s stack frame s(…) { … }

14 Background: Runtime Memory Organization Runtime stack: 14 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame r’s stack frame (deallocated) s(…) { … }

15 Background: Runtime Memory Organization Runtime stack: 15 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame r’s stack frame s(…) { … }

16 Background: Runtime Memory Organization Runtime stack: 16 p(…) { … q(…); s(…); } q(…) { … r(…); } r(…) { … } Code: stack growth p’s caller’s stack frame p’s stack frame top of stack q’s stack frame r’s stack frame s(…) { … } s’s stack frame

17 Uninitialized pointers: Example 17 str was never initialized to point to anything fix: initialize str

18 Dangling pointers What’s wrong with this code? 18

19 Dangling pointers What’s wrong with this code? 19 runtime stack main my_read read_string buf string str

20 Dangling pointers What’s wrong with this code? 20 runtime stack main my_read read_string a c b a buf string str

21 Dangling pointers What’s wrong with this code? 21 runtime stack main my_read read_string a c b a buf string str

22 Dangling pointers What’s wrong with this code? 22 runtime stack main my_read read_string a c b a buf string str dangling pointer!

23 Dynamic memory allocation We can’t always anticipate how much memory to allocate – too little  program doesn’t work – too much  wastes space Solution: allocate memory at runtime as necessary – malloc(), calloc() allocates memory in the heap area – free() deallocates previously allocated heap memory block program memory layout reserved code global variables, strings heap stack reserved 23 low addr high addr

24 Dynamic memory allocation: usage 24 void * : “generic pointer” Usage: int *iptr = malloc(sizeof(int))// one int char *str = malloc(64)// an array of 64 chars // ( sizeof(char) = 1 by definition ) int *iarr = calloc(40, sizeof(int))// a 0-initialized array of 40 ints

25 Dynamic memory allocation: example 1 25 ALWAYS check the return value of any system call that may fail

26 Dynamic memory allocation: example 1 26

27 Dynamic memory allocation: example 2 27 figure out the total size of the concatenated strings allocate space concatenate the strings into buf

28 Dynamic memory allocation: example 2 28


Download ppt "Announcements Partial Credit Due Date for Assignment 2 now due on Sat, Feb 27 I always seem to be behind and get tons of email daily. If you email me and."

Similar presentations


Ads by Google