Web Sever Memory Format pps

7 131 0
Web Sever Memory Format pps

Đang tải... (xem toàn văn)

Thông tin tài liệu

Identifying Keywords The C# language reserves 77 identifiers for its own use, and you should not reuse these identifiers for your own purposes. These identifiers are called keywords, and each has a particular meaning. Examples of keywords are class, namespace, and using. You'll learn the meaning of most of the keywords as you proceed through this book. The keywords are listed in the following table. abstract as base bool break byte case catch char checked class const continue decimal default delegate do double else enum event explicit extern false finally fixed float for foreach goto if implicit in int interface internal is lock long namespace new null object operator out override params private protected public readonly ref return sbyte sealed short sizeof stackalloc static string struct switch this throw true try typeof uint ulong unchecked unsafe ushort using virtual void volatile while TIP In the Visual Studio 2005 Code and Text Editor window, keywords are colored blue when you type them. How Computer Memory is Organized Computers use memory to hold programs being executed, and the data that these programs use. In order to understand the differences between value and reference types, it is helpful to understand how data is organized in memory. Operating systems and runtimes (such as the common language runtime) frequently divide the memory used for holding data into two separate chunks, each of which is managed in a distinct manner. These two chunks of memory are traditionally called the stack and the heap. The stack and the heap serve very different purposes: • When you call a method, the memory required for its parameters and its local variables is always acquired from the stack. When the method finishes (because it either returns or throws an exception), the memory acquired for the parameters and local variables is automatically released back to the stack and is available for reuse when another method is called. • When you create an object (an instance of a class) by using the new keyword and a constructor call, the memory required to build the object is always acquired from the heap. You have seen that the same object can be referenced from several places by using reference variables. When the last reference to an object disappears, the memory used by the object becomes available for reuse (although it might not be reclaimed immediately). Chapter 13 includes a more detailed discussion of how heap memory is reclaimed. NOTE All value types are created on the stack. All reference types (objects) are created on the heap. The names stack and heap come from the way in which the runtime organizes memory: • Stack memory is organized like a stack of boxes piled on top of each other. When a method is called, each parameter is put in a box which is placed on top of the stack. Each local variable is likewise assigned a box, and these are placed on top of the boxes already on the stack. When a method finishes, all its boxes are removed from the stack. • Heap memory is like a large pile of boxes strewn around a room rather than stacked neatly on top of each other. Each box has a label indicating whether it is in use or not. When a new object is created, the runtime searches for an empty box and allocates it to the object. The reference to the object is stored in a local variable on the stack. The run-time keeps track of the number of references to each box (remember that two variables can refer to the same object). When the last reference disappears, the runtime marks the box as not in use, and at some point in the future will empty the box and make it available for reuse. Using the Stack and the Heap Now let's examine what happens when the following Method is called: void Method(int param) { Circle c; c = new Circle(param); } Suppose the value passed into param is the value 42. A piece of memory (just enough for an int) is allocated from the stack, and initialized with the value 42. Inside the method, another piece of memory big enough to hold a reference is also allocated from the stack, but left uninitialized (this is for the Circle variable, c). Next, another piece of memory big enough for a Circle object is allocated from the heap. This is what the new keyword does. The Circle constructor runs to convert this raw heap memory into a Circle object. A reference to this Circle object is stored in the variable c. The following graphic illustrates the situation: At this point, you should note two things: 1. Although the object itself is stored on the heap, the reference to the object (variable c) is stored on the stack. 2. Heap memory is not infinite. If heap memory is exhausted, the new operator will throw an OutOfMemoryException and the object will not be created. NOTE The Circle constructor could also throw an exception. If it does, the memory allocated to the Circle object will be reclaimed and the value returned by the constructor will be a null reference. When the function ends, the parameters and local variables go out of scope. The memory acquired for c and the memory acquired for param is automatically released back to the stack. The runtime notes that the Circle object is no longer referenced, and at some point in the future will arrange for its memory to be reclaimed by the heap (see Chapter 13). Implementing an Operator In the following exercise, you will complete another Microsoft Windows digital clock application. This version of the code is similar to the exercise in Chapter 16, “Delegates and Events.” However, in this version, the delegate method (which is called every second) does not receive the current hour, minute, and second values when the event is raised. Instead, the delegate method keeps track of the time itself by updating three fields, one each for the hour, minute, and second values. The type of these three fields is Hour, Minute, and Second, respectively, and they are all structs. However, the application will not yet compile because the Minute struct is not finished. In the first exercise, you will finish the Minute struct by implementing its missing addition operators. Write the operator+ overloads 1. Start Microsoft Visual Studio 2005 if it is not already running. 2. Open the Operators project, located in the \Microsoft Press\Visual CSharp Step by Step\Chapter 19\Operators folder in your My Documents folder. 3. In the Code and Text Editor window, open the Clock.cs source file and locate the declarations of the hour, minute, and second fields at the end of the class. These fields hold the clock's current time: class Clock { private Hour hour; private Minute minute; private Second second; } 4. Locate the tock method of the Clock class. This method is called every second to update the hour, minute, and seconds variables. The tock method looks like this: private void tock() { this.second++; if (this.second == 0) { this.minute++; if (this.minute == 0) { this.hour++; } } } The constructors for the Clock class contain the following statement that arranges for this method to be called whenever the tick event of the pulsed field is raised (the pulsed field is a Ticker, that uses a Timer object to generate an event every second, as described in the exercises in Chapter 16. this.pulsed.tick += tock; 5. On the Build menu, click Build Solution. The project builds but displays the following error message in the Output pane: Operator '==' cannot be applied to operands of type 'Operators.Minute' and 'int'. The problem is that the tock method contains the following if statement, but the appropriate operator== is not declared in the Minute struct: if (minute == 0) { hour++; } Your next task is to implement this operator for the Minute struct. 6. In the Code and Text Editor window, open the Minute.cs file. 7. In the Code and Text Editor window, implement a version of operator== that accepts a Minute as its left-hand operand and an int as its right-hand operand. Don't forget that the return type of this operator should be a bool. The completed operator should look exactly as shown in the following class: struct Minute { public static bool operator== (Minute lhs, int rhs) { return lhs.value == rhs; } private int value; } 8. On the Build menu, click Build Solution. The project builds but displays the following error message in the Output pane: The operator 'Operators.Minute.operator ==(Operators.Minute, int)' requires a mat ching operator "!=" to also be defined. There is a still an error. The problem is that you have implemented a version of operator== but have not implemented its required operator!= partner. 9. Implement a version of operator!= that accepts a Minute as its left-hand operand and an int as its right-hand operand. The completed operator should look exactly like this: struct Minute { public static bool operator!= (Minute lhs, int rhs) { return lhs.value != rhs; } private int value; } 10. On the Build menu, click Build Solution. This time, the application builds without errors. 11. On the Debug menu, click Start Without Debugging. The application runs and displays a digital clock that updates itself every second. 12. Close the application and return to the Visual Studio 2005 programming environment. . object (variable c) is stored on the stack. 2. Heap memory is not infinite. If heap memory is exhausted, the new operator will throw an OutOfMemoryException and the object will not be created helpful to understand how data is organized in memory. Operating systems and runtimes (such as the common language runtime) frequently divide the memory used for holding data into two separate. manner. These two chunks of memory are traditionally called the stack and the heap. The stack and the heap serve very different purposes: • When you call a method, the memory required for its parameters

Ngày đăng: 01/07/2014, 09:20

Từ khóa liên quan

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan