Presentation is loading. Please wait.

Presentation is loading. Please wait.

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek

Similar presentations


Presentation on theme: "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek"— Presentation transcript:

1 CHARLES UNIVERSITY IN PRAGUE http://d3s.mff.cuni.cz/~jezek faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek pavel.jezek@d3s.mff.cuni.cz Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System Software, 2004 published under the Microsoft Curriculum License (http://www.msdnaa.net/curriculum/license_curriculum.aspx)

2 CLI Type System All types Reference types (allocated on managed heap) PointersValue types (allocated in-place [with exceptions] ) ClassesInterfaces ArraysDelegates Simple types (Int32, Int64, Double, Boolean, Char, …) Nullables Enumerations Structures User defined structures

3 Pointer Types Examples int*ip;// pointer to an int cell MyStruct*sp;// pointer to a MyStruct object void*vp;// pointer to any memory location int**ipp;// pointer to a pointer to an int cell PointerType=UnmanagedType * |void *. UnmanagedType=ValueType |PointerType. pointers to arbitrary types if a struct type, all fields must be of an UnmanagedType Syntax pointers are not traced by the garbage collector (referenced objects are not collected) pointer types are not compatible with System.Object pointer variables can have the value null pointers of different types can be compared with each other (==, !=,, >=) ip sp int MyStruct vp ipp int

4 Unsafe Code Code that works with pointers is potentially unsafe (can break type rules and destroy arbitrary memory locations) must be enclosed in an unsafe block or an unsafe method unsafe { int* p;... } unsafe void foo() { int* p;... } must be compiled with the unsafe option csc -unsafe MyProg.cs system administrator must assign FullTrust rights to the program

5 Using Pointers Dereferencing a pointer int var; int* ip = &var; ip *ip = 5; int x = *ip; if v is of type T*, *v is of type T void* cannot be dereferenced var Access to struct fields struct Block { int x, y, z; } Block b; Block* bp = &b; bp->x = 1;// pointer notation (*bp).y = 2// alternatively bp x y z bp must be of type Block* works also for method calls b Access to array elements int[] a = new int[3]; int* ap = a; ap[1] = 1;// array notation *(ap+1) = 2// alternatively ap no index bound checks! ap[3] would be accepted a 0 1 2


Download ppt "CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek"

Similar presentations


Ads by Google