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

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

description

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

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

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

CSC 107 -Programming for ScienceLecture 34:Dynamic Pointers

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

Problem of the Day

You drive a bus from Rotterdam to Delft. At the 1st stop, 33 people get in. At the 2nd stop, 7 more people get in, and 11 passengers leave. The 3rd 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!

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

Today’s Goal

After lecture, should understand strings Know how to use dynamic memory

Allocate an arrayUse a dynamic arrayFree the array

Remember: Pointers are still hard

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

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);

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

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);

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

Pointers & Variables

Variable names a memory location Initial value is unknownMemory location updated via assignmentValue is that stored in memory location

Pointer is type of variable…… but whose value is a memory locationAliases other variables Interchangeable with array variable

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

Memory Location Access

Arrays’ & pointers’ values are memory locationOften 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 1th element in pCan use array index or pointer arithmetic

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

Pointer Arithmetic

Addition & subtraction with pointer variableMoves pointer over number of elementsActual memory location may change by more

Can only add or subtract integersC cannot know decimal number will be wholeCannot use ½ a memory location

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

Problem with Arrays

Array declarations specify size of arrayMust be literal value, e.g., 2, 1000, or 67381

Size must be large enough to hold all dataMay not know max. size when writing code

Maximum size may be rare caseUsing too much memory slows performanceSlowdown is wasteful if memory not used

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

Dynamic Memory Allocation

Use malloc() to allocate array dynamicallyMust specify number of bytes neededReturns array’s memory location (e.g., pointer) If array could not be created, returns NULL

Figuring out bytes needed is hardC does not specify how big any data type isBest to use the sizeof() function

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

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;

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

We All Must Go Free Sometime

malloc() allocations not freed automaticallyWould eventually fill up computer’s memoryNeed method of releasing memory not in use

Can free memory using free() functionMust have been allocated via malloc()Have to free entire array that was allocatedOnce free, cannot reuse memory

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

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;

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

For Next Lecture

Read! Keep up with weekly assignments