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

Slides:



Advertisements
Similar presentations
C Language.
Advertisements

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 12 th -13 th Lecture Pavel Ježek.
Chapter 6 Data Types
Programming Languages and Paradigms The C Programming Language.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
C# Language Report By Trevor Adams. Language History Developed by Microsoft Developed by Microsoft Principal Software Architect Principal Software Architect.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Kernighan/Ritchie: Kelley/Pohl:
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
1 Chapter 4 Language Fundamentals. 2 Identifiers Program parts such as packages, classes, and class members have names, which are formally known as identifiers.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
1 Type Type system for a programming language = –set of types AND – rules that specify how a typed program is allowed to behave Why? –to generate better.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th & 8 th Lecture Pavel Ježek.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
The basics of the array data structure. Storing information Computer programs (and humans) cannot operate without information. Example: The array data.
1 Records Record aggregate of data elements –Possibly heterogeneous –Elements/slots are identified by names –Elements in same fixed order in all records.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
Pointers. What is pointer l Everything stored in a computer program has a memory address. This is especially true of variables. char c=‘y’; int i=2; According.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
19&20-2 Know how to declare pointer variables. Understand the & (address) and *(indirection) operators. Dynamic Memory Allocation Related Chapter: ABC.
C# Classes and Inheritance CNS 3260 C#.NET Software Development.
C# C1 CSC 298 Elements of C# code (part 1). C# C2 Style for identifiers  Identifier: class, method, property (defined shortly) or variable names  class,
The Cn Language over view The Cn language strongly on ANSI C. So if you are familiar with ANCI it is not so tough to deal with Cn language. Basic Data.
Slides created by: Professor Ian G. Harris Hello World #include main() { printf(“Hello, world.\n”); }  #include is a compiler directive to include (concatenate)
1 9/6/05CS360 Windows Programming CS360 Windows Programming.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming I 10 th Lecture Pavel Ježek
Computer Organization and Design Pointers, Arrays and Strings in C Montek Singh Sep 18, 2015 Lab 5 supplement.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 6 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 8 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 11 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 9 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 2 nd Lecture Pavel Ježek
Pointers in C++. Topics Covered  Introduction to Pointers  Pointers and arrays  Character Pointers, Arrays and Strings  Examples.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 4 th Lecture Pavel Ježek
- This slide is intentionally left blank - Some of the slides are based on University of Linz.NET presentations. © University of Linz, Institute for System.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics C# Language &.NET Platform 7 th Lecture Pavel Ježek
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 5 th Lecture Pavel Ježek
Introduction to programming in java Lecture 21 Arrays – Part 1.
Object Oriented Programming Lecture 2: BallWorld.
CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 2 nd Lecture Pavel Ježek
Object Lifetime and Pointers
Dynamic Storage Allocation
Advanced .NET Programming I 11th Lecture
Advanced .NET Programming II 4th Lecture
Programming Languages and Paradigms
Programming Paradigms
Programming Languages and Paradigms
Lecture 6 C++ Programming
DATA HANDLING.
Pointers C#, pointers can only be declared to hold the memory addresses of value types int i = 5; int *p; p = &i; *p = 10; // changes the value of i to.
Arrays in Java.
Java Programming Language
C# Language & .NET Platform 10th Lecture
- This slide is intentionally left blank -
C# Language & .NET Platform 11th Lecture
C# Language & .NET Platform 3rd Lecture
C# Language & .NET Platform 9th Lecture
C# Language & .NET Platform 12th Lecture
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
Presentation transcript:

CHARLES UNIVERSITY IN PRAGUE faculty of mathematics and physics Advanced.NET Programming II 3 rd Lecture Pavel Ježek 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 (

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

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

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

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

Address Arithmetic T* p =...; T p p++; p--; T p T // increases p by sizeof(T) T* q, r; q = p + 2; r = p - 1; T p T // q = p + 2*sizeof(T) // r = p - sizeof(T) rq T Pointers can be used in calculations No pointer arithmetic with void*

Type Casts On Pointers int i; int* ip; Block*bp; void*vp; Implicit Casts (without cast operator) T*  null ip = null; bp = null; vp = null; void*  T* vp = ip; vp = bp; Explicit Casts (with cast operator) T1*  T2* ip = (int*)vp; vp = (void*)ip; bp = (Block*)vp; T*  intType ip = (int*)i; bp = (Block*)i; vp = (void*)i; i = (int)ip; i = (int)bp; i = (int)vp; unchecked !

Pinned and Unpinned Variables Pinned cannot be moved by the garbage collector local variables value parameters fields of structs which are pinned themselves Unpinned can be moved by the garbage collector fields of classes (also static fields) array elements ref and out parameters

Adress Operator &designator Note designator must denote a pinned variable (e.g. &x, &s.f) the type of the variable must not be managed (i.e. no class, no array) if designator is of type T, &designator is of type T* yields the address of designator

fixed Statement Pins an unpinned variable during the execution of this statement (i.e. the garbage collector cannot move it during this statement) Syntax FixedStat= "fixed" "(" PointerType FixedVarDecl {"," FixedVarDecl} ")" Statement. FixedVarDecl= ident "=" ("&" UnpinnedVar |ArrayVar |StringVar ). UnpinnedVar must be of an unmanaged type T T* must be assignable to PointerType array elements must be of an unmanaged type T T* must be asignable to PointerType char* must be assignable to PointerType Examples int field; int[] a = new int[10]; Person person = new Person(); string s = "Hello"; fixed (byte* p = &field) { Console.Write(*(p+1)); } fixed (int* p = &a[1]) {...} fixed (int* p = &person.id) {...} fixed (int* p = a) {...} fixed (char* p = s) {...} Variables that are declared in the header of a fixed statement are read-only

Examples Printing the bytes of an int variable int x = 3; unsafe { byte* p = (byte*) &x; for (int i = 0; i < 4; i++) { Console.Write("{0:x2} ", *p); p++; } } x 3000 p String processing string s = "Hello"; unsafe { fixed (char* p = s) { for (int i = 0; p[i] != '\0'; i++) Console.Write(p[i]); } s Hell p o\0

Examples (continued) Overlaying a byte array with a struct type struct Block { int x; float f; int y; }... byte[] buffer = new byte[1024];... unsafe { fixed (byte* bp = &buffer[3]) { Block* b = (Block*) bp; Console.WriteLine(b->x + " " + b->f + " " + b->y); } xfy b buffer

Dangers of Pointer Processing Can destroy arbitrary memory locations int[] a = new int[3]; unsafe { fixed (int* p = a) { p[5] = 0; } } One can be left with pointers to objects that are moved by the garbage collector int[] a = new int[5]; int* p; unsafe { fixed (int* q = a) { p = q+2; }......// GC can move array now *p = 5;// destroys data } p p q a

Dangers of Pointer Processing (cont.) One can be left with pointers to non-existing local variables static unsafe int* Foo() { int local = 3; return &local; } static unsafe void Main() { int* p = Foo();... *p = 5;// accesses non-existing local variable! } Therefore Avoid pointer processing! Unless you really need it for system-level programming

StructLayout Attribute using System.Runtime.InteropServices; [StructLayout(LayoutKind.Sequential, Pack=4)]// members are allocated sequentially class Time {// and are aligned at 4 byte boundaries public ushort year; public ushort month;... } [StructLayout(LayoutKind.Explicit)]// members are allocated at specified offsets struct S { [FieldOffset(0)] ushort x; [FieldOffset(2)] int y; }

StructLayout Attribute [StructLayout ( layout-enum// Sequential | Explicit | Auto [, Pack=packing-size]// 0, 1, 2, 4, 8, 16,... [, CharSet=charset-enum]// Ansi | Unicode | Auto )]