[SOLVED] CSE340 Homework 3

30.00 $

Programming resource
Digital learning resource
Category:
Practical programming resource
Suitable for guided study and reference
Tutor guidance available when needed

Description

5/5 - (2 votes)

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; a = (struct T*) malloc(sizeof(struct T)); // memory 1 b = (struct T*) malloc(sizeof(struct T)); // memory 2 (*a).next = b; // point 1 b = (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?

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:

Resource details

Understand the Task Before You Use the Resource

Review the requirements, identify the programming concepts involved, study the implementation and test your understanding with your own examples and modifications.