Addison essential C# 4.0 Visual Studio_10
Số trang: 96
Loại file: pdf
Dung lượng: 3.23 MB
Lượt xem: 12
Lượt tải: 0
Xem trước 10 trang đầu tiên của tài liệu này:
Thông tin tài liệu:
Tham khảo tài liệu addison essential c# 4.0 visual studio_10, công nghệ thông tin, kỹ thuật lập trình phục vụ nhu cầu học tập, nghiên cứu và làm việc hiệu quả
Nội dung trích xuất từ tài liệu:
Addison essential C# 4.0 Visual Studio_10 835 P ointers and AddressesIf the data is an unmanaged variable type but is not fixed, then use thefixed statement to fix a moveable variable.Fixing DataTo retrieve the address of a moveable data item, it is necessary to fix, orpin, the data, as demonstrated in Listing 20.14.Listing 20.14: Fixed Statement byte[] bytes = new byte[24]; fixed (byte* pData = &bytes[0]) // pData = bytes also allowed { // ... } Within the code block of a fixed statement, the assigned data will notmove. In this example, bytes will remain at the same address, at least untilthe end of the fixed statement. The fixed statement requires the declaration of the pointer variablewithin its scope. This avoids accessing the variable outside the fixed state-ment, when the data is no longer fixed. However, it is the programmer’sresponsibility to ensure that he doesn’t assign the pointer to another vari-able that survives beyond the scope of the fixed statement—possibly in anAPI call, for example. Similarly, using ref or out parameters will be prob-lematic for data that will not survive beyond the method call. Since a string is an invalid referent type, it would appear invalid todefine pointers to strings. However, as in C++, internally a string is apointer to the first character of an array of characters, and it is possible todeclare pointers to characters using char*. Therefore, C# allows declar-ing a pointer of type char* and assigning it to a string within a fixedstatement. The fixed statement prevents the movement of the string dur -ing the life of the pointer. Similarly, it allows any moveable type that sup -ports an implicit conversion to a pointer of another type, given a fixedstatement. You can replace the verbose assignment of &bytes[0] with the abbrevi-ated bytes, as shown in Listing 20.15. From the Library of Wow! eBook836 C hapter 20: Platform Interoperability and Unsafe Code Listing 20.15: Fixed Statement without Address or Array Indexer byte[] bytes = new byte[24]; fixed (byte* pData = bytes) { // ... } Depending on the frequency and time to execute, fixed statements have the potential to cause fragmentation in the heap because the garbage col- lector cannot compact fixed objects. To reduce this problem, the best practice is to pin blocks early in the execution and to pin fewer large blocks rather than many small blocks. Unfortunately, this has to be tempered with pinning as little as possible for as short a time as possible, to minimize the chance that a collection will happen during the time that the data is pinned. To some extent, .NET 2.0 reduces the problem, due to some addi- tional fragmentation-aware code. Allocating on the Stack You should use the fixed statement on an array to prevent the garbage col- lector from moving the data. However, an alternative is to allocate the array on the call stack. Stack allocated data is not subject to garbage collec- tion or to the finalizer patterns that accompany it. Like referent types, the requirement is that the stackalloc data is an array of unmanaged types. For example, instead of allocating an array of bytes on the heap, you can place it onto the call stack, as shown in Listing 20.16. Listing 20.16: Allocating Data on the Call Stack byte* bytes = stackalloc byte[42];} Because the data type is an array of unmanaged types, it is possible for the runtime to allocate a fixed buffer size for the array and then to restore that buffer once the pointer goes out of scope. Specifically, it allocates sizeof(T) * E, where E is the array size and T is the referent type. Given the requirement of using stackalloc only on an array of unmanaged types, the runtime restores the buffer back to the system simply by unwinding the stack, eliminating the complexities of iterating over the f-reachable queue (see Garbage Collection and Finalization in Chapter 9) and compacting reachable data. Therefore, there is no way to explicitly free stackalloc data. From the Library of Wow! eBook 837 P ointers and Addresses Note that the stack is a precious resource and, although small, running outof stack space will result in a program crashing; every effort should be takento avoid running out. If a program does run out of stack space, the b ...
Nội dung trích xuất từ tài liệu:
Addison essential C# 4.0 Visual Studio_10 835 P ointers and AddressesIf the data is an unmanaged variable type but is not fixed, then use thefixed statement to fix a moveable variable.Fixing DataTo retrieve the address of a moveable data item, it is necessary to fix, orpin, the data, as demonstrated in Listing 20.14.Listing 20.14: Fixed Statement byte[] bytes = new byte[24]; fixed (byte* pData = &bytes[0]) // pData = bytes also allowed { // ... } Within the code block of a fixed statement, the assigned data will notmove. In this example, bytes will remain at the same address, at least untilthe end of the fixed statement. The fixed statement requires the declaration of the pointer variablewithin its scope. This avoids accessing the variable outside the fixed state-ment, when the data is no longer fixed. However, it is the programmer’sresponsibility to ensure that he doesn’t assign the pointer to another vari-able that survives beyond the scope of the fixed statement—possibly in anAPI call, for example. Similarly, using ref or out parameters will be prob-lematic for data that will not survive beyond the method call. Since a string is an invalid referent type, it would appear invalid todefine pointers to strings. However, as in C++, internally a string is apointer to the first character of an array of characters, and it is possible todeclare pointers to characters using char*. Therefore, C# allows declar-ing a pointer of type char* and assigning it to a string within a fixedstatement. The fixed statement prevents the movement of the string dur -ing the life of the pointer. Similarly, it allows any moveable type that sup -ports an implicit conversion to a pointer of another type, given a fixedstatement. You can replace the verbose assignment of &bytes[0] with the abbrevi-ated bytes, as shown in Listing 20.15. From the Library of Wow! eBook836 C hapter 20: Platform Interoperability and Unsafe Code Listing 20.15: Fixed Statement without Address or Array Indexer byte[] bytes = new byte[24]; fixed (byte* pData = bytes) { // ... } Depending on the frequency and time to execute, fixed statements have the potential to cause fragmentation in the heap because the garbage col- lector cannot compact fixed objects. To reduce this problem, the best practice is to pin blocks early in the execution and to pin fewer large blocks rather than many small blocks. Unfortunately, this has to be tempered with pinning as little as possible for as short a time as possible, to minimize the chance that a collection will happen during the time that the data is pinned. To some extent, .NET 2.0 reduces the problem, due to some addi- tional fragmentation-aware code. Allocating on the Stack You should use the fixed statement on an array to prevent the garbage col- lector from moving the data. However, an alternative is to allocate the array on the call stack. Stack allocated data is not subject to garbage collec- tion or to the finalizer patterns that accompany it. Like referent types, the requirement is that the stackalloc data is an array of unmanaged types. For example, instead of allocating an array of bytes on the heap, you can place it onto the call stack, as shown in Listing 20.16. Listing 20.16: Allocating Data on the Call Stack byte* bytes = stackalloc byte[42];} Because the data type is an array of unmanaged types, it is possible for the runtime to allocate a fixed buffer size for the array and then to restore that buffer once the pointer goes out of scope. Specifically, it allocates sizeof(T) * E, where E is the array size and T is the referent type. Given the requirement of using stackalloc only on an array of unmanaged types, the runtime restores the buffer back to the system simply by unwinding the stack, eliminating the complexities of iterating over the f-reachable queue (see Garbage Collection and Finalization in Chapter 9) and compacting reachable data. Therefore, there is no way to explicitly free stackalloc data. From the Library of Wow! eBook 837 P ointers and Addresses Note that the stack is a precious resource and, although small, running outof stack space will result in a program crashing; every effort should be takento avoid running out. If a program does run out of stack space, the b ...
Tìm kiếm theo từ khóa liên quan:
thủ thuật máy tính tài liệu công nghệ thông tin lập trình máy tính mẹo máy tính cài đặt máy tínhGợi ý tài liệu liên quan:
-
Top 10 mẹo 'đơn giản nhưng hữu ích' trong nhiếp ảnh
11 trang 314 0 0 -
Làm việc với Read Only Domain Controllers
20 trang 303 0 0 -
Thêm chức năng hữu dụng cho menu chuột phải trên Windows
4 trang 288 0 0 -
70 trang 250 1 0
-
Bài giảng Tin học lớp 11 bài 1: Giới thiệu ngôn ngữ lập trình C#
15 trang 237 0 0 -
Tổng hợp lỗi Win 8 và cách sửa
3 trang 232 0 0 -
Sửa lỗi các chức năng quan trọng của Win với ReEnable 2.0 Portable Edition
5 trang 213 0 0 -
Giáo trình Bảo trì hệ thống và cài đặt phần mềm
68 trang 207 0 0 -
UltraISO chương trình ghi đĩa, tạo ổ đĩa ảo nhỏ gọn
10 trang 203 0 0 -
Hướng dẫn sử dụng mạch nạp SP200S
31 trang 202 0 0