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.

Slides:



Advertisements
Similar presentations
Programming Languages and Paradigms The C Programming Language.
Advertisements

Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Kernighan/Ritchie: Kelley/Pohl:
Data Types in Java Data is the information that a program has to work with. Data is of different types. The type of a piece of data tells Java what can.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
ISBN Chapter 6 Data Types: Structured types.
C#.NET C# language. C# A modern, general-purpose object-oriented language Part of the.NET family of languages ECMA standard Based on C and C++
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
Chapter 6 Structured Data Types Arrays Records. Copyright © 2007 Addison-Wesley. All rights reserved. 1–2 Definitions data type –collection of data objects.
Peter Juszczyk CS 492/493 - ISGS. // Is this C# or Java? class TestApp { static void Main() { int counter = 0; counter++; } } The answer is C# - In C#
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 19 Clicker Questions November 3, 2009.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Java2C# Antonio Cisternino Part II. Outline Array types Enum types Value types  differences from classes  boxing and unboxing Delegate types  Base.
Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.
More about Class 靜宜大學資工系 蔡奇偉副教授 ©2011. 大綱 Instance Class Members Class members can be associated with an instance of the class or with the class as a.
C# EMILEE KING. HISTORY OF C# In the late 1990’s Microsoft recognized the need to be able to develop applications that can run on multiple operating system.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
Understanding Data Types and Collections Lesson 2.
Types(1). Lecture 52 Type(1)  A type is a collection of values and operations on those values. Integer type  values..., -2, -1, 0, 1, 2,...  operations.
Copyright Curt Hill Variables What are they? Why do we need them?
Object Oriented Software Development 4. C# data types, objects and references.
1 Lecture07: Memory Model 5/2/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Introduction to C# By: Abir Ghattas Michel Barakat.
Array and Pointers An Introduction Unit Unit Introduction This unit covers the usage of pointers and arrays in C++
Java and C# - Some Commonalities Compile into machine-independent, language- independent code which runs in a managed execution environment Garbage Collection.
Windows Programming Lecture 03. Pointers and Arrays.
Object Based Programming Chapter 8. 2 Contrast ____________________ Languages –Action oriented –Concentrate on writing ________________ –Data supports.
Chapter 9 A Second Look at Classes and Objects - 4.
Understanding Data Types and Collections Lesson 2.
Object Lifetime and Pointers
The C++ Data Types Fundamental Data Types
Dynamic Storage Allocation
Data Types In Text: Chapter 6.
Creating and Using Objects, Exceptions, Strings
Chapter 6 – Data Types CSCE 343.
Static data members Constructors and Destructors
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Type Checking, and Scopes
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Pointers, Enum, and Structures
Advanced .NET Programming II 4th Lecture
Java Primer 1: Types, Classes and Operators
Pointers and Memory Overview
BY GAWARE S.R. COMPUTER SCI. DEPARTMENT
Student Book An Introduction
Pointers Revisited What is variable address, name, value?
Java Review: Reference Types
Enumerated DATA Types Enum Types Enumerated Data Types
CS360 Windows Programming
.NET and .NET Core 5.2 Type Operations Pan Wuming 2016.
Advanced Programming Behnam Hatami Fall 2017.
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Beginning C Lecture 2 Lecturer: Dr. Zhao Qinpei
Enumerated DATA Types Enum Types Enumerated Data Types
CS 240 – Lecture 7 Boolean Operations, Increment and Decrement Operators, Constant Types, enum Types, Precedence.
Pointer Variables A pointer is a variable that contains a memory address The address is commonly the location of another variable in memory This pointer.
The C Language: Intro.
Quiz Points 1 Rules Raise your hand if you know the question
Classes and Objects Object Creation
Lecture 7: Types (Revised based on the Tucker’s slides) 10/4/2019
SPL – PS2 C++ Memory Handling.
CSE 303 Concepts and Tools for Software Development
Interfaces, Enumerations, Boxing, and Unboxing
Presentation transcript:

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 10 int * p1, p2; // declares two pointers p1 and p2 // ‘*’ applies to the type ‘->’ is used to access the fields in a struct

Pointers and Unsafe Code Managed code Managed code is executed under the control of Common Language Runtime (CRL) Automatic garbage collection No explicit memory allocation and deallocation No explicit destructor Unmanaged code Provides access to memory through pointers Useful in: Pointers may be used to enhance performance in real time applications External functions: In non .net DLLs some functions require a pointer as a parameter such as Windows APIs that were written in C Debugging: Sometimes we need to inspect the memory contents for debugging purposes or, might need to write an application that analyzes another applications’ processes and memory

‘unsafe’ The keyword ‘unsafe’ is used while dealing with pointer The name reflects the risk that you might face while using it

Disadvantages of Unsafe Code in C# Complex syntax Harder to use Misusing pointers might lead to the following; Overwrite other variables Illegal access to memory areas not under your control Stack overflow Pointers are harder to debug You may compromise type safety

We can declare a whole class as unsafe unsafe class Class1 { //you can use pointers here! } Or only some class members can be declared as unsafe class Class1 { unsafe int * ptr; //pointer unsafe void MyUnsafeMethod() { //you can use pointers here! } }

To declare unsafe local variables in a method, you have to put them in unsafe block as follows: static void Main() { //can't use pointers here unsafe { //you can declare and use pointer here } //can't use pointers here }

‘fixed’ and Garbage Collector GC changes position of an object the pointer will point at wrong place in memory To avoid such problems C# contains ‘fixed’ keyword – informing the system not to relocate an object by the garbage collector

Arrays Single Dimensional Arrays Multidimensional Arrays Rectangular Jagged A rectangular array is a single array with more than one dimension (the dimensions' sizes are fixed in the array's declaration) int[,] squareArray = new int[2,3];

Jagged Arrays Multidimensional arrays with irregular dimensions This flexibility derives from the fact that multidimensional arrays are implemented as arrays of arrays int[][] jag = new int[2][]; jag[0] = new int [4]; jag[1] = new int [6]; Each jag[0] and jag[1] holds a reference to a single-dimensional ‘int’ array

Pointers and Arrays A pointer can be declared in relation to an array: int[] a = {4, 5}; int *b = a; Memory location held by ‘b’ is the location of the first type held by ‘a’ This first type must, as before, be a value type Pointer arithmetic

Enumerations An enumeration is a special kind of value type limited to a restricted and unchangeable set of numerical values. By default, these numerical values are integers, but they can also be longs, bytes, etc. (any numerical value except char)

public enum DAYS {      Monday=1, Tuesday,      Wednesday,      Thursday, Friday,      Saturday,      Sunday }

In C# enumerations are type-safe, that means that the compiler will do its best to stop you assigning illegal values to enumeration type variables. For instance, the following code should not compile: int i = DAYS.Monday; DAYS d = i; In order to get this code to compile, you would have to make explicit casts both ways (even converting from DAYS to int) int i = (int)DAYS.Monday; DAYS d = (DAYS) i;

A useful feature of enumerations is that one can retrieve the literal as a string from the numeric constant with which it is associated. This is given by the default ToString() method, so the following expression comes out to be true: DAYS.Monday.ToString()=="Monday"

Arrays int[] i = new int[2]; i[0] = 1; i[1] = 2; By default, as we have seen, all arrays start with their lower bound as 0 Using the .NET framework's System.Array class it is possible to create and manipulate arrays with an alternative initial lower bound