DOC PREVIEW
Stanford CS 106B - Lecture Notes

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 #24CS 106B April 22, 2009Memory and C++Data Types in C++Before we can talk about objects in C++, it is useful to reviewthe 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 thisquarter, because the Vector class is so much better.• From the client perspective, an array is like a brain-damagedform 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 mustpass 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 nis a constant integer expression indicating the length.A Simple Array Example012345678942 18 89 57 91 14 59 32 16 28const 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 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 wordmay vary 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.– 2 –Octal and Hexadecimal Notation• Because binary notation tends to get rather long, computerscientists often prefer octal (base 8) or hexadecimal (base16) 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.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.• Memory diagrams that show individual bytes arenot as useful as those that are organized into words.The revised diagram on the right now includes fourbytes in each of the memory cells, which means thatthe address numbers increase by four each time.• In these slides, addresses are four-digit hexadecimalnumbers, which makes them easy to recognize.000000040008000C001000140018001C002000240028002CFFD0FFD4FFD8FFDCFFE0FFE4FFE8FFECFFF0FFF4FFF8FFFC...• When you create memory diagrams, you don’tknow the actual memory addresses at which valuesare stored, but you do know that everything has anaddress. Just make something up.• When you create memory diagrams, you don’tknow the actual memory addresses at which valuesare stored, but you do know that everything has anaddress.Sizes of the Fundamental Types• Enumerated types are typically assigned the space of an int.• Structure types have a size equal to the sum of their fields.• Arrays take up the element size times the number of elements.• Pointers take up the space needed to hold an address, which is4 bytes on a 32-bit machine and 8 bytes on a 64-bit machine.• The expression sizeof(t) returns the size of the type t.• The memory space required to represent a value depends onthe type of value. Although the C++ standard actually allowscompilers some flexibility, the following sizes are typical:1 byte(8 bits)2 bytes(16 bits)4 bytes(32 bits)8 bytes(64 bits)16 bytes(128 bits)shortcharboolintfloatlongdoublelong doublePointers• In C++, every value is stored somewhere in memory and cantherefore be identified with that address. Such addresses arecalled pointers.• Because C++ is designed to allow programmers to controldata at the lowest level, pointers can be manipulated just likeany other kind of data. In particularly, you can assign onepointer value to


View Full Document

Stanford CS 106B - Lecture Notes

Download Lecture Notes
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 Lecture Notes 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 Lecture Notes 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?