CS106A Handout #37 Summer 2003 August 7,2003 Pointer Practice 1)Pointer Function Traces For the following code segments,you are asked to draw the state of the computer's memory during code execution.Trace through,starting from main and stop at the point marked.Be sure to distinguish between stack memory and heap memory.Local variables are allocated in the stack,and all dynamically allocated memory is in the heap.Label all the parts of the drawing: include the names of variables,note uninitialized or garbage values/pointers with a question mark,and indicate whenever memory is orphaned.Note that this code is purposely written to be confusing.When you write code using pointers,it should not look like this. a void RainbowSherbet (double *pineapple,double *lime,int frozen) lime (double *GetBlock(3 sizeof(double)); for (frozen =0;frozen *pineapple;frozen++)( *(lime frozen)=frozen 2; <-Draw the state of memory here main() int i; double **cherry,*orange; double raspberry[4]; cherry &orange; orange =&raspberry[1]; fox(i=1;i<4:1++){ raspberry[i]i*4; **cherry =3.0; RainbowSherbet (orange,*cherry,raspberry[3]);
CS106A Handout #37 Summer 2003 August 7, 2003 Pointer Practice 1) Pointer Function Traces For the following code segments, you are asked to draw the state of the computer’s memory during code execution. Trace through, starting from main and stop at the point marked. Be sure to distinguish between stack memory and heap memory. Local variables are allocated in the stack, and all dynamically allocated memory is in the heap. Label all the parts of the drawing: include the names of variables, note uninitialized or garbage values/pointers with a question mark, and indicate whenever memory is orphaned. Note that this code is purposely written to be confusing. When you write code using pointers, it should not look like this. a) void RainbowSherbet(double *pineapple, double *lime, int frozen) { lime = (double *) GetBlock(3 * sizeof(double)); for (frozen = 0; frozen < *pineapple; frozen++) { *(lime + frozen) = frozen / 2; } <—Draw the state of memory here } main() { int i; double **cherry, *orange; double raspberry[4]; cherry = &orange; orange = &raspberry[1]; for (i = 1; i < 4; i++) { raspberry[i] = i * 4; } **cherry = 3.0; RainbowSherbet(orange, *cherry, raspberry[3]); }
-2- b) main() int i,*pl,**p2; int array[6]; pl array; p2=&p1; for(i=0;i<3i++){ p1[i]=i; p1++; } *p2 GetBlock(3 sizeof (int)); *p1++=42: Draw a diagram indicating the contents of memory at this point. c) main ( int i; double *kimball,branner[3]; for(i=0;i<3;i++)( branner[i]=6 /(i+1); HousingDraw(&kimball,branner[2],branner); Draw a diagram indicating the contents of memory at this point. void HousingDraw(double **flomo,int lamaison,double wilbur[]) *flomo GetBlock(lamaison sizeof(double)); wilbur++; *(wi1bur+1)=4; for (lamaison 0;lamaison *wilbur;lamaison++){ wilbur [lamaison]wilbur[lamaison]2; }
- 2 - b) c) main() { int i, *p1, **p2; int array[6]; p1 = array; p2 = &p1; for (i = 0; i < 3; i++) { p1[i] = i; p1++; } *p2 = GetBlock(3 * sizeof (int)); *p1++ = 42; Draw a diagram indicating the contents of memory at this point. } main() { int i; double *kimball, branner[3]; for (i = 0; i < 3; i++) { branner[i] = 6 / (i + 1); } HousingDraw(&kimball, branner[2], branner); Draw a diagram indicating the contents of memory at this point. } void HousingDraw(double **flomo, int lamaison, double wilbur[]) { *flomo = GetBlock(lamaison * sizeof(double)); wilbur++; *(wilbur + 1) = 4; for (lamaison = 0; lamaison < *wilbur; lamaison++) { wilbur[lamaison] = wilbur[lamaison] / 2; } }
3- 2)Pointers and Arrays(Final Exam Question,Spring 98) a)Write a function countEvensAndodds which calculates the number of odd and even numbers in an array of integers.countEvensAndodds takes four parameters:the array of integers,the size of the array,and pointers to two integers so that the number of even and odd integers may be returned by reference.Write the prototype,and then write the function. b)Using your countEvensAndodds function from part(a),write a function Partition which takes an array of integers and its effective size,and returns two new integer arrays and their respective sizes by reference.The first new array should contain all those integers of the original array that are even,and the second array should contain all those elements of the original array which are odd.The original array passed in should not be at all changed.What's this function's prototype? c)The following program,presumably designed to test your functions above,does not work.In fact,Thetis interrupts the execution complaining that an uninitialized variable is being passed to Partition.In two or three sentences,identify the problems with the test program and then explain how all the errors should be corrected so that the program runs properly. void Init(int array[],int n) { int i; Randomize ()i for (i 0;i<n;i++)( array[i]RandomInteger(1,1000); main ( int numbers [1000]; int **odds,**evens; int *numodds,*numEvens; Init(numbers,1000); Partition(numbers,1000,evens,numEvens,odds,numodds); FreeBlock(evens); FreeBlock (odds);
- 3 - 2) Pointers and Arrays (Final Exam Question, Spring 98) a) Write a function CountEvensAndOdds which calculates the number of odd and even numbers in an array of integers. CountEvensAndOdds takes four parameters: the array of integers, the size of the array, and pointers to two integers so that the number of even and odd integers may be returned by reference. Write the prototype, and then write the function. b) Using your CountEvensAndOdds function from part (a), write a function Partition which takes an array of integers and its effective size, and returns two new integer arrays and their respective sizes by reference. The first new array should contain all those integers of the original array that are even, and the second array should contain all those elements of the original array which are odd. The original array passed in should not be at all changed. What's this function's prototype? c) The following program, presumably designed to test your functions above, does not work. In fact, Thetis interrupts the execution complaining that an uninitialized variable is being passed to Partition. In two or three sentences, identify the problems with the test program and then explain how all the errors should be corrected so that the program runs properly. void Init(int array[], int n) { int i; Randomize(); for (i = 0; i < n; i++) { array[i] = RandomInteger(1, 1000); } } main() { int numbers[1000]; int **odds, **evens; int *numOdds, *numEvens; Init(numbers, 1000); Partition(numbers, 1000, evens, numEvens, odds, numOdds); FreeBlock(evens); FreeBlock(odds); }
-4- 3)Concat Now that you know how strings are really implemented,we want you to implement concat without using any functions from either "strlib.h"or the ANSI library string.h. a)First,as a warm-up,implement: int StringLength(string s); b)Now,implement concat.You are permitted to use stringLength,but no other functions from "strlib.h"or string.h.Here's the prototype: string Concat(string s1,string s2); 4)Common pointer mistakes a b) int *IndexArray (int n) int *IndexArray(int n) { { int array[n],i; int *array,i; for (i=0;i<n;i++){ for (i=0;i<n;i++)( array[i]i; array[i]i; } return (array)i return (array); } Each of these implementations is buggy in some way.Describe in English the error or errors that arise in each one
- 4 - 3) Concat Now that you know how strings are really implemented, we want you to implement Concat without using any functions from either "strlib.h" or the ANSI library string.h. a) First, as a warm-up, implement: int StringLength(string s); b) Now, implement Concat. You are permitted to use StringLength, but no other functions from "strlib.h" or string.h. Here's the prototype: string Concat(string s1, string s2); 4) Common pointer mistakes a) b) int *IndexArray(int n) int *IndexArray(int n) { { int array[n], i; int *array, i; for (i = 0; i < n; i++) { for (i = 0; i < n; i++) { array[i] = i; array[i] = i; } } return (array); return (array); } } Each of these implementations is buggy in some way. Describe in English the error or errors that arise in each one
-5- 5)Program Tracing:Meet the cast of Friends Draw the state of the computer's memory twice during code execution below.Trace through, starting from main,and stop at the first point marked before the call to Friends and draw the state of memory.Then execute the call to Friends and draw the state of memory afterwards. Note stack and heap,label variables and record fields,and indicate orphaned memory. typedef struct char monica [8]; string ross[3]; string *rachel; int **phoebe; int joey; moviestarRec; static void MyCopy(char *dst,char *src) int i; for(i=0;src[i]I=\0';1++){ dst[i]=src[i]; dst[i]src[i]; static void Friends(moviestarRec rosanne,moviestarRec *grace,int *ellen) int i; MyCopy (rosanne.ross [0],"bob"); *e11en-=2; grace->rachel--; grace->ross[0]SubString(*(rosanne.rachel -1),1,6); for (i=0;iphoebe [i]ellen; rosanne.ross[2]CopyString(rosanne.monica 3); main() { moviestarRec cast; MyCopy (cast.monica,"Joey"); cast.ross[0]cast.monica +3; cast.ross[1]Concat("Chandler","rules"); cast.rachel cast.ross 2; cast.joey cast.monica[2]-'a'; cast.phoebe (int **GetBlock(cast.joey sizeof(int *)) First draw the state of memory here Friends(cast,&cast,&cast.joey); And then again right here
- 5 - 5) Program Tracing: Meet the cast of Friends Draw the state of the computer’s memory twice during code execution below. Trace through, starting from main, and stop at the first point marked before the call to Friends and draw the state of memory. Then execute the call to Friends and draw the state of memory afterwards. Note stack and heap, label variables and record fields, and indicate orphaned memory. typedef struct { char monica[8]; string ross[3]; string *rachel; int **phoebe; int joey; } movieStarRec; static void MyCopy(char *dst, char *src) { int i; for (i = 0; src[i] != '\0'; i++) { dst[i]= src[i]; } dst[i] = src[i]; } static void Friends(movieStarRec rosanne, movieStarRec *grace, int *ellen) { int i; MyCopy(rosanne.ross[0], "bob"); *ellen -= 2; grace->rachel--; grace->ross[0] = SubString(*(rosanne.rachel - 1), 1, 6); for (i = 0; i phoebe[i] = ellen; } rosanne.ross[2] = CopyString(rosanne.monica + 3); } main() { movieStarRec cast; MyCopy(cast.monica, "Joey"); cast.ross[0] = cast.monica + 3; cast.ross[1] = Concat("Chandler","rules"); cast.rachel = cast.ross + 2; cast.joey = cast.monica[2] - 'a'; cast.phoebe = (int **) GetBlock(cast.joey * sizeof(int *)); ⇐ First draw the state of memory here Friends(cast, &cast, &cast.joey); ⇐ And then again right here