1 / 14

CSCE3193: Programming Paradigms

CSCE3193: Programming Paradigms. Nilanjan Banerjee. University of Arkansas Fayetteville, AR nilanb@uark.edu http://www.csce.uark.edu/~nilanb/3193/S10/. Programming Paradigms. Memory model .

nida
Download Presentation

CSCE3193: Programming Paradigms

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSCE3193: Programming Paradigms Nilanjan Banerjee University of Arkansas Fayetteville, AR nilanb@uark.edu http://www.csce.uark.edu/~nilanb/3193/S10/ Programming Paradigms

  2. Memory model • Smallest unit of memory is called a bit (short form of binary digit). A bit can be one of two states: 0 or a 1 • Memory is usually organized as a collection of bits • 8 bits together is called a byte • Numbers are organized in units larger than a byes • Word: refers to a collection of bytes (usually 4 bytes) 0 1 0 1 0 1 0 1

  3. Numbers and base systems • There are different ways of representing numbers • Binary is base 2 • Octal is base 8 • Hexadecimal is base 16 • There are just representation of numbers • Lets take a few examples to try to understand them

  4. The following diagrams show how the number forty-two appears in both octal and hexadecimal notation: octal hexadecimal 5 2 2 A 1 1 2 x = 2 10 x = 10 8 16 5 x = 40 02 x = 32 42 42 Numbers and base systems

  5. 0000 0004 0008 000C 0010 0014 0018 001C 0020 0024 0028 002C FFD0 FFD4 FFD8 FFDC FFE0 FFE4 FFE8 FFEC FFF0 FFF4 FFF8 FFFC Addressing memory • Every byte in memory has an address • That address is represented as a hexadecimal • On the side memory is represented as a 4-digit hexadecimal number • The number of digits depends on the complete size of your memory • In Java there is no way of finding out what memory address an object is stored

  6. Allocation of memory to variables • When you declare a variable Java allocates memory from one of the many memory regions • One region of the memory is reserved for variables that are neither created nor destroyed while your program runs---static variables, constants (final) • When a dynamic variable is created using new, Java allocates memory from something called heap that grows from top to bottom • Each time a method is called, Java allocates something called a stack frame for its variables, the stack frame is allocated from a memory region called the stack 0000 static data heap stack FFFF

  7. Use of stack heap diagram • Whenever you run a program, it is beneficial to draw a visualization in the form of a stack heap diagram. • Whenever a program creates a new object a block of memory from the heap must be allocated to store the instance variables along with some additional metadata called overhead • Whenever your program calls a method, you need to create a new stack frame by adding memory to the stack. Allocate memory enough for the local variables, and some overhead to keep track of what the program is doing. 0000 static data heap stack FFFF

  8. As an example, when Java evaluates the declaration Rational r1 = new Rational(1, 2); it allocates heap space for the new Rational object. For this example, imagine that the object is created at address 1000. • The local variable r1 is allocated in the current stack frame and is assigned the value 1000, which identifies the object. heap stack 1000 num 1004 1 r1 den FFFC 1008 1000 2 Object References • Internally, Java identifies an object by its address in memory. That address is called a reference. • The next slide traces the execution of the TestRational program that I will show you right now.

  9. public void run(){ Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = a.add(b).add(c); println(a + " + " + b + " + " + c + " = " + sum); } public Rational add(Rational r) { return new Rational( this.num * r.den + r.num * this.den , this.den * r.den ); } public Rational add(Rational r) { return new Rational( this.num * r.den + r.num * this.den , this.den * r.den ); } heap stack TestRational 1000 1000 num num 1004 1004 1 1 1/2 + 1/3 + 1/6 = 1 All objects are created in the heap. den den 1008 1008 2 2 100C 100C num num 1010 1010 1 1 den den 1014 1014 3 3 Thisobjectisatemporaryvalue used only during the calculation. 1018 1018 This stack frame is created for the add method. r num num FFE0 101C 101C 100C 1 1 1018 this den den FFE4 1020 1020 1000 6 6 1024 FFE8 1024 1024 sum sum num num FFEC FFEC 1028 1028 5 5 This stack frame is created for the run method. 1030 c c den den FFF0 FFF0 102C 102C 6 6 1018 b b FFF4 FFF4 1030 1030 100C a a num num FFF8 FFF8 1034 1034 1 1 1000 den den FFFC FFFC 1038 1038 1 1 A Complete Heap-Stack Trace public void run(){ Rational a = new Rational(1, 2); Rational b = new Rational(1, 3); Rational c = new Rational(1, 6); Rational sum = a.add(b).add(c); println(a + " + " + b + " + " + c + " = " + sum); } 5 36 2 1 3 1 36 6 heap stack TestRational 1/2 + 1/3 + 1/6 = 1 100C 1000 1030 1018 100C 1000 skip simulation

  10. The diagram at the lower right shows exactly the same state using arrows instead of numeric addresses. This style of diagram is said to use the pointer model. heap stack 1000 num num 1004 1 1 den den 1008 2 2 100C num num 1010 1 1 den den 1014 3 3 1018 num num 101C 1 1 den den 1020 6 6 1024 sum sum num num FFEC 1028 5 5 c c den den FFF0 102C 6 6 b b FFF4 1030 a a num num FFF8 1034 1 1 den den FFFC 1038 1 1 The Pointer Model • The heap-stack diagram at the lower left shows the state of memory at the end of the run method from TestRational. heap stack 1030 1018 100C 1000

  11. num 1 den 2 num 1 den 3 This object was used to hold a temporary result and is no longer accessible. num 1 den 6 num 5 den 6 num 1 den 1 Garbage Collection • One fact that the pointer model makes clear in this diagram is that there are no longer any references to the Rational value 5/6. That value has now become garbage. • From time to time, Java runs through the heap and reclaims any garbage. This process is called garbage collection. heap stack sum c b a

  12. Primitive Types vs. Objects • At first glance, Java’s rules for passing objects as arguments seem to differ from the rules Java uses with arguments that are primitive types. • When you pass an argument of a primitive type to a method, Java copies the value of the argument into the parameter variable. As a result, changes to the parameter variable have no effect on the argument. • When you pass an object as an argument, there seems to be some form of sharing going on. Although changing the parameter variable itself has no effect, any changes that you make to the instance variables inside an object—usually by calling setters—have a permanent effect on the object. • Stack-heap diagrams make the reason for this seeming asymmetry clear. When you pass an object to a method, Java copies the reference but not the object itself.

  13. Parameter Passing… • Can occur by value or reference • Apart from primitive types, everything else (objects) are passed by reference • However, you always pass a copy of the reference • Impossible to write a swap method on Objects in Java • Lets see an example to understand this…

  14. Concept of binding • Language time binding, compile time, load time, link time, or runtime • count = count + 5 • Count is bound at compile time • The possible values that count could have is language time bounded • Meaning of ‘+’ is compile time bounded • The representation of 5 is compile time bounded • The value of count after the operation is runtime bounded

More Related