



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!




CS 403/503 Exam 3 Fall 2007 Solution
CS 403 has built-in extra credit up to 120 points. CS 503 will be prorated to 100-point scale.
#include
enum E {Y, Z};
struct X { E tag; int i; char c[8]; union { int j; char d[8]; }; };
int F (X & w) { if (w.tag != Y) return w.i; else return w.i * w.j; }
char * G (X & w) { if (w.tag != Z) return w.c; else return strcat (w.c, w.d); // string catenation }
int main( ) { X arr[2]; arr[0].tag = Y; arr[0].i = 2; strcpy (arr[0].c, "ab"); // copy "ab" into arr[0].c arr[0].j = 3;
arr[1].tag = Z; arr[1].i = 7; strcpy (arr[1].c, "cd"); strcpy (arr[1].d, "ef");
cout << F(arr[0]) << " " << G(arr[0]) << endl; cout << F(arr[1]) << " " << G(arr[1]) << endl; } Output: 6 ab 7 cdef
class X { protected int i; protected String c; public X (int ii, String cc) { i=ii; c=cc; } public int F ( ) { return i; } public String G ( ) { return c; } public void print ( ) { System.out.println (F( ) + " " + G( )); } }
class Y extends X { private int j; public Y (int ii, String cc, int jj) { super (ii, cc); j = jj; } public int F ( ) { return ij; } }*
class Z extends X { private String d; public Z (int ii, String cc, String dd) { super (ii, cc); d=dd; } public String G ( ) { return c+d; } }
class MAIN { public static void main (String[ ] args) { X[ ] arr = new X[2]; arr[0] = new Y (2, "ab", 3); arr[1] = new Z (7, "cd", "ef"); arr[ 0 ].print( ); arr[1].print( ); } }
a. Write C++ code that causes a dangling pointer and a memory leak. Show where the dangling pointer and the memory leak occur. [12 points]
**int p = new int(23); p = new int(99); // memory leak: the block containing 23 is now inaccessible delete p; cout << p; // dangling pointer: the block addressed by p containing 99 was deleted
b. Select any one of these three garbage collection algorithms: mark-and-sweep algorithm, copying algorithm, or reference counting algorithm. Explain how this algorithm works. [12 points]
See pages 265-266 in textbook.
class MAIN { static void F (boolean x) throws A { try { System.out.println(1); G(x); System.out.println(2); } catch (B b) { System.out.println(3); } catch (D d) { System.out.println(4); } catch (A a) { System.out.println(5); } finally { System.out.println(6); if (x) throw new E( ); System.out.println(7); } } static void G (boolean y) throws A { try { System.out.println(8); H(y); System.out.println(9); } catch (E e) { System.out.println(10); } catch (D d) { System.out.println(11); } catch (A a) { System.out.println(12); } finally { System.out.println(13); if (y) throw new C( ); System.out.println(14); } } static void H (boolean z) throws A { try { System.out.println(15); if (z) throw new B( ); System.out.println(16); } catch (C c) { System.out.println(17); } catch (B b) { System.out.println(18); } catch (A a) { System.out.println(19); } finally { System.out.println(20); if (z) throw new D( ); System.out.println(21); } } public static void main (String[ ] args) { try { System.out.println(22); F(true); System.out.println(23); } catch (D d) { System.out.println(24); } catch (B b) { System.out.println(25); } catch (A a) { System.out.println(26); } finally { System.out.println(27); } } }
Output: