Pointer-C #'s pointer unsafe Field

Source: Internet
Author: User

Pointers can be said to be unique in C and C ++. For security reasons, the use of pointers in C # released by Microsoft is canceled. However, C ++ code can still be written in the keyword unsafe in C. Because unsafe is at the cost of Time and Space consumption. Therefore, it is not often used. Its main purpose is to use the unsafe keyword when dealing with a large number of arrays and having a large number of addresses changing. Not long ago, I was discussing a problem with my friends. The following Code describes the problem:

Code
Using System;

Using System. Collections. Generic;

Using System... Linq;

Using System... Text;

 

Namespace ConsoleApplication7

{

Class Program

{

Static void Main (string [] args)

{

Int32 I = 0;

Console. WriteLine ("Before unsafe code, I = {0}", I );

Long l = 0;

Unsafe // Seg 1

{

L = (long) & I;

L ++;

Char * ptr = (char *) l;

* Ptr = 'a ';

}

Console. WriteLine ("After unsafe code seg 1, I = {0}", I );

Unsafe // 2

{

Char * ptr = (char *) l;

Console. WriteLine ("Inside unsafe code seg 2, * ptr = {0}", * ptr );

}

Console. WriteLine ("QUESTION: There is no code to modify variable I directly, why the value of I is changed to 24832? ");

}

}

}
 

OUTPUT:

Before unsafe code, I = 0

After unsafe code seg 1, I = 24832

Inside unsafe code seg 2, * ptr =

QUESTION: There is no code to modify variable I directly, why the value of I is changed to 24832?

Press any key to continue...
 

 

The output result is 24832. What's going on? My personal analysis is as follows:

Int32 I = 0; Console. WriteLine ("Before unsafe code, I = {0}", I );
Long l = 0;
Unsafe // Seg 1
{
L = (long) & I;
L ++;
Char * ptr = (char *) l;
* Ptr = 'a ';
}
High 2 High 1 Low 2 Low 1  
00 00 00 00 <= I value (0) in hexadecimal notation, l points to 1 lower 0
00 00 00 00 <= Execute l ++, and l points to the lower two
00 00 00 00 <= Execute char * ptr = (char *) l; ptr also points to 2 lower bits
00 00 A 00 <= Execute * ptr = 'a'; assign a low 2-bit value to
00 00 61 00 <= 'A' The hexadecimal value of the ASCLL code is 61.
24832 <= Convert I to 10 and output 24832
         
         

In fact, we mainly understand the usage of pointers. The pointer is the address. The pointer is the address that points to the address. A reference is an alias. Knowing this is a very good solution.

If you have other good understandings or answers, you can share them with us.

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.