DOC PREVIEW
Stanford CS 106B - Memory And C

This preview shows page 1-2-20-21 out of 21 pages.

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

Unformatted text preview:

Memory and C++Data Types in C++Simple Arrays in C++A Simple Array ExampleThe Structure of MemoryBinary NotationNumbers and BasesOctal and Hexadecimal NotationExercises: Number BasesMemory and AddressesSizes of the Fundamental TypesPointersDeclaring a Pointer VariablePointer OperatorsPointers and ArraysThe Allocation of Memory to VariablesDynamic AllocationHeap-Stack DiagramsExercise: Heap-Stack DiagramsSlide 20The EndMemory and C++Eric RobertsCS 106BApril 22, 2009Data Types in C++Before we can talk about objects in C++, it is useful to review the more traditional data types that C++ inherits from C:•Enumerated types defined using the enum keyword•Structure types defined using the struct keyword•Arrays of some base type•Pointers to a target type•Atomic types:–short, int, long, and their unsigned variants–float, double, and long double–char–boolSimple Arrays in C++•We haven’t actually used arrays in their low-level form this quarter, because the Vector class is so much better.•From the client perspective, an array is like a brain-damaged form of Vector with the following differences:–The only operation is selection using []–Array selection does not check that the index is in range–The length of an array is fixed at the time it is created–Arrays don’t store their length, so programs that use them must pass an extra integer value that represents the effective size•Array variables are declared using the following syntax: type name[n];where type is the element type, name is the array name, and n is a constant integer expression indicating the length.A Simple Array Example 0 1 2 3 4 5 6 7 8 942 18 89 57 91 14 59 32 16 28skip simulationconst int N = 10;int main() { int array[N]; for ( int i = 0 ; i < N ; i++ ) { array[i] = RandomInteger(0, 99); } Sort(array, N);}array012345678910iThe Structure of Memory•The fundamental unit of memory inside a computer is called a bit, which is a contraction of the words binary digit. A bit can be in either of two states, usually denoted as 0 and 1.•Numbers are stored in still larger units that consist of multiple bytes. The unit that represents the most common integer size on a particular hardware is called a word. Because machines have different architectures, the number of bytes in a word may vary from machine to machine.0 0 1 0 1 0 1 0•The hardware structure of a computer combines individual bits into larger units. In most modern architectures, the smallest unit on which the hardware operates is a sequence of eight consecutive bits called a byte. The following diagram shows a byte containing a combination of 0s and 1s:Binary NotationThe rightmost digitis the units place.The next digit givesthe number of 2s.The next digit givesthe number of 4s.And so on . . .•Bytes and words can be used to represent integers of different sizes 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 a different base. Decimal numbers use 10 as their base, which means that each digit counts for ten times as much as the digit to its right. Binary notation uses base 2, which means that each 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 it clear that the binary representation 00101010 is equivalent to the 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 one base to another, it is important to remember that the number remains the same. What changes is how you write it down. •The number 42 is what you get if you count how many stars are in the pattern at the right. The number is the same whether you write it in English as forty-two, in decimal as 42, or in binary as 00101010.•Numbers do not have bases; representations do.Octal and Hexadecimal Notation•Because binary notation tends to get rather long, computer scientists 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 by the letters A through F to indicate the values 10 to 15.•The following diagrams show how the number forty-two appears 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 is that doing so makes it easy to translate the number back to individual bits because you can convert each digit separately.Exercises: Number Bases•What is the decimal value for each of the following numbers?1000121778AD161 x= 110 x= 020 x= 040 x= 081 x= 1161 0 0 0 1177 x= 717 x= 5687 712711 x= 646413 x= 13110 x= 16016A D17317 127 173•As part of a code to identify the file type, every Java class file begins 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?1 1 0 0 1 0 1 0 1 1 1 1 1 1 1 0A F ECAFE161 1 0 0 1 0 1 0 1 1 1 1 1 1 1 0Memory and Addresses•Every byte inside the primary memory of a machine is identified by a numeric address. The addresses begin at 0 and extend up to the number of bytes in the machine, as shown in the diagram on the right.•Memory diagrams that show individual bytes are not as useful as those that are organized into words. The revised diagram on the right now includes four bytes in each of the memory cells, which means that the address numbers increase by four each time.•In these slides, addresses are four-digit hexadecimal numbers, which makes them easy to recognize.0000000100020003000400050006000700080009000A000BFFF4FFF5FFF6FFF7FFF8FFF9FFFAFFFBFFFCFFFDFFFEFFFF000000040008000C001000140018001C002000240028002CFFD0FFD4FFD8FFDCFFE0FFE4FFE8FFECFFF0FFF4FFF8FFFC......•When you create memory diagrams, you don’t know the actual memory addresses at which values are stored, but you do know that everything has an address. Just make something up.•When you create memory diagrams, you don’t know the actual memory addresses at which values are stored, but you do know that everything has an address.Sizes of the Fundamental Types•Enumerated types


View Full Document

Stanford CS 106B - Memory And C

Download Memory And C
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 Memory And C 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 Memory And C 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?