Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
C-sharp language specification.2004.pdf
Скачиваний:
14
Добавлен:
23.08.2013
Размер:
2.55 Mб
Скачать

Chapter 25 Unsafe code

1code to take the address of a local variable, value parameter, or parameter array which has been captured by

2an anonymous method.

325.5.5 Pointer increment and decrement

4In an unsafe context, the ++ and -- operators (§14.5.9 and §14.6.5) can be applied to pointer variables of all

5types except void*. Thus, for every pointer type T*, the following operators are implicitly defined:

6T* operator ++(T* x);

7T* operator --(T* x);

8The operators produce the same results as x+1 and x-1, respectively (§25.5.6). In other words, for a pointer

9variable of type T*, the ++ operator adds sizeof(T) to the address contained in the variable, and the

10-- operator subtracts sizeof(T) from the address contained in the variable.

11If a pointer increment or decrement operation overflows the domain of the pointer type, the result is

12implementation-defined, but no exceptions are produced.

1325.5.6 Pointer arithmetic

14In an unsafe context, the + operator (§14.7.4) and operator (§14.7.5) can be applied to values of all

15pointer types except void*. Thus, for every pointer type T*, the following operators are implicitly defined:

16T* operator +(T* x, int y);

17T* operator +(T* x, uint y);

18T* operator +(T* x, long y);

19T* operator +(T* x, ulong y);

20T* operator +(int x, T* y);

21T* operator +(uint x, T* y);

22T* operator +(long x, T* y);

23T* operator +(ulong x, T* y);

24T* operator –(T* x, int y);

25T* operator –(T* x, uint y);

26T* operator –(T* x, long y);

27T* operator –(T* x, ulong y);

28long operator –(T* x, T* y);

29Given an expression P of a pointer type T* and an expression N of type int, uint, long, or ulong, the

30

expressions P + N and N + P compute the pointer value of type T* that results from adding

31

N * sizeof(T) to the address given by P. Likewise, the expression P – N computes the pointer value of

32type T* that results from subtracting N * sizeof(T) from the address given by P.

33Given two expressions, P and Q, of a pointer type T*, the expression P – Q computes the difference

34between the addresses given by P and Q and then divides that difference by sizeof(T). The type of the

35result is always long. In effect, P - Q is computed as ((long)(P) - (long)(Q)) / sizeof(T).

36[Example:

37using System;

38class Test

39{

40

static void Main() {

41

unsafe {

42

int* values = stackalloc int[20];

43

int* p = &values[1];

44

int* q = &values[15];

45

Console.WriteLine("p - q = {0}", p - q);

46

Console.WriteLine("q - p = {0}", q - p);

47

}

48}

49}

50which produces the output:

389

C# LANGUAGE SPECIFICATION

1p - q = -14

2q - p = 14

3end example]

4If a pointer arithmetic operation overflows the domain of the pointer type, the result is truncated in an

5implementation-defined fashion, but no exceptions are produced.

625.5.7 Pointer comparison

7In an unsafe context, the ==, !=, <, >, <=, and => operators (§14.9) can be applied to values of all pointer

8types. The pointer comparison operators are:

9bool operator ==(void* x, void* y);

10bool operator !=(void* x, void* y);

11bool operator <(void* x, void* y);

12bool operator >(void* x, void* y);

13bool operator <=(void* x, void* y);

14bool operator >=(void* x, void* y);

15Because an implicit conversion exists from any pointer type to the void* type, operands of any pointer type

16can be compared using these operators. The comparison operators compare the addresses given by the two

17operands as if they were unsigned integers.

1825.5.8 The sizeof operator

19The sizeof operator returns the number of 8-bit bytes occupied by a variable of a given type. The type

20specified as an operand to sizeof shall be an unmanaged-type (§25.2).

21sizeof-expression:

22sizeof ( unmanaged-type )

23The result of the sizeof operator is a value of type int. For certain predefined types, the sizeof operator

24yields a constant value as shown in the table below.

25

Expression

Result

 

 

sizeof(sbyte)

1

 

 

sizeof(byte)

1

 

 

sizeof(short)

2

 

 

sizeof(ushort)

2

 

 

sizeof(int)

4

 

 

sizeof(uint)

4

 

 

sizeof(long)

8

 

 

sizeof(ulong)

8

 

 

sizeof(char)

2

 

 

sizeof(float)

4

 

 

sizeof(double)

8

 

 

sizeof(bool)

1

 

 

sizeof(decimal)

16

 

 

26

27For all other types, the result of the sizeof operator is implementation-defined and is classified as a value,

28not a constant.

29When the sizeof-expression is not a constant value, the sizeof-expression shall be inside an unsafe context.

30When the sizeof-expression is a constant value, the sizeof-expression is allowed both inside and outside an

31unsafe context.

390

Соседние файлы в предмете Электротехника