Parameter Passing & Array Examples – Part 1

Slides:



Advertisements
Similar presentations
Carnegie Mellon 1 Dynamic Memory Allocation: Basic Concepts : Introduction to Computer Systems 17 th Lecture, Oct. 21, 2010 Instructors: Randy Bryant.
Advertisements

Topic 10 Java Memory Management. 1-2 Memory Allocation in Java When a program is being executed, separate areas of memory are allocated for each class.
Chris Riesbeck, Fall 2007 Dynamic Memory Allocation Today Dynamic memory allocation – mechanisms & policies Memory bugs.
EC-241 Object-Oriented Programming
Passing information through Parameters ( our setter methods or mutators) Formal Parameter – parameter in the method. public void SetA(int x){ (int x) is.
Principles of programming languages 4: Parameter passing, Scope rules Department of Information Science and Engineering Isao Sasano.
Memory Management & Method Calls in Java Program Execution © Allan C. Milne v
Memory Arrangement Memory is arrange in a sequence of addressable units (usually bytes) –sizeof( ) return the number of units it takes to store a type.
Stacks. 2 Outline and Reading The Stack ADT (§2.1.1) Applications of Stacks (§2.1.1) Array-based implementation (§2.1.1) Growable array-based stack (§1.5)
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
Runtime Environments Compiler Construction Chapter 7.
Computer Science and Software Engineering University of Wisconsin - Platteville 2. Pointer Yan Shi CS/SE2630 Lecture Notes.
Current Assignments Homework 3 is due tonight. Iteration and basic functions. Exam 1 on Monday.
Chapter 7 Pointers: Java does not have pointers. Used for dynamic memory allocation.
CSI1390 – Java Programming Methods II Instructor: Saeid Nourian
COP 2800 Lake Sumter State College Mark Wilson, Instructor.
University of Malta CSA2090: Lecture 4 © Chris Staff 1 of 20 CSA2090: Systems Programming Introduction to C Dr. Christopher Staff.
1 Advanced Programming Examples Output. Show the exact output produced by the following code segment. char[,] pic = new char[6,6]; for (int i = 0; i
1 st Semester Module 6 C# Methods – Part II อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering.
Print Row Function void PrintRow(float x[ ][4],int i) { int j; for(j=0;j
1 st Semester Module 7 Arrays อภิรักษ์ จันทร์สร้าง Aphirak Jansang Computer Engineering Department.
Memory Management in Java Mr. Gerb Computer Science 4.
AKA the birth, life, and death of variables.
Java Array Object Chuen-Liang Chen Department of Computer Science
Arrays 2/4 By Pius Nyaanga.
Command Line Arguments
Principles of programming languages 4: Parameter passing, Scope rules
Pointers Revisited What is variable address, name, value?
Arrays Computer & Programming Group
Methods and Parameters
More Object Oriented Programming
Method Mark and Lyubo.
CSC 253 Lecture 8.
Compilation VS Interpretation
CSC 253 Lecture 8.
Overloading the << operator
Stack Memory 2 (also called Call Stack)
Dale Roberts, Lecturer IUPUI
Stacks.
Dynamic Memory A whole heap of fun….
CSE 1342 Programming Concepts
Arrays Strings and Parameter Passing CSCI N305
Pointers And Memory Acknowledgement: THE Slides are Prepared FROM SLIDES PROVIDED By NANCY M. AMATO AND Jory Denny.
Introduction to Programming
Java Lesson 36 Mr. Kalmes.
References, const and classes
Dynamic Memory A whole heap of fun….
CS2011 Introduction to Programming I Arrays (II)
Seconds to (hours,minutes,seconds) conversion
Dynamic Memory A whole heap of fun….
Module 8 & 9 Method :Part I & Array :Part II
C++ Pointers and Strings
C (and C++) Pointers April 4, 2019.
Dynamic Memory A whole heap of fun….
Dynamic Memory.
Ex1: class RefOutOverloadExample { public void SampleMethod(int i) { }
Pointer & Memory Allocation Review
EECE.3220 Data Structures Instructor: Dr. Michael Geiger Spring 2019
Arrays.
C++ Programming CLASS This pointer Static Class Friend Class
The Stack.
Pointers and References
Programming Arrays.
Overloading the << operator
C++ Pointers and Strings
How Memory Leaks Work with Memory Diagram
CIS 110: Introduction to Computer Programming
Advanced .NET Programming I 5th Lecture
Introduction to Pointers
Presentation transcript:

Parameter Passing & Array Examples – Part 1 Chalermsak Chatdokmaiprai 01204111 Computer and Programming Department of Computer Engineering Kasetsart University Cliparts are taken from http://openclipart.org

Example 1 Program's Stack Space output Main()'s Stack i 10 8 j 50 14 class MyClass { public static void Main () { int i = 10; int j = Foo( i, i+5 ); Console.WriteLine("{0},{1}", i, j); Console.WriteLine("{0},{1}", Bar(6, out i), i); } static int Foo (int x, int y) { int i; x = Bar( x, out i ) + 1; return x + y + i; static int Bar (int j, out int k) { k = j + 2; return j + k; i 10 8 j 50 50 10 15 14 50 Bar()'s Stack Foo()'s Stack j x 10 6 23 14 6 8 k y 15 i 12 22 10 23 1 22 Bar()'s Stack j 10 23 50 15 12 k 10 6 12 8 2 output 10 22 12 10,50 14,8 6 14 8

Example 2 Program's Stack Space output using C=System.Console; class MyClass { static void Main () { int i = 22; C.WriteLine("At A: {0}", i); AlterInt(i); C.WriteLine("At B: {0}", i); } static void AlterInt (int i) { C.WriteLine("At C: {0}", i); i = 55; C.WriteLine("At D: {0}", i); Main()'s Stack i 22 AlterInt()'s Stack i 55 22 output At A: 22 At C: 22 At D: 55 At B: 22

Example 3 using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {22, 99}; C.WriteLine("At A: {0}", i[0]); AlterInt(i); C.WriteLine("At B: {0}", i[0]); } static void AlterInt (int[] i) { C.WriteLine("At C: {0}", i[0]); i[0] = 55; C.WriteLine("At D: {0}", i[0]); output At A: 22 At C: 22 At D: 55 At B: 55

Array Example Program's Stack Program's Heap Main()'s Stack [0] [1] using C=System.Console; class MyClass { static void Main () { double[] a; //uninitialized a = null; //initialized to null a = new double[2];//allocated double[] b = new double[] {1.1, 2.2, 3.3}; a = b; int m = (int) a[1]; b[(m*10)%3] = b[m-1] + 10; C.WriteLine("{0}, {1}, {2}", a[0],a[1],a[2]); } a garbage b [0] 1.1 [1] 2.2 [2] 3.3 m 2 12.2 output 2 b[2] 12.2 1.1, 2.2, 12.2 2.2 10

Example 3 dissected output Program's Stack Program's Heap using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {22, 99} ; C.WriteLine("At A: {0}", i[0]); AlterInt(i); C.WriteLine("At B: {0}", i[0]); } static void AlterInt (int[] i) { C.WriteLine("At C: {0}", i[0]); i[0] = 55; C.WriteLine("At D: {0}", i[0]); Program's Stack Program's Heap Main()'s Stack i = AlterInt()'s Stack [0] [1] 55 22 i 99 output At A: 22 At C: 22 At D: 55 At B: 55

Example 3 plus output Program's Stack Program's Heap using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {22, 99} ; C.WriteLine("At A: {0}", i[0]); AlterInt(i); C.WriteLine("At B: {0}", i[0]); } static void AlterInt (int[] i) { C.WriteLine("At C: {0}", i[0]); i[0] = 55; C.WriteLine("At D: {0}", i[0]); i = new int[3] {66,77,88}; C.WriteLine("At E: {0}", i[0]); Program's Stack Program's Heap Main()'s Stack i = AlterInt()'s Stack [0] [1] 55 22 i 99 [0] 66 [1] 77 [2] 88 output i= At A: 22 At C: 22 At D: 55 At E: 66 At B: 55

Example 4 a output Stack Heap i 55 66 k 99 n 10 3 20 x 30 i 2 1 3 Main() using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {55,66}; C.WriteLine("@A:i={0},{1}", i[0], i[1]); int[] k = i; k[1] = 99; C.WriteLine("@B:i={0},{1}", i[0], i[1]); i = GetData(3); C.WriteLine("@C:i={0},{1},{2}", i[0], i[1], i[2]); C.WriteLine("@D:k={0},{1}", k[0], k[1]); } static int[] GetData (int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = (i+1)*10; C.WriteLine("@E:x={0},{1},{2}", x[0], x[1], x[2]); return x; i [0] 55 [1] 66 k 99 GetData() [0] [1] [2] n 10 3 20 x 30 i 2 1 3 output @A:i=55,66 @B:i=55,99 @E:x=10,20,30 @C:i=10,20,30 @D:k=55,99

Example 4 b Example 4 a output Stack Heap i 55 66 k 99 n 10 3 20 x 30 Main() using C=System.Console; class MyClass { static void Main () { int[] i = new int[2] {55,66}; C.WriteLine("@A:i={0},{1}", i[0], i[1]); int[] k = i; k[1] = 99; C.WriteLine("@B:i={0},{1}", i[0], i[1]); GetData(3, out i); C.WriteLine("@C:i={0},{1},{2}", i[0], i[1], i[2]); C.WriteLine("@D:k={0},{1}", k[0], k[1]); } static void GetData (int n, out int[] x) { x = new int[n]; for (int i = 0; i < n; i++) x[i] = (i+1)*10; C.WriteLine("@E:x={0},{1},{2}", x[0], x[1], x[2]); i [0] 55 [1] 66 k 99 GetData() [0] [1] [2] n 10 3 20 x 30 Example 4 a i 3 1 2 i = GetData(3); C.WriteLine("@C:i={0},{1}" C.WriteLine("@D:k={0},{1}" } static int[] GetData (int n) { int[] x = new int[n]; for (int i = 0; i < n; i++) x[i] = (i+1)*10; C.WriteLine("@E:x={0},{1},{2 return x; output @A:i=55,66 @B:i=55,99 @E:x=10,20,30 @C:i=10,20,30 @D:k=55,99

The End