CSE340 Homework 3

Problem 1 out of 2:
Consider the following code in C syntax.

int a, b;

void print() {

    printf("%d, %d\n" , a, b);

}

void g() {     a = a + 3;     b = b + 4;     print(); }

int f() {     int a, p;     a = 1;    
b = b + 1;     p = 2;     g();    
return a + b + p;

}

int main() {     int a, b;

   
a = 5;     b = 7;     g();    
a = f();     g();     return 0; }

− What is the output of this program assuming static scoping?− What is the output of this program assuming dynamic scoping?Problem 2 out of 2:
Consider the following C code and assume stack memory allocation for nested
scope is used

struct T {      int data;      struct T* next; 

}; 


struct T** p;  struct T** q; 


int main()  { 

    struct T* a; 

    { 

        struct T* b; 


=
(struct T*) malloc(sizeof(struct T));   
// memory 1          b = (struct
T*) malloc(sizeof(struct T));    //
memory 2          (*a).next = b;    // point 1 



=
(struct T*) malloc(sizeof(struct T));   
// memory 3 

        (*b).next = a;          p = &a; 

        q = &b;           // point 2 

        free(b); 

    } 

   
// point 3 after the curly brace 
}

Assume all un-initialized
pointers are equal to NULL.

Draw box-circle diagram for a and b at point 1.Which memory locations are garbage at point 2? Which memory locations are garbage at point 3? 
What are the dangling references at point 2? 
What are the dangling references at point 3? 
Which memory locations have been deallocated by the time point 3 is reached? 
Powered by