Question: // You should answer the questions and provide justification for your answers. // All questions assume that the code is compiled according to ANSI C99
// You should answer the questions and provide justification for your answers. // All questions assume that the code is compiled according to ANSI C99 // standard. To compile it according to C99 standard execute the // following from the command prompt (not visual studio!) // // gcc -std=gnu99 CSE340F17_HW4.c // #include#include struct T { int a; int *b; struct T *next; } ; struct T1 { int a; int *b; struct T *next; } ; struct T* (*f)(int, int, struct T*); struct T* g(int a, int b, struct T* c) { return (a > b ? c : NULL); } struct T *p1; struct T **p2; int *p; int *q; int main() { int i; struct T* cursor; p1 = (struct T *) malloc(sizeof(struct T)); p2 = (struct T **) malloc(sizeof(struct T *)); p = q; p2 = &p1; (*p1).a = 4; (*p1).next = *p2; //------------------------------------------------------------------ // Question1. Draw a box-circle diagram for p1, p2 and the memory // allocated above at this point //------------------------------------------------------------------ { struct T *a[5]; int i; a[0] = (struct T*) malloc(sizeof(struct T)); p2 = &(a[0]); (*a[0]).next = p1; //------------------------------------------------------------------ // Question 2. Draw a box-circle diagram for p1, p2, a[] and the // memory allocated above at this point //------------------------------------------------------------------ for (i = 1; i < 4; i++) { a[i] = (struct T*) malloc(sizeof(struct T)); (*a[i]).next = a[i-1]; (*a[i]).a = i; } (*p1).next = a[2]; //------------------------------------------------------------------ // Question 3. Draw a box-circle diagram of ALL the variables // and the memory allocated up to this point? // what locations are garbage at this point? //------------------------------------------------------------------ //------------------------------------------------------------------ // Question 4. Give an alias for a[2] at this point. // This alias should not include the names "a" !! // This might be tricky and you might want to skip // it and do other parts first. //------------------------------------------------------------------ { struct T *b[6]; int j; for (i = 1; i < 6; i++) { b[i] = (struct T*) malloc(sizeof(struct T)); (*b[i]).next = a[i%4]; (*b[i]).b = &i; } //------------------------------------------------------------------ // Question 5. Explain the output produced by the // following loop //------------------------------------------------------------------ for (j = 1; j < 6; j++) { printf("%d ",(*(*b[j]).next).a); } } //------------------------------------------------------------------ // Question 6. What are the dangling references at // this point? //------------------------------------------------------------------ //------------------------------------------------------------------ // Question 7. What locations are garbage at this point? //------------------------------------------------------------------ } //------------------------------------------------------------------ // Question 8. What are the dangling references at this point. //------------------------------------------------------------------ //------------------------------------------------------------------ // Question 9. What is the effect of executing the following // assignment? Explain. //------------------------------------------------------------------ *p2 = p1; }
Step by Step Solution
There are 3 Steps involved in it
1 Expert Approved Answer
Step: 1 Unlock
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
