C # advanced programming learning notes (4) memory management and pointers

Source: Internet
Author: User

VII. Memory Management and pointers

1. Internal background management
2. Release unmanaged Resources
When defining a category, you can use two mechanisms to automatically release resources:
Declare An destructor (or Terminator) as a member of the class.
Implement the system. idisposable interface in the class
Destructor: similar to a method ~. No return type, no parameters, no access modifier.
C # When the compiler compiles the destructor,CodeCompile the code corresponding to the finalize () method to ensure that the finalize () method of the parent class is executed.
C # The execution of destructor delays the time when the object is finally deleted from the memory. Because the objects without destructor will be deleted from the memory in one processing of the garbage collector, but the objects with destructor need to be processed twice to be deleted: objects are not deleted when the Destructor is called for the first time. Can refer to the introduction of C # garbage collection: Secondary recovery mechanism, destructor, dispose, and finalize Etc .. (http://www.cnblogs.com/sban/archive/2007/12/02/978737.html)
Idisposable interface: in C #, The system. idisposable interface is recommended to replace the destructor.
3. Insecure code
C # Only the Unsafe code can use pointers.
In C #, the * symbol is related to the type, and in C ++, * is related to the variable name. For example, int * PX and Py correspond to int * PX and * py in C ++.
& Indicates "Get address" and converts a value type to a pointer. For example, int is converted to * Int. This operator is called an addressing operator.
* Indicates "Get address content" and converts a pointer to a value data type (for example, * float is converted to float ). this operator is called "indirect addressing operator" (sometimes called "unreferencing operator ")

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.