Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string.

Slides:



Advertisements
Similar presentations
Data Structures Using C++ 2E
Advertisements

Chapter 6 Data Types
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
Unions The storage referenced by a union variable can hold data of different types subject to the restriction that at any one time, the storage holds data.
CS-1030 Dr. Mark L. Hornick 1 Pointers And Dynamic Memory.
Various languages….  Could affect performance  Could affect reliability  Could affect language choice.
Pointer applications. Arrays and pointers Name of an array is a pointer constant to the first element whose value cannot be changed Address and name refer.
Kernighan/Ritchie: Kelley/Pohl:
Informática II Prof. Dr. Gustavo Patiño MJ
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
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.
Pointer. Warning! Dangerous Curves C (and C++) have just about the most powerful, flexible and dangerous pointers in the world. –Most other languages.
Pointers. Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location.
Pointers and Dynamic Variables. Objectives on completion of this topic, students should be able to: Correctly allocate data dynamically * Use the new.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Object References. Objects An array is a collection of values, all of the same type An object is a collection of values, which may be of different types.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 9: Pointers.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Overview Working directly with memory locations is beneficial. In C, pointers allow you to: change values passed as arguments to functions work directly.
Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 13: Pointers, Classes, Virtual Functions, and Abstract Classes.
Pointer Data Type and Pointer Variables
Data Structures Using C++ 2E Chapter 3 Pointers and Array-Based Lists.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes.
Lecture 2 Arrays, Pointers, and Structures. Objective In this chapter, we will discuss several concepts: Arrays (first-class arrays, using vector) Strings.
Addresses in Memory When a variable is declared, enough memory to hold a value of that type is allocated for it at an unused memory location. This is.
1 C - Memory Simple Types Arrays Pointers Pointer to Pointer Multi-dimensional Arrays Dynamic Memory Allocation.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
Chapter 0.2 – Pointers and Memory. Type Specifiers  const  may be initialised but not used in any subsequent assignment  common and useful  volatile.
C++ Programming: From Problem Analysis to Program Design, Second Edition1 Objectives In this chapter you will: Learn about the pointer data type and pointer.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
1 Pointers Arrays have a disadvantage: Their size must be known at compile time. We would like the capability to allocate an array-like object of any needed.
Chapter 1 Arrays, Pointers, and Structures Saurav Karmakar Spring 2007.
Recap Visual Perception and Data Visualization Types of Information Display Examples of Diagrams used for Data Display Planning Requirement for Data Visualization.
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.
1 Dynamic Memory Allocation –The need –malloc/free –Memory Leaks –Dangling Pointers and Garbage Collection Today’s Material.
Dynamic Memory Allocation. Domain A subset of the total domain name space. A domain represents a level of the hierarchy in the Domain Name Space, and.
Pointers in C++. 7a-2 Pointers "pointer" is a basic type like int or double value of a pointer variable contains the location, or address in memory, of.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes.
CS 261 – Data Structures Introduction to C Programming.
Pointers in C Computer Organization I 1 August 2009 © McQuain, Feng & Ribbens Memory and Addresses Memory is just a sequence of byte-sized.
1 CS Programming Languages Class 09 September 21, 2000.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 14: Pointers.
ICOM 4035 – Data Structures Dr. Manuel Rodríguez Martínez Electrical and Computer Engineering Department Lecture 2 – August 23, 2001.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
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.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 11: Pointers.
Chapter 12: Pointers, Classes, Virtual Functions, Abstract Classes, and Lists.
C++ for Engineers and Scientists Second Edition Chapter 12 Pointers.
You learned how to declare pointer variables how to store the address of a variable into a pointer variable of the same type as the variable how to manipulate.
Data Types Chapter 6: Data Types Lectures # 13. Topics Chapter 6: Data Types 2 Introduction Primitive Data Types Character String Types Array Types Associative.
CMSC 341 Lecture 2 – Dynamic Memory and Pointers (Review)
Java Review: Reference Types
Pointers and References
Andy Wang Object Oriented Programming in C++ COP 3330
Dynamic Memory Allocation
Chapter 12: Pointers, Classes, Virtual Functions, and Abstract Classes
Object Oriented Programming COP3330 / CGS5409
Chapter 14: Pointers, Classes, Virtual Functions, and Abstract Classes
Chapter 12 Pointers and Memory Management
Presentation transcript:

Recap Resizing the Vector Push_back function Parameters passing Mechanism Primitive Arrays of Constants Multidimensional Arrays The Standard Library string Type

Pointer Syntax in C++ To have a pointer point at an object, the target object's memory address must be known For any object obj, its memory address is given by applying the unary address-of operator & Thus &obj is the memory location that stores obj We can declare that an object ptr points at an int object by saying int *ptr; The value represented by ptr is an address using ptr before assigning anything to it invariably produces bad results

Continued…. Suppose we have declaration int x=5; int y=7; make ptr point at x by assigning to ptr the memory location where x is stored. Thus ptr = &x; / / LEGAL The unary dereferencing operator * access the data through pointer To dereference something that is not a pointer is illegal The * operator is the opposite of & Dereferencing works not only for reading values from an object, but also for writing new values to the object *ptr = 10; // LEGAL changed the value of x to 10

Continued…. Declaration of pointer can be done as int x = 5; int y = 7; int *ptr = &x; / / LEGAL Wrong declaration int *ptr = &x; // ILLEGAL: x is not declared yet int x = 5; int y = 7;

Continued…. Wrong declaration int x = 5; int y = 7; int *ptr = x; // ILLEGAL: x is not an address Correct but not meaningful declaration int x = 5; int y = 7; int *ptr; // LEGAL but ptr is uninitialized

Continued…. A dereferenced pointer behaves just like the object that it is pointing at. So, following declaration will save x=15 x = 5; ptr = &x; *ptr += 10; Two more declaration of dereferenced operator *ptr +=1; *ptr ++; In the first statement the += operator is applied to *ptr, but in the second statement the ++ operator is applied to ptr

Continued…. If ptr1 and ptr2 are pointers to the same type, then following sets ptrl to point to the same location as ptr2 ptr1 = ptr2; Following assigns the dereferenced ptrl the value of the dereferenced ptr2 *ptr1 = *ptr2; Following is true if the two pointers are pointing at the same memory location ptrl == ptr2 Following is true if the values stored at the two indicated addresses are equal *ptr1 == *ptr2;

Null Pointer The NULL pointer points at a memory location that is guaranteed to be incapable of holding anything A NULL pointer cannot be dereferenced The symbolic constant NULL is defined in several header files, and either it or an explicit zero can be used Pointers are best initialized to the NULL pointer because in many cases they have no default initial values

Dynamic Memory Management Term automatic variable indicate local variables are created when they are reached in the function and that they are destroyed when they are no longer in scope Objects need to be created in a different way. This different way is called dynamic memory allocation

The new Operator Objects can be created dynamically by calling new The new operator dynamically allocates memory It returns a pointer to the newly created object

Garbage Collection and delete Operator In some languages, when an object is no longer referenced, it is subject to automatic garbage collection C++ does not have garbage collection When an object allocated by new is no longer referenced, the delete operator must be applied to the object Otherwise, the memory that it consumes is lost which is known as a memory leak Unfortunately memory leaks are common occurrences in many C++ programs Fortunately, many sources of memory leaks can be automatically removed with care

Continued…. One important rule is not to use new when an automatic variable can be used instead An automatic variable is automatically cleaned up The delete operator should never used on an object that was not created by new; if it is used, run-time havoc is likely to result