X hits on this document

1317 views

0 shares

0 downloads

0 comments

339 / 396

C# LANGUAGE SPECIFICATION

using System;

class Test {

static void Main() { unsafe { int* values = stackalloc int[20]; int* p = &values[1]; int* q = &values[15]; Console.WriteLine("p - q = {0}", p - q); Console.WriteLine("q - p = {0}", q - p); } } }

which produces the output:

p - q = -14 q - p = 14

If a pointer arithmetic operation overflows the domain of the pointer type, the result is truncated in an implementation-defined fashion, but no exceptions are produced.

18.5.7 Pointer comparison

In an unsafe context, the ==, !=, <, >, <=, and => operators (§‎7.9) can be applied to values of all pointer types. The pointer comparison operators are:

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

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

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

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

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

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

Because an implicit conversion exists from any pointer type to the void* type, operands of any pointer type can be compared using these operators. The comparison operators compare the addresses given by the two operands as if they were unsigned integers.

18.5.8 The sizeof operator

The sizeof operator returns the number of bytes occupied by a variable of a given type. The type specified as an operand to sizeof must be an unmanaged-type (§‎18.2).

sizeof-expression: sizeof   (   unmanaged-type   )

The result of the sizeof operator is a value of type int. For certain predefined types, the sizeof operator yields a constant value as shown in the table below.

Expression

Result

sizeof(sbyte)

1

sizeof(byte)

1

sizeof(short)

2

326Copyright Microsoft Corporation 1999-2003. All Rights Reserved.

Document info
Document views1317
Page views1317
Page last viewedSat Jan 21 11:55:34 UTC 2017
Pages396
Paragraphs9401
Words133190

Comments