Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 - Programming for Science Lecture 34: Dynamic Pointers.

Similar presentations


Presentation on theme: "CSC 107 - Programming for Science Lecture 34: Dynamic Pointers."— Presentation transcript:

1 CSC 107 - Programming for Science Lecture 34: Dynamic Pointers

2 Problem of the Day You drive a bus from Rotterdam to Delft. At the 1 st stop, 33 people get in. At the 2 nd stop, 7 more people get in, and 11 passengers leave. The 3 rd stop, sees 5 people leave and 2 get in. After one hour, the bus arrives in Delft. What is the name of the driver? Read the question: You are the driver!

3 Today’s Goal After lecture, should understand strings Know how to use dynamic memory  Allocate an array  Use a dynamic array  Free the array Remember: Pointers are still hard

4 From Last Lecture char str[50] = “Car”; char *str2 = str; char bob[] = “Bob”; printf(“%s\n”, strcpy(str, bob)); printf(“%d %s\n”, strlen(str), str); printf(“%d %s\n”, strlen(bob), bob); printf(“%d %s\n”, strlen(str2), str2); strcpy(str, “Car”); printf(“%s\n”, strcat(str, bob)); printf(“%d %s\n”, strlen(str2), str2);

5 From Last Lecture char str[50] = “The quick brown fox”; char *str2, *str3; str2 = strchr(str, 'Q'); printf(“%p %s\n”, str2, str); str2 = strchr(str, 'q'); str3 = str2 + 1; printf(“%s %s %s\n”, str, str2, str3); str2[0] = '\0'; printf(“%s %s %s\n”, str, str2, str3);

6 Pointers & Variables Variable names a memory location  Initial value is unknown  Memory location updated via assignment  Value is that stored in memory location Pointer is type of variable…  … but whose value is a memory location  Aliases other variables  Interchangeable with array variable

7 Memory Location Access Arrays’ & pointers’ values are memory location  Often used interchangeably Use * or [] to access value  *p is a synonym for p[0] But what about other elements?  p[1] or *(p+1) gets 1 th element in p  Can use array index or pointer arithmetic

8 Pointer Arithmetic Addition & subtraction with pointer variable  Moves pointer over number of elements  Actual memory location may change by more Can only add or subtract integers  C cannot know decimal number will be whole  Cannot use ½ a memory location

9 Problem with Arrays Array declarations specify size of array  Must be literal value, e.g., 2, 1000, or 67381 Size must be large enough to hold all data  May not know max. size when writing code Maximum size may be rare case  Using too much memory slows performance  Slowdown is wasteful if memory not used

10 Dynamic Memory Allocation Use malloc() to allocate array dynamically  Must specify number of bytes needed  Returns array’s memory location (e.g., pointer)  If array could not be created, returns NULL Figuring out bytes needed is hard  C does not specify how big any data type is  Best to use the sizeof() function

11 malloc() examples int *up, *down; float *truth, *beauty; char *strange, bob[] = “Bob”; up = malloc(3 * sizeof(int)); down = up; truth = malloc(5 * sizeof(float)); beauty = malloc(2 * sizeof(int)); strange = malloc((strlen(bob) + 1)*sizeof(char)); strcpy(strange, bob); strange[2] = ‘t’; up[0] = 0; down[1] = 2;

12 We All Must Go Free Sometime malloc() allocations not freed automatically  Would eventually fill up computer’s memory  Need method of releasing memory not in use Can free memory using free() function  Must have been allocated via malloc()  Have to free entire array that was allocated  Once free, cannot reuse memory

13 malloc() examples int *up, *down, *charm; up = malloc(3 * sizeof(int)); down = up; charm = malloc(2000 * sizeof(int)); up[0] = 0; down[1] = 2; up[2] = 4; down = down + 1; free(down); free(up); down[1] = 34; charm[0] = 12;

14 For Next Lecture Read! Keep up with weekly assignments


Download ppt "CSC 107 - Programming for Science Lecture 34: Dynamic Pointers."

Similar presentations


Ads by Google