C # variable type (1): pointer

zhaozj2021-02-16  47

This lesson will briefly introduce the pointer and its use in C #, but this course will only involve a light knowledge of some pointers, if you are not very familiar with the pointer, and you want to use pointers in your code We suggest you understand it more deeper. Fortunately, the pointer is only needed in C # only when the program is running extremely important. (In most cases, we can do not pay attention to the pointer.)

Pointer symbol

The pointer is a variable that saves other types of data storage addresses. In C #, the pointer can only point value type data and arrays.

The pointer uses * implicit definitions in the following manner:

INT * P;

Some of the programmers may put * directly on the type name, for example: int * p, which is identical to the mode of work in the previous manner.

The above definition will create a pointer P. P The initial memory address of an integer (occupied 4 bytes) is saved.

* p (plus * prefix before P) This composite syntax element can be used to represent the actual data stored in memory from P. Therefore, if the declaration is given, * p can appear on the left side of the assignment number, as shown below:

* p = 5;

This line code assigns 5 assigns an integer initialized by the above declaration statement. However, you may be confused for the following assignment statements:

P = 5;

The effect of this statement is to change the P-saved memory address, which does not change the integer value initialized by the initialization statement statement. This only means that P will no longer point to this integer. In fact, P will now point to the continuous 4 bytes starting by the memory address 5 (the number of memory bit occupied by the INT type).

Another important symbol using a pointer is the address operator &, which returns the memory address stored in the variable. Below is an example of using this symbol, this example creates a pointer P:

INT i = 5; int * p; p = & i;

On the basis of the code above, the code

* p = 10;

The value of the variable I will be changed to 10. Because * P can be understood to be integrated in memory from P.

There is another important symbol to point to the pointer to the structural type ->. We can declare a pointer to the structural type, as shown below: (Use the 'Coords' structure in the following example, the rear code is also used)

Coords x = new coords (); coords * y = & x;

Then we can use a defined pointer Y to access one public member (eg Z). I can use the following two ways:

(* y) .z;

Y -> x; // Use the symbol ->

Unsafe code

One of the main issues of using pointers in C # is C # Manage environmental garbage collection. When cleaning memory space, the garbage collector may change a stored location without warning. At this time, the pointer to this type will not point to it again. This will lead to two potential problems. First, it may endanger the operation of this C # program; two, it may affect the integrity of other programs.

For the above problem, the C # restriction pointer can only be used in code that is explicitly declared by the programmer as 'unsafe'. Because unsafe code is potentially maliciously using problems, programs that contain unsafe codes can only be run when they can be fully trust.

For the problem of moving data storage locations for the garbage collector, you can declare a pointer using the 'Fixed' expression. This will fix the position of the data pointed to by the pointer, so that the storage location of this data will remain fixed, not affected by the garbage collector. However, 'Fixed' statements can only be used in unsafe code.

It is also worth noting that any value type that is declared in an insecure code is automatically changed to 'Fixed', and if you use the 'Fixed' expression to use it, it will generate runtime errors. However, this rule does not apply to the reference type (array). The following code gives an example of a method labeled 'unsafe'. From the previous story, we know that the pointer P on the 9th line cannot be declared using the 'Fixed' statement, because P pointing is an unsafe code. The structural type C declared.

1.

Using system;

2.

Public struct coords

3.

{

4.

INT X;

5.

Int Y;

6.

Unsafe public static void main ()

7.

{

8.

Coords c = new coords ();

9.

Coords * p = & c;

10.

{

11.

P-> y = 6;

12.

(* p) .x = 5;

13.

}

14.

Console.writeLine (C.Y);

15.

Console.writeLine (C.x);

16.

}

17.

}

Let's take a look at the code below, the pointer P on the 8th line must use the 'fixed' statement declaration because it points to a type not declared in the unsafe code segment.

1.

Using system;

2.

Public struct coords

3.

{

4.

INT X;

5.

Int Y;

6.

Unsafe Public Static Void Notmain (Ref Coords C)

7.

{

8.

Fixed (CoRDS * P = & C)

9.

{

10.

P-> y = 6;

11.

(* p) .x = 5;

12.

}

13.

Console.writeLine (C.Y);

14.

Console.writeLine (C.x);

15.

}

16.

}

In the examples given above, 'unsafe' is a modifier of the method, but you can also use it in a code segment, the following example shows this usage:

1.

Using system;

2.

Public static void main ()

3.

{

4.

Unsafe

5.

{

6.

Coords c = new coords ();

7.

[...]

8.

}

9.

}

Pointers, methods and arrays

Although the pointers we use in the above story are pointing to a value type, but pointers can also point to arrays (reference types). (There are also some authors think that the pointer can also point to strings)

The pointer can be declared as the following example to point to an array:

Int [] a = {4, 5}; int * b = a;

In this case, the Save the memory address saved in the array A. The position of the first element of the array A. As mentioned earlier, this element must be a value type. The following code shows how to use a pointer to change the value of each element of the array, the interpretation of this is not in this paper discussed the scope.

1.

Using system;

2.

Public Class Tester

3.

{

4.

Public static void main ()

5.

{

6.

Int [] a = {4, 5};

7.

Changeval (a);

8.

Console.writeLine (a [0]);

9.

Console.WriteLine (a [1]);

10.

}

11.

12.

Public Unsafe Static Void ChangeVal (int [] a)

13.

{

14.

FIXED (INT * B = a) 15.

{

16.

* b = 5;

17.

* (B 1) = 7;

18.

}

19.

}

20.

}

转载请注明原文地址:https://www.9cbs.com/read-23443.html

New Post(0)