29
Homework 2 Concerns http://www.cse.buffalo.edu/~shapiro/Courses /CSE305/2010/Homeworks/hw2.pdf

Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

  • Upload
    others

  • View
    2

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Homework 2 Concerns

• http://www.cse.buffalo.edu/~shapiro/Courses/CSE305/2010/Homeworks/hw2.pdf

Page 2: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Language Subsets

• Sebesta makes the point that we only learn a subset of any language.

Page 3: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Readability vs. Writability (vs. Optimality)

• Conditional Operator vs. Conditional Statement– C, C++, JavaSystem.out.println((x == y) ? x : y);

Vs.If(x == y) System.out.println(x);else System.out.println(y);

(Fun fact: This is a lot how functional languages express if’s!)

Page 4: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Readability vs. Writability (vs. Optimality)

Shift Operatorint n = 10;n = n << 2;What’s the value of n?

Page 5: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Readability vs. Writability (vs. Optimality)

Shift Operatorint n = 10;n = n << 2;What’s the value of n?

1010 bit shifted 2 to the left.Becomes: 101000 (=40).

Page 6: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Readability vs. Writability (vs. Optimality)

Shift Operatorint n = 10;n = n << 2;What’s the value of n?

int n = 100;n = n >> 1;What’s the value of n?

1010 bit shifted 2 to the left.Becomes: 101000 (=40).

Page 7: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Readability vs. Writability (vs. Optimality)

Shift Operatorint n = 10;n = n << 2;What’s the value of n?

int n = 100;n = n >> 1;What’s the value of n?

1010 bit shifted 2 to the left.Becomes: 101000 (=40).

1100100 bit shifted 1 to the right.Becomes: 110010 (=50).

Page 8: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Readability vs. Writability (vs. Optimality)

Shift Operatorint n = 10;n = n << 2;What’s the value of n?

int n = 100;n = n >> 1;What’s the value of n?

int n = 10;n = n << 30;What’s the value of n?

1010 bit shifted 2 to the left.Becomes: 101000 (=40).

1100100 bit shifted 1 to the right.Becomes: 110010 (=50).

Page 9: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Readability vs. Writability (vs. Optimality)

Shift Operatorint n = 10;n = n << 2;What’s the value of n?

int n = 100;n = n >> 1;What’s the value of n?

int n = 10;n = n << 30;What’s the value of n?

1010 bit shifted 2 to the left.Becomes: 101000 (=40).

1100100 bit shifted 1 to the right.Becomes: 110010 (=50).

This is a 32 bit int. One bit is the sign.This becomes invalid! (Actual result: -2147483648)

Page 10: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Scopeprocedure Main is

X, Y : Integer;procedure sub1 is

X: Integer;begin…end;

procedure sub2 isY: Integer;begin…X……Y…end;

begin…end;

Assume Static Scoping:-If Main calls sub1 calls sub2, what does X refer to in sub2?

Page 11: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Scopeprocedure Main is

X, Y : Integer;procedure sub1 is

X: Integer;begin…end;

procedure sub2 isY: Integer;begin…X……Y…end;

begin…end;

Assume Static Scoping:-If Main calls sub1 calls sub2, what does X refer to in sub2?

X refers to Main’s X

Page 12: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Scopeprocedure Main is

X, Y : Integer;procedure sub1 is

X: Integer;begin…end;

procedure sub2 isY: Integer;begin…X……Y…end;

begin…end;

Assume Static Scoping:-If Main calls sub1 calls sub2, what does X refer to in sub2?

X refers to Main’s X

-If Main calls sub1 calls sub2, what does Y refer to in sub2?

Page 13: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Scopeprocedure Main is

X, Y : Integer;procedure sub1 is

X: Integer;begin…end;

procedure sub2 isY: Integer;begin…X……Y…end;

begin…end;

Assume Static Scoping:-If Main calls sub1 calls sub2, what does X refer to in sub2?

X refers to Main’s X

-If Main calls sub1 calls sub2, what does Y refer to in sub2?

-Y refers to sub2’s Y

Page 14: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Scopeprocedure Main is

X, Y : Integer;procedure sub1 is

X: Integer;begin…end;

procedure sub2 isY: Integer;begin…X……Y…end;

begin…end;

Assume Dynamic Scoping:-If Main calls sub1 calls sub2, what does X refer to in sub2?

Page 15: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Scopeprocedure Main is

X, Y : Integer;procedure sub1 is

X: Integer;begin…end;

procedure sub2 isY: Integer;begin…X……Y…end;

begin…end;

Assume Dynamic Scoping:-If Main calls sub1 calls sub2, what does X refer to in sub2?

X refers to sub1’s X

Page 16: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Scopeprocedure Main is

X, Y : Integer;procedure sub1 is

X: Integer;begin…end;

procedure sub2 isY: Integer;begin…X……Y…end;

begin…end;

Assume Dynamic Scoping:-If Main calls sub1 calls sub2, what does X refer to in sub2?

X refers to sub1’s X

-If Main calls sub1 calls sub2, what does Y refer to in sub2?

Page 17: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

Scopeprocedure Main is

X, Y : Integer;procedure sub1 is

X: Integer;begin…end;

procedure sub2 isY: Integer;begin…X……Y…end;

begin…end;

Assume Dynamic Scoping:-If Main calls sub1 calls sub2, what does X refer to in sub2?

X refers to sub1’s X

-If Main calls sub1 calls sub2, what does Y refer to in sub2?

-Y refers to sub2’s Y

Page 18: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

The Run-time Stackpublic class RTS{

public static void main(String[] args){RTS1 tClass = new RTS1();tClass.testVar += tClass.testMethod(5);System.out.println(tClass.testVar);

}}

class RTS1{public int testVar;

public int testMethod(int i){int j = i+1;return j;

}}

ParametersReturn Address

Stack Heap

Page 19: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

The Run-time Stackpublic class RTS{

public static void main(String[] args){RTS1 tClass = new RTS1();tClass.testVar += tClass.testMethod(5);System.out.println(tClass.testVar);

}}

class RTS1{public int testVar;

public int testMethod(int i){int j = i+1;return j;

}}

ParametersReturn Address

Stack Heap

tClass

RTS1testVar=0

(ref)

Page 20: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

The Run-time Stackpublic class RTS{

public static void main(String[] args){RTS1 tClass = new RTS1();tClass.testVar += tClass.testMethod(5);System.out.println(tClass.testVar);

}}

class RTS1{public int testVar;

public int testMethod(int i){int j = i+1;return j;

}}

ParametersReturn Address

Stack Heap

tClass

RTS1testVar=0

i= 5Return Address

(ref)

Page 21: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

The Run-time Stackpublic class RTS{

public static void main(String[] args){RTS1 tClass = new RTS1();tClass.testVar += tClass.testMethod(5);System.out.println(tClass.testVar);

}}

class RTS1{public int testVar;

public int testMethod(int i){int j = i+1;return j;

}}

ParametersReturn Address

Stack Heap

tClass

RTS1testVar=0

i= 5Return Address

int j =6

(ref)

Page 22: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

The Run-time Stackpublic class RTS{

public static void main(String[] args){RTS1 tClass = new RTS1();tClass.testVar += tClass.testMethod(5);System.out.println(tClass.testVar);

}}

class RTS1{public int testVar;

public int testMethod(int i){int j = i+1;return j;

}}

ParametersReturn Address

Stack Heap

tClass

RTS1testVar=6

i= 5Return Address

int j =6

(ref)

Page 23: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

The Run-time Stackpublic class RTS{

public static void main(String[] args){RTS1 tClass = new RTS1();tClass.testVar += tClass.testMethod(5);System.out.println(tClass.testVar);

}}

class RTS1{public int testVar;

public int testMethod(int i){int j = i+1;return j;

}}

ParametersReturn Address

Stack Heap

tClass

RTS1testVar=6

(ref)

Page 24: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

The Run-time Stackpublic class RTS{

public static void main(String[] args){RTS1 tClass = new RTS1();tClass.testVar += tClass.testMethod(5);System.out.println(tClass.testVar);

}}

class RTS1{public int testVar;

public int testMethod(int i){int j = i+1;return j;

}}

ParametersReturn Address

Stack Heap

tClass

RTS1testVar=6

(ref)

Page 25: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

A “problem” with C/C++

• Built-in arrays in C/C++ can’t be returned!int[] test(){

int i[10];return i;

}“arr.cpp:1: error: expected unqualified-id before [ token”

Why not?

Page 26: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

A “problem” with C/C++

• Built-in arrays in C/C++ can’t be returned!int[] test(){

int i[10];return i;

}“arr.cpp:1: error: expected unqualified-id before [ token”

Why not?A: They are on the stack!

Page 27: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

What does Java do?

static int[] test(){int[] i = new int[10];return i;

}

This is fine! Why?

Page 28: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

What does Java do?

static int[] test(){int[] i = new int[10];return i;

}

This is fine! Why?A: “new int[10]” is on the heap. i (which is just a

reference to the new int[10]) is on the stack!

Page 29: Homework 2 Concerns - danielschlegel.orgdanielschlegel.org/teaching/305_sp10/Recitation 1-27-2010.pdf · Language Subsets • Sebesta makes the point that we only learn a subset of

One workaround for C/C++

int* test(){int* i;i = (int*)malloc(10);return i;

}

Do you see why this is OK?