DOC PREVIEW
Stanford CS 106A - Objects and Memory

This preview shows page 1 out of 3 pages.

Save
View full document
View full document
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience
Premium Document
Do you want full access? Go Premium and unlock all 3 pages.
Access to all documents
Download any document
Ad free experience

Unformatted text preview:

Eric Roberts Handout #36CS 106A February 5, 2010Objects and MemoryObjects and MemoryEric RobertsCS 106AFebruary 5, 2010Claude ShannonClaude Shannon was one of the pioneerswho shaped computer science in its earlyyears. In his master’s thesis, Shannonshowed how it was possible to useBoolean logic and switching circuits toperform arithmetic calculations. That workled directly to the use of binary arithmeticinside computers—an idea that initiallygenerated considerable resistance amongmore established scientists.Shannon is best known as the inventor ofinformation theory, which provides themathematical foundation for understandingdata communication. To many, however,Shannon’s great contribution was that henever lost his sense of fun throughout hislong career.Claude Shannon (1916-2001)The Structure of Memory• The fundamental unit of memory inside a computer is called abit, which is a contraction of the words binary digit. A bitcan be in either of two states, usually denoted as 0 and 1.• Numbers are stored in still larger units that consist of multiplebytes. The unit that represents the most common integer sizeon a particular hardware is called a word. Because machineshave different architectures, the number of bytes in a wordvaries from machine to machine.0 0 1 0 1 0 1 0• The hardware structure of a computer combines individualbits into larger units. In most modern architectures, thesmallest unit on which the hardware operates is a sequence ofeight consecutive bits called a byte. The following diagramshows a byte containing a combination of 0s and 1s:Binary Notation• Bytes and words can be used to represent integers of differentsizes by interpreting the bits as a number in binary notation.0 x=011 x=220 x=04421 x=880 x=0161 x=32320 x=0640 x=0128• Binary notation is similar to decimal notation but uses adifferent base. Decimal numbers use 10 as their base, whichmeans that each digit counts for ten times as much as the digitto its right. Binary notation uses base 2, which means thateach position counts for twice as much, as follows:0 0 1 0 1 0 1 0Numbers and Bases• The calculation at the end of the preceding slide makes itclear that the binary representation 00101010 is equivalent tothe number 42. When it is important to distinguish the base,the text uses a small subscript, like this:001010102 = 4210• Although it is useful to be able to convert a number from onebase to another, it is important to remember that the numberremains the same. What changes is how you write it down.• The number 42 is what you get if you counthow many stars are in the pattern at the right.The number is the same whether you write itin English as forty-two, in decimal as 42, orin binary as 00101010.• Numbers do not have bases; representations do.Octal and Hexadecimal Notation• Because binary notation tends to get rather long, computerscientists often prefer octal (base 8) or hexadecimal (base 16)notation instead. Octal notation uses eight digits: 0 to 7.Hexadecimal notation uses sixteen digits: 0 to 9, followed bythe letters A through F to indicate the values 10 to 15.• The following diagrams show how the number forty-twoappears in both octal and hexadecimal notation:2 x=215 x=4085 24210 x=10102 x=32162 A42octal hexadecimal• The advantage of using either octal or hexadecimal notation isthat doing so makes it easy to translate the number back toindividual bits because you can convert each digit separately.– 2 –Exercises: Number Bases• What is the decimal value for each of the following numbers?1000121778AD16• As part of a code to identify the file type, every Java class filebegins with the following sixteen bits:1 1 0 0 1 0 1 0 1 1 1 1 1 1 1 0How would you express that number in hexadecimal notation?Memory and Addresses• Every byte inside the primary memory of a machineis identified by a numeric address. The addressesbegin at 0 and extend up to the number of bytes inthe machine, as shown in the diagram on the right.• In these slides as well as in the diagrams in the text,memory addresses appear as four-digit hexadecimalnumbers, which makes addresses easy to recognize.• In Java, it is impossible to determine the address ofan object. Memory addresses used in the examplesare therefore chosen completely arbitrarily.• Memory diagrams that show individual bytes arenot as useful as those that are organized into words.The diagram on the right includes four bytes in eachof the memory cells, which means that the addressnumbers increase by four each time.000000040008000C001000140018001C002000240028002CFFD0FFD4FFD8FFDCFFE0FFE4FFE8FFECFFF0FFF4FFF8FFFC...The Allocation of Memory to Variables• When you declare a variable in a program, Java allocatesspace for that variable from one of several memory regions.• One region of memory is reserved for variables thatare never created or destroyed as the program runs,such as named constants and other class variables.This information is called static data.staticdata0000stackFFFF• Each time you call a method, Java allocates a newblock of memory called a stack frame to hold itslocal variables. These stack frames come from aregion of memory called the stack.• Whenever you create a new object, Java allocatesspace from a pool of memory called the heap.heap• In classical architectures, the stack and heap growtoward each other to maximize the available space.Heap-Stack Diagrams• It is easier to understand how Java works if you have a goodmental model of its use of memory. The text illustrates thismodel using heap-stack diagrams, which show the heap onthe left and the stack on the right, separated by a dotted line.• Whenever your program creates a new object, you need toadd a block of memory to the heap side of the diagram. Thatblock must be large enough to store the instance variables forthe object, along with some extra space, called overhead, thatis required for any object. Overhead space is indicated inheap-stack diagrams as a crosshatched box.• Whenever your program calls a method, you need to create anew stack frame by adding a block of memory to the stackside. For method calls, you need to add enough space to storethe local variables for the method, again with some overheadinformation that tracks what the program is doing. When amethod returns, Java reclaims the memory in its frame.Object References• Internally, Java identifies an object by its address in memory.That address is called a


View Full Document

Stanford CS 106A - Objects and Memory

Download Objects and Memory
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Objects and Memory and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Objects and Memory 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?