1. 2 Automated memory Management (Automatic memory management)
Manual management of memory requires the programmer to allocate and free chunks of memory. This requires the programmer to have a clear mind and a very
Grasp (very difficult!) )。 and C # Frees programmers from this difficult task. In most cases, this automatic memory management reference
High code quality and programmer productivity. And it will not affect the format of the program's intent and execution. I don't believe in M$ 's ghost.
words). However, it's probably a bit better than the Java Recycle Bin. Because C # debut Shama (do nonsense). Okay, let's take a look at the examples. */
Using System;
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;
}
}
}
Class Test
{
static void Main () {
Stack s = new stack ();
for (int i = 0; i < i++)
S.push (i);
while (!s.empty)
Console.WriteLine (S.pop ());
}
}
/*
The Stack class implements an instance of a series of node. You can look at the push method of the Stack class. The instance of node is created in the push method.
Is "The new Node (O, a)." Please remember this "new" Oh. It is used to create a class instance. The relevant syntax is too
More, walk to the back with a section in detail. Just to understand the benefits of automatic memory management (Automatic memory management)?! "New"
is in charge of initializing class instances. Releasing these instances in C + + will use another keyword, "delete". But when will you use the delete?
This is usually a very demanding job, and a veteran will capsize in the gutter. Besides, it's me! But there's no use in C #. In the example, "delete" is not used.
When the instance of node is not needed, the garbage collector (garbage collector) automatically destroys it, so don't worry about it. This to and Java pretty
Like (may be copied).
In a test class, I used a loop to assign a value of 10 times to the push method of an instance of a stack class. So, push creates node's 10 real
Example (instance). Then use pop to show them. The order is exactly the opposite of the order in which they were created.
This example is pretty good, it's stack.
A typical, but also a good description of the mechanism of automatic memory management. But it is not good to understand that this section is not written to the user without foundation.
I have spent a few minutes to understand, you prawns are no problem.
In fact, when the "10" is displayed, a node instance conforms to the released condition, but the garbage collector does not necessarily do so.
In other words, its behavior is not certain (this is the same as Java, I guess). Sometimes, this behavior can have some negative effects. At least it's sex.
can reduce. Automatic memory management itself is also problematic. Because it is difficult to manage some special cases. There are some garbage collectors about Java.
The article is also mentioned. M$ is not going to get much better. So, m$ has a term for unsafe code (unsafe codes) that is used to service advanced users
Works That is, the user may not adopt a garbage collector. However, it must be explicitly declared with the "unsafe" keyword. This avoids the user inadvertently to
Use unsafe code outside. Here is an example: * *
Using System;
Class Test
{
unsafe static void Writelocations (byte[] arr) {
Fixed (byte *p_arr = arr) {
byte *p_elem = P_arr;
for (int i = 0; i < arr. 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 (arr);
}
}
/*
I am not very satisfied with this example, also let me a bit confused, have the opportunity to write one by myself. It's simple, just use the pointer! Hail!
In fact, I am not sure about this section! There are a lot of places can not be justified! So, please make great criticisms from prawns. */
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