C # key Knowledge (b)

Source: Internet
Author: User
Tags empty
Detailed chapter II Memory management

C # Memory management provides the same automatic memory management capabilities as Java, allowing programmers to get rid of heavy memory management, which improves the quality of code and improves development efficiency.

C # limits the use of pointers and eliminates the annoyance of programmers to memory leaks, but does not mean that C # programmers, like Java programmers, cannot use the benefits of pointer generation. Microsoft has taken this into account when designing the C # language, while discarding pointers on the one hand, and using a compromise approach to introduce pointers through a logo.

First, let's learn about automatic memory management.

public class Stack
{
Private Node-i = null;

public bool Empty {
get {
return (a = null);
}
}

public Object Pop () {
if (i = null)
throw new Exception ("Can" t Pop from a empty Stack. ");
else {
Object temp = i. Value;
A. Next;
return temp;
}
}

public void Push (object o) {
The new Node (O, a)
}

Class Node
{
Public Node Next;

public object Value;

Public Node (object value): This (value, null) {}

Public node (object value, Node next) {
Next = next;
Value = value;
}
}
}

The program creates a stack class to implement a chain, using a push method to create the node node instance and a collector when the node node is no longer needed. When a node instance cannot be accessed by any code, it is collected. For example, when a point element is moved out of the stack, the associated node is collected.

The example

Class Test
{
static void Main () {
Stack s = new stack ();

for (int i = 0; i < i++)
S.push (i);

s = null;
}
}





For reference to pointers, use the unsafe flag in C # to refer to the team pointer. The following program demonstrates the use of pointers, but because of the use of pointers, memory management has to be done manually.

Using System;

Class Test
{
unsafe static void locations (byte[] ar) {
Fixed (byte *p = ar) {
byte *p_elem = p;
for (int i = 0; i < ar. Length; i++) {
byte value = *p_elem;
string addr = Int. Format ((int) P_elem, "X");
Console.WriteLine ("Arr[{0}] at 0x{1} is {2}", I, addr, value);
p_elem++;
}
}
}

static void Main () {
byte[] arr = new byte[] {1, 2, 3, 4, 5};
Writelocations (AR);
}
}




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.