Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003.

Slides:



Advertisements
Similar presentations
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
Advertisements

This Time Pointers (declaration and operations) Passing Pointers to Functions Const Pointers Bubble Sort Using Pass-by-Reference Pointer Arithmetic Arrays.
Kernighan/Ritchie: Kelley/Pohl:
Pointers Typedef Pointer Arithmetic Pointers and Arrays.
Informática II Prof. Dr. Gustavo Patiño MJ
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.
Dynamic Memory Allocation in C++. Memory Segments in C++ Memory is divided in certain segments – Code Segment Stores application code – Data Segment Holds.
Pointers: Part I. Why pointers? - low-level, but efficient manipulation of memory - dynamic objects  Objects whose memory is allocated during program.
ARRAYS AND POINTERS Although pointer types are not integer types, some integer arithmetic operators can be applied to pointers. The affect of this arithmetic.
Review on pointers and dynamic objects. Memory Management  Static Memory Allocation  Memory is allocated at compiling time  Dynamic Memory  Memory.
Memory and C++ Pointers.  C++ objects and memory  C++ primitive types and memory  Note: “primitive types” = int, long, float, double, char, … January.
1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation –The new operator –The delete operator –Dynamic.
1 Chapter 9 Pointers. 2 Topics 8.1 Getting the Address of a Variable 8.2 Pointer Variables 8.3 Relationship Between Arrays and Pointers 8.4 Pointer Arithmetic.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Copyright 2001 Oxford Consulting, Ltd1 January Pointers and More Pointers Pointers and Arrays We’ve now looked at  Pointers  Arrays  Strings.
Basic Elements of C++ Chapter 2.
Review of C++ Programming Part II Sheng-Fang Huang.
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.
Pointer Data Type and Pointer Variables
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
CS212: Object Oriented Analysis and Design Lecture 6: Friends, Constructor and destructors.
February 11, 2005 More Pointers Dynamic Memory Allocation.
Pointers review Let a variable aa be defined as ‘int *aa;’, what is stored in aa? Let a variable aa be defined as ‘int ** aa;’ what is stored in aa? Why.
Basic Semantics Associating meaning with language entities.
Copyright  Hannu Laine C++-programming Part 1 Hannu Laine.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 11 Structured Data.
1 C++ Classes and Data Structures Jeffrey S. Childs Chapter 4 Pointers and Dynamic Arrays Jeffrey S. Childs Clarion University of PA © 2008, Prentice Hall.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
Pointers and Dynamic Memory Allocation Copyright Kip Irvine 2003, all rights reserved. Revised 10/28/2003.
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.
Dynamic memory allocation and Pointers Lecture 4.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
Copyright 2005, The Ohio State University 1 Pointers, Dynamic Data, and Reference Types Review on Pointers Reference Variables Dynamic Memory Allocation.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 11: Structured Data.
Copyright © 2012 Pearson Education, Inc. Chapter 11: Structured Data.
Review 1 List Data Structure List operations List Implementation Array Linked List.
CS415 C++ Programming Takamitsu Kawai x4212 G11 CERC building WV Virtual Environments Lab West Virginia University.
Pointers by Dr. Bun Yue Professor of Computer Science CSCI 3333 Data Structures.
System Programming Practical Session 7 C++ Memory Handling.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
1 Classes II Chapter 7 2 Introduction Continued study of –classes –data abstraction Prepare for operator overloading in next chapter Work with strings.
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
Functions. Predefined Functions C++ comes with libraries of code that can be reused in your programs. The code comes in the form of predefined functions.
1 Recall that... char str [ 8 ]; str is the base address of the array. We say str is a pointer because its value is an address. It is a pointer constant.
1 Chapter 15-1 Pointers, Dynamic Data, and Reference Types Dale/Weems.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
Pointers, Arrays, And Dynamic Memory Allocation by Bindra Shrestha CSCI 3333 Data Structures.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
CSC 533: Programming Languages Spring 2016
Eine By: Avinash Reddy 09/29/2016.
Object Lifetime and Pointers
CSC 533: Programming Languages Spring 2015
COM S 326X Deep C Programming for the 21st Century Prof. Rozier
Pointers and Memory Overview
Andy Wang Object Oriented Programming in C++ COP 3330
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Pointers, Dynamic Data, and Reference Types
Dynamic Memory A whole heap of fun….
Chapter 12 Pointers and Memory Management
Dynamic Memory A whole heap of fun….
Java Programming Language
Submitted By : Veenu Saini Lecturer (IT)
Pointers, Dynamic Data, and Reference Types
CSC 533: Programming Languages Spring 2019
SPL – PS2 C++ Memory Handling.
Presentation transcript:

Defining and Converting Data Copyright Kip Irvine, 2003 Last Update: 11/4/2003

Const Qualifier The const qualifier prevents an object from begin modified after its initial definition. A const-qualified object must be given a value when it is defined. A const-qualified object can be assigned to a non-const object. const int n = 25; n = 36; // error const double z; // error int m = n; m = 36;

Storage Allocation A variable with static storage allocation exists for the lifetime of the program. –Global variables –Variables declared with static qualifier A variable with automatic storage is created on the stack when declared inside a code block. –Local function variables –Function parameters A variable with dynamic storage allocation exists on the heap, and my be both created and removed at runtime (new and delete operators).

Storage Allocation Examples string FileName; void MySub( int N ) { double X; static int count; int * pN = new int; } static automatic dynamic

Variable Scope A global variable exists outside of any code block. –a block is described by the {... } symbols A local variable exists inside a code block. –Inside a function, for example A member variable exists inside a class definition block –class name {... }

References A reference is an alias for some existing object. Physically, the reference stores the address of the object it references. In the following example, when we assign a value to rN, we automatically modify N: int N = 25; int & rN = N; rN = 36; cout << N; // "36" displayed

Pointers A pointer stores the address of some other object. The address-of (&) operator obtains the address of an object. int N = 26; int * pN = &N; // get the address

26 N 29A6 pN (Address 29A6) pN points to N because pN contains N's address Implementing a Pointer

Pointer Variables double Z = 26; int * pN; pN = &Z; // error! A pointer variable must be declared with the same type it points to. In the following, pN cannot point to Z because Z is a double: The internal format and size of a double is not the same as an integer!

Dereference Operator int N = 26; int * pN = &N; cout << *pN << endl; // "26" The dereference (*) operator obtains the contents of the variable that is referenced by a pointer. Only pointers can be dereferenced.

Dereference Operator int N = 26; int * pN = &N; *pN = 35; cout << *pN << endl; // "35" cout << N << endl; // "35" The dereference operator can also be used to modify the contents of the referenced variable. Assigning a value to *pN changes the value of N.

Assigning Pointers int N = 26; int * pN = &N; int * pZ; pZ = pN; *pZ = 35; // now N = 35 One pointer may be assigned to another, as long as they point to the same type. In this example, when pZ is dereferenced, it lets us change the value of N:

Assigning Pointers int N = 26; int * pN = &N; int Z = 0; int * pZ = &Z; *pZ = *pN; // Z = 26 Assigning one object to another can be done by dereferencing pointers.

Data Conversion

Implicit Conversion Implicit conversion can take place when an object of one type is assigned to an object of another type C++ handles conversions automatically for intrinsic numeric types Warning messages may appear when there is a danger of losing data. –They vary from one compiler to the next Examples...

Conversion Examples int n = 26; double x = n; double x = 36; int b = x; // possible warning float f = 36.5; // possible warning bool isOK = 1; // possible warning int n = true; char ch = 26; // possible warning int w = 'A';

Cast Operation A cast operation explicitly converts data from one type to another. It is used only for "safe" conversions that might be done by the compiler automatically. Use it to avoid compiler warning messages. The traditional cast operator from the C language puts the new data type in parentheses. C++ improves this with a function-style cast. Examples...

Cast Examples int n = (int)3.5; // traditional C int w = int(3.5); // function-style bool isOK = bool(15); char ch = char(86); string st = string("123"); // errors: no conversions available: int x = int("123"); string s = string(3.5);

static_cast<> The static_cast<> operator is the preferred way to perform "safe" conversions in C++. It replaces both the traditional C cast operator and the C++ function-style cast. Examples...

static_cast examples int w = static_cast (3.5); bool isOK = static_cast (1); char ch = static_cast (86);

The End