C# machine model. Programming Language Concepts and Implementation Fall 2011, Lecture 2. Rasmus Ejlers Møgelberg

Size: px
Start display at page:

Download "C# machine model. Programming Language Concepts and Implementation Fall 2011, Lecture 2. Rasmus Ejlers Møgelberg"

Transcription

1 C# machine model Programming Language Concepts and Implementation Fall 2011, Lecture 2 Reference types vs. value types Structs 2-dimensional arrays Overview Method calls: call-by-value vs. call-by-reference Other C# constructs - operator overloading - extension methods 2

2 Java: Primitive types vs. object types Java primitive types - int, bool, char, etc - Cannot be null Java object types - Object, String, arrays etc - Wrapper types: Integer, Boolean, Character, etc - All user defined types - Object types inherit from class Object - Null is a value in any object type 3 C#: Reference types vs. value types Value types in C# - Simple types: int, bool, char, etc - Also user defined structs - Value types inherit from class ValueType Reference types in C# - All other types - All user defined classes - Inherit from class Object - Null is a value of any reference type 4

3 Top layers of the type hierarchy Class Object Class String Class Array Class ValueType Class Delegate other classes array types delegate types Class Enum simple types enum types struct types 5 Machine model Objects in heap, structs and values on stack Stack Heap M2 frame M2 frame M2 frame M1 frame arr1 arr2 x y 0 1 void M1() { int[] arr1 = new int[3]; var arr2 = arr1; int x = 0; int y = x; y = 1; 6

4 Machine model Memory divided into heap and stack - Method invocations allocate space for local variables on stack. This is called a stack frame - Space allocated on the heap has longer lifetime and is deallocated using garbage collection - Variables of value types allocated directly in stack - Variables of reference types are allocated in heap, only reference left on stack - When method invocations return the stack frame space is deallocated 7 Structs: User defined value types public class Point { protected internal int x, y; public Point(int x, int y) { this.x = x; this.y = y; public void Move(int dx, int dy) { x += dx; y += dy; public override String ToString() { return "(" + x + ", " + y + ")"; public struct SPoint { internal int x, y; public SPoint(int x, int y) { this.x = x; this.y = y; public SPoint Move(int dx, int dy) { x += dx; y += dy; return this; public override String ToString() { return "(" + x + ", " + y + ")"; 8

5 Example 71 public static void M1() { Point p = new Point(11, 111), q = new Point(22, 222); p = q; p.x = 33; SPoint r = new SPoint(44, 444), s = new SPoint(55, 555); r = s; r.x = 66; int[] iarr1 = new int[4]; int[] iarr2 = iarr1; iarr1[0] = 77; SPoint[] sarr = new SPoint[3]; sarr[0].x = 88; Console.WriteLine("q.x={0 s.x={1 iarr2[0]={2", p.x, s.x, iarr2[0]); M2(2); 9 Memory model Example 71 Stack Heap M2 frame i 0 M2 frame i 1 M2 frame i sarr iarr2 2 x y x 0 x 0 y 0 y 0 0 iarr1 s x y 555 r q x y : Point x 11 y 111 : Point x 33 y 222 M1 frame p 10

6 Struct benefits - Code often clearer using structs - Read and write performance better When to use structs On the other hand - Copying (big) structs is expensive - This means method calls using structs can be expensive (can call by reference) Structs cannot inherit but can implement interfaces 11 Boxing and unboxing Assignment of struct to object creates a copy on heap. This is called boxing SPoint p = new SPoint(11, 22); // Create a struct value in p SPoint[] arr = { p, p ; // Two more copies of p arr[0].x = 33; Console.WriteLine(arr[0] + " " + arr[1]); // Prints (33, 22) (11, 22) Object o = p; // Another copy of p, in heap p.x = 44; Console.WriteLine(p + " " + o); // Prints (44, 22) (11, 22) Console.WriteLine(o is SPoint); // Prints True Console.WriteLine(o is int); // Prints False 12

7 Two dimensional arrays A Java 2D array is an array of arrays C# also has proper 2D arrays. These are less flexible but faster to access Java/C# double[][] an array of arrays C# double[,] a rectangular array 13 Example 38 // Rectangular array creation double[,] r1 = { { 0.0, 0.1, { 1.0, 1.1, { 2.0, 2.1 ; double[,] r2 = new double[3,2]; for (int i=0; i<3; i++) for (int j=0; j<2; j++) r2[i,j] = i * j; // Jagged array creation double[] row0 = { 0.0, row1 = { 1.0, 1.1, row2 = { 2.0, 2.1, 2.2 ; double[][] t1 = { row0, row1, row2 ; double[][] t2 = { new double[] {0.0, new double[] {1.0, 1.1, new double[] {2.0, 2.1, 2.2; double[][] t3 = new double[3][]; // Create first dimension array for (int i=0; i<3; i++) { t3[i] = new double[i+1]; for (int j=0; j<=i; j++) t3[i][j] = i * j; // Create second dimension arrays // double[][] t4 = new double[3][3]; // Illegal array creation 14

8 Method calls public static void Swap(int x, int y) { int tmp = x; x = y; y = tmp; What do these methods do? public static void Swap(int[] x, int[] y) { int[] tmp = x; x = y; y = tmp; public static void Swap(int[] arr, int x, int y) { int tmp = arr[x]; arr[x] = arr[y]; arr[y] = tmp; public static void Main() { int a = 1, b = 2; Swap(a,b); Console.WriteLine("a = {0, b = {1", a, b); int[] myarr = new int[3], myarr2 = new int[3]; myarr2[0] = 1; Swap(myArr, myarr2); Console.WriteLine("myArr[0] = {0, myarr2[0] = {1", myarr[0], myarr2[0]); int[] myarr3 = new int[2]; myarr3[1] = 1; Swap(myArr3, 0, 1); Console.WriteLine("myArr3[0] = {0, myarr3[1] = {1", myarr3[0], myarr3[1]); 16

9 public static void Swap(int x, int y) { int tmp = x; x = y; y = tmp; What do these methods do Formal parameter public static void Swap(int[] x, int[] y) { int[] tmp = x; x = y; y = tmp; public static void Swap(int[] arr, int x, int y) { int tmp = arr[x]; arr[x] = arr[y]; arr[y] = tmp; public static void Main() { Actual int a = 1, b = 2; parameter Swap(a,b); Console.WriteLine("a = {0, b = {1", a, b); int[] myarr = new int[3], myarr2 = new int[3]; myarr2[0] = 1; Swap(myArr, myarr2); Console.WriteLine("myArr[0] = {0, myarr2[0] = {1", myarr[0], myarr2[0]); int[] myarr3 = new int[2]; myarr3[1] = 1; Swap(myArr3, 0, 1); Console.WriteLine("myArr3[0] = {0, myarr3[1] = {1", myarr3[0], myarr3[1]); 17 Method calls in C# and Java are by value First actual parameters are evaluated Method calls Then their values are copied to called methods stack frame In case of reference types, the value is a pointer Exercise: Draw the heap and stack as it develops during execution of method Main slide 16 18

10 Example: method calls with structs public static SPoint Reflect(SPoint p) { return new SPoint(p.y, p.x); SPoint p = new SPoint(0,1); SPoint q = Reflect(p); Console.WriteLine(q); 19 C# also allows call by reference Call by reference in C# public static void Swap(ref int x, ref int y) { int tmp = x; x = y; y = tmp; int a = 0; int b = 1; Swap(ref a, ref b); Console.WriteLine("a = {0, b = {1", a, b); public static void Reflect(ref SPoint p) { int tmp = p.x; p.x = p.y; p.y = tmp; p = new SPoint(0,1); Reflect(ref p); Console.WriteLine(p); 20

11 Method calls in call-by-reference First actual parameters are evaluated Then pointers to their values are copied to called methods stack frame This allows for more efficient manipulations of values without copying 21 Example 94 double d1 = 1.1, d2 = 2.2; int[] a1 = new int[4], a2 = new int[4]; M(d1, ref d2, a1, ref a2); static void M(double dd1, ref double dd2, int[] aa1, ref int[] aa2) { dd1 = 3.3; dd2 = 4.4; aa1[0] = 17; aa2[0] = 18; aa2 = new int[3]; aa1 = aa2; dd1 In M: 3.3 d1 In Main: 1.1 Heap: dd2 d2 4.4 aa1 a aa2 a

12 Call of instance methods Call of instance methods on structs pass struct by reference public struct SPoint { internal int x, y; public SPoint(int x, int y) { this.x = x; this.y = y; public void Reflect() { int tmp = x; x = y; y = tmp public override String ToString() { return "(" + x + ", " + y + ")"; SPoint p = new SPoint(0,1); p.reflect(); // Changes p 23 Garbage collection Consider the following degenerate code public void M() { Object o = new Object(); Object o is allocated in heap But when M returns the only reference to it is deallocated So then object o just wastes space in memory Run-time system garbage collects o 24

13 Other C# constructions Operator overloading Operators ==,!=, +, *, can be overloaded An operator is a public static method Works well with struct types struct Frac : IComparable { public readonly long n, d; // NB: Meaningful only if d!=0 public static Frac operator+(frac r1, Frac r2) { return new Frac(r1.n*r2.d+r2.n*r1.d, r1.d*r2.d); public static Frac operator*(frac r1, Frac r2) { return new Frac(r1.n*r2.n, r1.d*r2.d); public static bool operator==(frac r1, Frac r2) { return r1.n==r2.n && r1.d==r2.d; public static bool operator!=(frac r1, Frac r2) { return r1.n!=r2.n r1.d!=r2.d; Frac x = new Frac(2,3), y = new Frac(3,5), z = new Frac(28,17); x+y*z == z*y+x 26

14 User defined conversions Special methods that transform values Implicit (automatic), explicit (by cast) struct Frac : IComparable { // Implicit conversion from int to Frac: public static implicit operator Frac(int n) { return new Frac(n, 1); // Explicit conversion from Frac to double: public static explicit operator double(frac r) { return ((double)r.n)/r.d; Frac x = 1; double y = double(x); Frac z = x + 14; 27 Can be added to existing type Are defined separately in a static class static class BoolExtensions { public static void Print(this bool b) { Console.WriteLine(b? ja : nej ); Are called like instance methods (1 == 1).Print(); Extension methods No instances allowed 28

15 Extension methods as syntactic sugar A call to extension method public static void M(this C x, int y) { o.m(42); Is just syntactic sugar for M(o, 42); Not the same as an instance method call e.g. extension methods may not refer to private fields Also subtleties on structs Extension methods are always non-virtual 29 Week numbers (1-53) on DateTime Let s teach.net ISO week numbers static class DateTimeExtensions { public static int IsoWeek (this DateTime dt) { Implementation omitted Use it like any System.DateTime method int thisweek = DateTime.Today.IsoWeek(); 30

16 Extension methods on interfaces public static bool IsSorted(this IEnumerable<T> xs) where T : IComparable<T> { var etor = xs.getenumerator(); if (etor.movenext()) { T prev = etor.current; while (etor.movenext()) if (prev.compareto(etor.current) > 0) return false else prev = etor.current; return true; Only for data that have CompareTo() double[] darr = {4.5, 1.6, 5.6, 7.9 ; Console.WriteLine(darr.IsSorted()); 31 Extension methods on structs An instance method on a struct passes the struct by reference, so that fields can be updated An extension method call passes it by value So an extension method is not an instance method struct MyStruct { internal int x; public void RealIncrement() { x++; By value: Pass a copy of struct static class MyStructExtensions { public static void UselessIncrement(this mystruct b) { b.x++; No effect on b.x b.uselessincrement(); 32

17 This weeks intended learning outcomes After this week you should be able to - Explain difference between reference types and value types - Draw machine model and use it to explain program behaviour Explain assignments in machine model Explain method calls in machine model - Create and use structs - Create and use call-by-reference methods - Create and use extension methods and overloaded operators 33

Introduce C# as Object Oriented programming language. Explain, tokens,

Introduce C# as Object Oriented programming language. Explain, tokens, Module 2 98 Assignment 1 Introduce C# as Object Oriented programming language. Explain, tokens, lexicals and control flow constructs. 99 The C# Family Tree C Platform Independence C++ Object Orientation

More information

Introduction to Programming Using Java (98-388)

Introduction to Programming Using Java (98-388) Introduction to Programming Using Java (98-388) Understand Java fundamentals Describe the use of main in a Java application Signature of main, why it is static; how to consume an instance of your own class;

More information

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh

C# Fundamentals. Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh C# Fundamentals Hans-Wolfgang Loidl School of Mathematical and Computer Sciences, Heriot-Watt University, Edinburgh Semester 1 2018/19 H-W. Loidl (Heriot-Watt Univ) F20SC/F21SC 2018/19

More information

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals

C# Types. Industrial Programming. Value Types. Signed and Unsigned. Lecture 3: C# Fundamentals C# Types Industrial Programming Lecture 3: C# Fundamentals Industrial Programming 1 Industrial Programming 2 Value Types Memory location contains the data. Integers: Signed: sbyte, int, short, long Unsigned:

More information

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages

Agenda. Objects and classes Encapsulation and information hiding Documentation Packages Preliminaries II 1 Agenda Objects and classes Encapsulation and information hiding Documentation Packages Inheritance Polymorphism Implementation of inheritance in Java Abstract classes Interfaces Generics

More information

Industrial Programming

Industrial Programming Industrial Programming Lecture 3: C# Fundamentals Industrial Programming 1 C# Types Industrial Programming 2 Value Types Memory location contains the data. Integers: Signed: sbyte, int, short, long Unsigned:

More information

G Programming Languages - Fall 2012

G Programming Languages - Fall 2012 G22.2110-003 Programming Languages - Fall 2012 Lecture 12 Thomas Wies New York University Review Last lecture Modules Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 26 March 26, 2015 Inheritance and Dynamic Dispatch Chapter 24 public interface Displaceable { public int getx(); public int gety(); public void move

More information

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes

inside: THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 PROGRAMMING McCluskey: Working with C# Classes THE MAGAZINE OF USENIX & SAGE August 2003 volume 28 number 4 inside: PROGRAMMING McCluskey: Working with C# Classes & The Advanced Computing Systems Association & The System Administrators Guild working

More information

Chapter 2. Procedural Programming

Chapter 2. Procedural Programming Chapter 2 Procedural Programming 2: Preview Basic concepts that are similar in both Java and C++, including: standard data types control structures I/O functions Dynamic memory management, and some basic

More information

Language Specification

Language Specification # Language Specification File: C# Language Specification.doc Last saved: 5/7/2001 Version 0.28 Copyright? Microsoft Corporation 1999-2000. All Rights Reserved. Please send corrections, comments, and other

More information

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations

C#.Net. Course Contents. Course contents VT BizTalk. No exam, but laborations , 1 C#.Net VT 2009 Course Contents C# 6 hp approx. BizTalk 1,5 hp approx. No exam, but laborations Course contents Architecture Visual Studio Syntax Classes Forms Class Libraries Inheritance Other C# essentials

More information

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University 9/5/6 CS Introduction to Computing II Wayne Snyder Department Boston University Today: Arrays (D and D) Methods Program structure Fields vs local variables Next time: Program structure continued: Classes

More information

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings 19/10/2017 CE221 Part 2 1 Variables and References 1 In Java a variable of primitive type is associated with a memory location

More information

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. CMSC 131: Chapter 28 Final Review: What you learned this semester The Big Picture Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach. Java

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 26 March 23, 2016 Inheritance and Dynamic Dispatch Chapter 24 Inheritance Example public class { private int x; public () { x = 0; } public void incby(int

More information

2. The object-oriented paradigm!

2. The object-oriented paradigm! 2. The object-oriented paradigm! Plan for this section:! n Look at things we have to be able to do with a programming language! n Look at Java and how it is done there" Note: I will make a lot of use of

More information

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals

CS/ENGRD 2110 FALL Lecture 6: Consequence of type, casting; function equals CS/ENGRD 2110 FALL 2018 Lecture 6: Consequence of type, casting; function equals http://courses.cs.cornell.edu/cs2110 Overview references in 2 Quick look at arrays: array Casting among classes cast, object-casting

More information

Short Notes of CS201

Short Notes of CS201 #includes: Short Notes of CS201 The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with < and > if the file is a system

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 25 November 1, 2017 Inheritance and Dynamic Dispatch (Chapter 24) Announcements HW7: Chat Client Available Soon Due: Tuesday, November 14 th at 11:59pm

More information

CS201 - Introduction to Programming Glossary By

CS201 - Introduction to Programming Glossary By CS201 - Introduction to Programming Glossary By #include : The #include directive instructs the preprocessor to read and include a file into a source code file. The file name is typically enclosed with

More information

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University

G Programming Languages Spring 2010 Lecture 9. Robert Grimm, New York University G22.2110-001 Programming Languages Spring 2010 Lecture 9 Robert Grimm, New York University 1 Review Last week Modules 2 Outline Classes Encapsulation and Inheritance Initialization and Finalization Dynamic

More information

CPSC 3740 Programming Languages University of Lethbridge. Data Types

CPSC 3740 Programming Languages University of Lethbridge. Data Types Data Types A data type defines a collection of data values and a set of predefined operations on those values Some languages allow user to define additional types Useful for error detection through type

More information

Compiler construction 2009

Compiler construction 2009 Compiler construction 2009 Lecture 6 Some project extensions. Pointers and heap allocation. Object-oriented languages. Module systems. Memory structure Javalette restrictions Only local variables and parameters

More information

PIC 20A The Basics of Java

PIC 20A The Basics of Java PIC 20A The Basics of Java Ernest Ryu UCLA Mathematics Last edited: November 1, 2017 Outline Variables Control structures classes Compilation final and static modifiers Arrays Examples: String, Math, and

More information

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... CISC 2000 Computer Science II Fall, 2014 Note 12/1/2014 1 Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance... (a) What s the purpose of inheritance?

More information

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette

COMP 250: Java Programming I. Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette COMP 250: Java Programming I Carlos G. Oliver, Jérôme Waldispühl January 17-18, 2018 Slides adapted from M. Blanchette Variables and types [Downey Ch 2] Variable: temporary storage location in memory.

More information

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists

Lecture 5: Outline. I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Lecture 5: Outline I. Multi- dimensional arrays II. Multi- level arrays III. Structures IV. Data alignment V. Linked Lists Multidimensional arrays: 2D Declaration int a[3][4]; /*Conceptually 2D matrix

More information

TYPES, VALUES AND DECLARATIONS

TYPES, VALUES AND DECLARATIONS COSC 2P90 TYPES, VALUES AND DECLARATIONS (c) S. Thompson, M. Winters 1 Names, References, Values & Types data items have a value and a type type determines set of operations variables Have an identifier

More information

C++ (classes) Hwansoo Han

C++ (classes) Hwansoo Han C++ (classes) Hwansoo Han Inheritance Relation among classes shape, rectangle, triangle, circle, shape rectangle triangle circle 2 Base Class: shape Members of a class Methods : rotate(), move(), Shape(),

More information

CS 200 Objects and ArrayList Jim Williams, PhD

CS 200 Objects and ArrayList Jim Williams, PhD CS 200 Objects and ArrayList Jim Williams, PhD This Week 1. Academic Integrity 2. BP1: Milestone 2 due this week 3. Team Lab: Multi-Dimensional Arrays a. Bring paper and pencil to draw diagrams. b. Code

More information

Dynamic Data Structures. CSCI 112: Programming in C

Dynamic Data Structures. CSCI 112: Programming in C Dynamic Data Structures CSCI 112: Programming in C 1 It s all about flexibility In the programs we ve made so far, the compiler knows at compile time exactly how much memory to allocate for each variable

More information

Computer Science II (20082) Week 1: Review and Inheritance

Computer Science II (20082) Week 1: Review and Inheritance Computer Science II 4003-232-08 (20082) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Syntax and Semantics of Formal (e.g. Programming) Languages Syntax

More information

C# and Java. C# and Java are both modern object-oriented languages

C# and Java. C# and Java are both modern object-oriented languages C# and Java C# and Java are both modern object-oriented languages C# came after Java and so it is more advanced in some ways C# has more functional characteristics (e.g., anonymous functions, closure,

More information

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib.

Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations. <stdlib. 1 The previous lecture Other C materials before pointer Common library functions [Appendix of K&R] 2D array, string manipulations Pointer basics 1 Common library functions [Appendix of K+R]

More information

Chapter 2: Using Data

Chapter 2: Using Data Chapter 2: Using Data TRUE/FALSE 1. A variable can hold more than one value at a time. F PTS: 1 REF: 52 2. The legal integer values are -2 31 through 2 31-1. These are the highest and lowest values that

More information

Java Classes & Primitive Types

Java Classes & Primitive Types Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.)

More information

Class Information ANNOUCEMENTS

Class Information ANNOUCEMENTS Class Information ANNOUCEMENTS Third homework due TODAY at 11:59pm. Extension? First project has been posted, due Monday October 23, 11:59pm. Midterm exam: Friday, October 27, in class. Don t forget to

More information

Object-oriented Programming. Object-oriented Programming

Object-oriented Programming. Object-oriented Programming 2014-06-13 Object-oriented Programming Object-oriented Programming 2014-06-13 Object-oriented Programming 1 Object-oriented Languages object-based: language that supports objects class-based: language

More information

A brief introduction to C programming for Java programmers

A brief introduction to C programming for Java programmers A brief introduction to C programming for Java programmers Sven Gestegård Robertz September 2017 There are many similarities between Java and C. The syntax in Java is basically

More information

References and pointers

References and pointers References and pointers Pointers are variables whose value is a reference, i.e. an address of a store location. Early languages (Fortran, COBOL, Algol 60) had no pointers; added to Fortran 90. In Pascal,

More information

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management Session 8 Memory Management The issues Dr Christos Kloukinas City, UoL http://staff.city.ac.uk/c.kloukinas/cpp (slides originally produced by Dr Ross Paterson) Programs manipulate data, which must be stored

More information

Introduction & Java Review. EE 564 Lecture 1

Introduction & Java Review. EE 564 Lecture 1 Introduction & Java Review EE 564 Lecture 1 Course Overview Personnel introduction Course plan: theory: OO design 1 month practice 1: eclipse ~1.5 months practice 2: Java EE ~1.5 months Plan to devote

More information

CSC Java Programming, Fall Java Data Types and Control Constructs

CSC Java Programming, Fall Java Data Types and Control Constructs CSC 243 - Java Programming, Fall 2016 Java Data Types and Control Constructs Java Types In general, a type is collection of possible values Main categories of Java types: Primitive/built-in Object/Reference

More information

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem:

CSE 504. Expression evaluation. Expression Evaluation, Runtime Environments. One possible semantics: Problem: Expression evaluation CSE 504 Order of evaluation For the abstract syntax tree + + 5 Expression Evaluation, Runtime Environments + + x 3 2 4 the equivalent expression is (x + 3) + (2 + 4) + 5 1 2 (. Contd

More information

Instance Members and Static Members

Instance Members and Static Members Instance Members and Static Members You may notice that all the members are declared w/o static. These members belong to some specific object. They are called instance members. This implies that these

More information

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring

Outline. Java Models for variables Types and type checking, type safety Interpretation vs. compilation. Reasoning about code. CSCI 2600 Spring Java Outline Java Models for variables Types and type checking, type safety Interpretation vs. compilation Reasoning about code CSCI 2600 Spring 2017 2 Java Java is a successor to a number of languages,

More information

Common Misunderstandings from Exam 1 Material

Common Misunderstandings from Exam 1 Material Common Misunderstandings from Exam 1 Material Kyle Dewey Stack and Heap Allocation with Pointers char c = c ; char* p1 = malloc(sizeof(char)); char** p2 = &p1; Where is c allocated? Where is p1 itself

More information

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst

Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst Operating Systems CMPSCI 377, Lec 2 Intro to C/C++ Prashant Shenoy University of Massachusetts Amherst Department of Computer Science Why C? Low-level Direct access to memory WYSIWYG (more or less) Effectively

More information

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to.

Pointers. A pointer value is the address of the first byte of the pointed object in the memory. A pointer does not know how many bytes it points to. Pointers A pointer is a memory address of an object of a specified type, or it is a variable which keeps such an address. Pointer properties: P (pointer) 12316 12316 (address) Typed object A pointer value

More information

CS201- Introduction to Programming Current Quizzes

CS201- Introduction to Programming Current Quizzes CS201- Introduction to Programming Current Quizzes Q.1 char name [] = Hello World ; In the above statement, a memory of characters will be allocated 13 11 12 (Ans) Q.2 A function is a block of statements

More information

C# Language Specification

C# Language Specification Standard ECMA- December 00 Standardizing Information and Communication Systems C# Language Specification. Phone: +.0.00 - Fax: +.0.0 - URL: http://www.ecma.ch - Internet: helpdesk@ecma.ch Brief history

More information

DAD Lab. 1 Introduc7on to C#

DAD Lab. 1 Introduc7on to C# DAD 2017-18 Lab. 1 Introduc7on to C# Summary 1..NET Framework Architecture 2. C# Language Syntax C# vs. Java vs C++ 3. IDE: MS Visual Studio Tools Console and WinForm Applica7ons 1..NET Framework Introduc7on

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 24 October 29, 2018 Arrays, Java ASM Chapter 21 and 22 Announcements HW6: Java Programming (Pennstagram) Due TOMORROW at 11:59pm Reminder: please complete

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 22 March 14 th, 2016 Object Oriented Programming in Java Java Bootcamp tonight Announcements Monday, March 14 from 6-8pm in Levine 101 (Wu & Chen)

More information

Example: Count of Points

Example: Count of Points Example: Count of Points 1 class Point { 2... 3 private static int numofpoints = 0; 4 5 Point() { 6 numofpoints++; 7 } 8 9 Point(int x, int y) { 10 this(); // calling the constructor with no input argument;

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 28 March 30, 2018 Overriding Methods, Equality, Enums, Iterators Chapters 25 and 26 Method Overriding When a subclass replaces an inherited method

More information

Multidimension array, array of strings

Multidimension array, array of strings 1 Multidimension array, array of strings char messages[3][7] ={ Hello, Hi, There ; Array of strings 0 1 2 0 1 2 3 4 5 6 H e l l o \0 H i \0 T h e r e \0 Each row (e.g., message[0]) is a char array (string)

More information

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17 List of Programs xxv List of Figures xxix List of Tables xxxiii Preface to second version xxxv PART 1 Structured Programming 1 1 Getting started 3 1.1 Programming 3 1.2 Editing source code 5 Source code

More information

CSE 431S Type Checking. Washington University Spring 2013

CSE 431S Type Checking. Washington University Spring 2013 CSE 431S Type Checking Washington University Spring 2013 Type Checking When are types checked? Statically at compile time Compiler does type checking during compilation Ideally eliminate runtime checks

More information

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking

CS 430 Spring Mike Lam, Professor. Data Types and Type Checking CS 430 Spring 2015 Mike Lam, Professor Data Types and Type Checking Type Systems Type system Rules about valid types, type compatibility, and how data values can be used Benefits of a robust type system

More information

Programming Languages and Techniques (CIS120)

Programming Languages and Techniques (CIS120) Programming Languages and Techniques (CIS120) Lecture 21 March 12, 2018 Java: Objects, Interfaces, Static Members Chapters 19 & 20 Announcements Java Bootcamp Tonight!! Towne 100, 6-8 pm HW06: Pennstagram

More information

C# Language Reference

C# Language Reference C# Language Reference Owners: Anders Hejlsberg and Scott Wiltamuth File: clangref Last saved: 6/26/2000 Last printed: 7/10/2000 Version 0.17b Copyright Microsoft Corporation 1999-2000. All Rights Reserved.

More information

Course Hours

Course Hours Programming the.net Framework 4.0/4.5 with C# 5.0 Course 70240 40 Hours Microsoft's.NET Framework presents developers with unprecedented opportunities. From 'geoscalable' web applications to desktop and

More information

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++

Course Text. Course Description. Course Objectives. StraighterLine Introduction to Programming in C++ Introduction to Programming in C++ Course Text Programming in C++, Zyante, Fall 2013 edition. Course book provided along with the course. Course Description This course introduces programming in C++ and

More information

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java

Agenda CS121/IS223. Reminder. Object Declaration, Creation, Assignment. What is Going On? Variables in Java CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors Agenda

More information

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects.

More Java Basics. class Vector { Object[] myarray;... //insert x in the array void insert(object x) {...} Then we can use Vector to hold any objects. More Java Basics 1. INHERITANCE AND DYNAMIC TYPE-CASTING Java performs automatic type conversion from a sub-type to a super-type. That is, if a method requires a parameter of type A, we can call the method

More information

C11: Garbage Collection and Constructors

C11: Garbage Collection and Constructors CISC 3120 C11: Garbage Collection and Constructors Hui Chen Department of Computer & Information Science CUNY Brooklyn College 10/5/2017 CUNY Brooklyn College 1 Outline Recap Project progress and lessons

More information

Instantiation of Template class

Instantiation of Template class Class Templates Templates are like advanced macros. They are useful for building new classes that depend on already existing user defined classes or built-in types. Example: stack of int or stack of double

More information

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction

Data Types. Data Types. Introduction. Data Types. Data Types. Data Types. Introduction Introduction Primitive Composite Structured Abstract Introduction Introduction Data Type is a Collection of Data Objects Possible r-values for a memory cell Set of operations on those objects Descriptor

More information

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class.

Hierarchical inheritance: Contains one base class and multiple derived classes of the same base class. 1. What is C#? C# (pronounced "C sharp") is a simple, modern, object oriented, and type safe programming language. It will immediately be familiar to C and C++ programmers. C# combines the high productivity

More information

Java Classes & Primitive Types

Java Classes & Primitive Types Java Classes & Primitive Types Rui Moreira Classes Ponto (from figgeom) x : int = 0 y : int = 0 n Attributes q Characteristics/properties of classes q Primitive types (e.g., char, byte, int, float, etc.)

More information

Type Conversion. and. Statements

Type Conversion. and. Statements and Statements Type conversion changing a value from one type to another Void Integral Floating Point Derived Boolean Character Integer Real Imaginary Complex no fractional part fractional part 2 tj Suppose

More information

Java and C CSE 351 Spring

Java and C CSE 351 Spring Java and C CSE 351 Spring 2018 https://xkcd.com/801/ Roadmap C: car *c = malloc(sizeof(car)); c->miles = 100; c->gals = 17; float mpg = get_mpg(c); free(c); Assembly language: Machine code: get_mpg: pushq

More information

CS121/IS223. Object Reference Variables. Dr Olly Gotel

CS121/IS223. Object Reference Variables. Dr Olly Gotel CS121/IS223 Object Reference Variables Dr Olly Gotel ogotel@pace.edu http://csis.pace.edu/~ogotel Having problems? -- Come see me or call me in my office hours -- Use the CSIS programming tutors CS121/IS223

More information

Computer Science II (20073) Week 1: Review and Inheritance

Computer Science II (20073) Week 1: Review and Inheritance Computer Science II 4003-232-01 (20073) Week 1: Review and Inheritance Richard Zanibbi Rochester Institute of Technology Review of CS-I Hardware and Software Hardware Physical devices in a computer system

More information

Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class

Remedial classes. G51PRG: Introduction to Programming Second semester Lecture 2. Plan of the lecture. Classes and Objects. Example: Point class G51PRG: Introduction to Programming Second semester Lecture 2 Remedial classes Monday 3-5 in A32 Contact Yan Su (yxs) Ordinary labs start on Tuesday Natasha Alechina School of Computer Science & IT nza@cs.nott.ac.uk

More information

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented

Subclass Gist Example: Chess Super Keyword Shadowing Overriding Why? L10 - Polymorphism and Abstract Classes The Four Principles of Object Oriented Table of Contents L01 - Introduction L02 - Strings Some Examples Reserved Characters Operations Immutability Equality Wrappers and Primitives Boxing/Unboxing Boxing Unboxing Formatting L03 - Input and

More information

C# Adds Useful Features

C# Adds Useful Features C# Adds Useful Features Events and delegates are included to handle asynchronous actions (like keyboard or mouse actions). Properties allow user-defined read and write actions for fields. You can add get

More information

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

SEMANTIC ANALYSIS TYPES AND DECLARATIONS SEMANTIC ANALYSIS CS 403: Type Checking Stefan D. Bruda Winter 2015 Parsing only verifies that the program consists of tokens arranged in a syntactically valid combination now we move to check whether

More information

Introduction to Programming (Java) 4/12

Introduction to Programming (Java) 4/12 Introduction to Programming (Java) 4/12 Michal Krátký Department of Computer Science Technical University of Ostrava Introduction to Programming (Java) 2008/2009 c 2006 2008 Michal Krátký Introduction

More information

CS 314 Principles of Programming Languages

CS 314 Principles of Programming Languages CS 314 Principles of Programming Languages Lecture 15: Review and Functional Programming Zheng (Eddy) Zhang Rutgers University March 19, 2018 Class Information Midterm exam forum open in Sakai. HW4 and

More information

Memory and C++ Pointers

Memory and C++ Pointers Memory and C++ Pointers C++ objects and memory C++ primitive types and memory Note: primitive types = int, long, float, double, char, January 2010 Greg Mori 2 // Java code // in function, f int arr[];

More information

Chapter 1 Getting Started

Chapter 1 Getting Started Chapter 1 Getting Started The C# class Just like all object oriented programming languages, C# supports the concept of a class. A class is a little like a data structure in that it aggregates different

More information

Modern Programming Languages. Lecture Java Programming Language. An Introduction

Modern Programming Languages. Lecture Java Programming Language. An Introduction Modern Programming Languages Lecture 27-30 Java Programming Language An Introduction 107 Java was developed at Sun in the early 1990s and is based on C++. It looks very similar to C++ but it is significantly

More information

CS162: Introduction to Computer Science II

CS162: Introduction to Computer Science II CS162: Introduction to Computer Science II Java Fundamentals 1 Primitive Types 2 1 Primitive types: Primitive types byte, short, int, long, float, double, char, boolean Example: int size = 42; size is

More information

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018

CSC207H: Software Design. Java + OOP. CSC207 Winter 2018 Java + OOP CSC207 Winter 2018 1 Why OOP? Modularity: code can be written and maintained separately, and easily passed around the system Information-hiding: internal representation hidden from the outside

More information

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012)

COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) COE318 Lecture Notes: Week 3 1 of 8 COE318 Lecture Notes Week 3 (Week of Sept 17, 2012) Announcements Quiz (5% of total mark) on Wednesday, September 26, 2012. Covers weeks 1 3. This includes both the

More information

Operators and Expressions

Operators and Expressions Operators and Expressions Conversions. Widening and Narrowing Primitive Conversions Widening and Narrowing Reference Conversions Conversions up the type hierarchy are called widening reference conversions

More information

CS 520 Theory and Practice of Software Engineering Fall 2018

CS 520 Theory and Practice of Software Engineering Fall 2018 Logistics CS 520 Theory and Practice of Software Engineering Fall 2018 Best and worst programming practices September 11, 2018 Reminder Recap: software architecture vs. design Class website: https://people.cs.umass.edu/~brun/class/2018fall/cs520/

More information

CS 211: Methods, Memory, Equality

CS 211: Methods, Memory, Equality CS 211: Methods, Memory, Equality Chris Kauffman Week 2-1 So far... Comments Statements/Expressions Variable Types little types, what about Big types? Assignment Basic Output (Input?) Conditionals (if-else)

More information

Lecture 3: C Programm

Lecture 3: C Programm 0 3 E CS 1 Lecture 3: C Programm ing Reading Quiz Note the intimidating red border! 2 A variable is: A. an area in memory that is reserved at run time to hold a value of particular type B. an area in memory

More information

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments

Administrivia. Java Review. Objects and Variables. Demo. Example. Example: Assignments CMSC433, Spring 2004 Programming Language Technology and Paradigms Java Review Jeff Foster Feburary 3, 2004 Administrivia Reading: Liskov, ch 4, optional Eckel, ch 8, 9 Project 1 posted Part 2 was revised

More information

CS24 Week 2 Lecture 1

CS24 Week 2 Lecture 1 CS24 Week 2 Lecture 1 Kyle Dewey Overview C Review Void pointers Allocation structs void* (Void Pointers) void* Like any other pointer, it refers to some memory address However, it has no associated type,

More information

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes,

Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes, Chapter 5 Basic Semantics Attributes, Bindings, and Semantic Functions Declarations, Blocks, Scope, and the Symbol Table Name Resolution and Overloading Allocation, Lifetimes, and the Environment Variables

More information

Lecture 3. Lecture

Lecture 3. Lecture True Object-Oriented programming: Dynamic Objects Static Object-Oriented Programming Reference Variables Eckel: 30-31, 41-46, 107-111, 114-115 Riley: 5.1, 5.2 D0010E Object-Oriented Programming and Design

More information

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017

Pointers (part 1) What are pointers? EECS We have seen pointers before. scanf( %f, &inches );! 25 September 2017 Pointers (part 1) EECS 2031 25 September 2017 1 What are pointers? We have seen pointers before. scanf( %f, &inches );! 2 1 Example char c; c = getchar(); printf( %c, c); char c; char *p; c = getchar();

More information

COE318 Lecture Notes Week 6 (Oct 10, 2011)

COE318 Lecture Notes Week 6 (Oct 10, 2011) COE318 Software Systems Lecture Notes: Week 6 1 of 8 COE318 Lecture Notes Week 6 (Oct 10, 2011) Topics Announcements final qualifiers Example: An alternative to arrays == vs..equals(...): A first look

More information

CS 520 Theory and Practice of Software Engineering Fall 2017

CS 520 Theory and Practice of Software Engineering Fall 2017 Logistics CS 520 Theory and Practice of Software Engineering Fall 2017 Best and worst programming practices September 12, 2017 Recap: software architecture vs. design Recap: software architecture examples

More information

CS558 Programming Languages

CS558 Programming Languages CS558 Programming Languages Fall 2016 Lecture 4a Andrew Tolmach Portland State University 1994-2016 Pragmatics of Large Values Real machines are very efficient at handling word-size chunks of data (e.g.

More information