Object Reference and Memory Allocation. Questions:

Similar documents
Functions and Methods. Questions:

Pointers, Dynamic Data, and Reference Types

Extending Classes (contd.) (Chapter 15) Questions:

CS201- Introduction to Programming Current Quizzes

Chapter 2. Procedural Programming

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

CS 376b Computer Vision

Pointers and Arrays CS 201. This slide set covers pointers and arrays in C++. You should read Chapter 8 from your Deitel & Deitel book.

CSE 303: Concepts and Tools for Software Development

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Lecture Notes CPSC 224 (Spring 2012) Today... Java basics. S. Bowers 1 of 8

Arrays. Returning arrays Pointers Dynamic arrays Smart pointers Vectors

Intermediate Programming, Spring 2017*

Exercise 1.1 Hello world

CE221 Programming in C++ Part 2 References and Pointers, Arrays and Strings

Introducing C++ to Java Programmers

PIC 10A Pointers, Arrays, and Dynamic Memory Allocation. Ernest Ryu UCLA Mathematics

CSC1322 Object-Oriented Programming Concepts

CSE 374 Programming Concepts & Tools. Hal Perkins Spring 2010

W3101: Programming Languages C++ Ramana Isukapalli

6. Pointers, Structs, and Arrays. March 14 & 15, 2011

C++ for Java Programmers

CHAPTER 7 OBJECTS AND CLASSES

CSE 374 Programming Concepts & Tools. Hal Perkins Fall 2015 Lecture 19 Introduction to C++

Memory and C++ Pointers

Short Notes of CS201

Program template-smart-pointers-again.cc

arrays review arrays and memory arrays: character array example cis15 advanced programming techniques, using c++ summer 2008 lecture # V.

Inheritance, and Polymorphism.

CS201 - Introduction to Programming Glossary By

The Notion of a Class and Some Other Key Ideas (contd.) Questions:

Scope. Scope is such an important thing that we ll review what we know about scope now:

6. Pointers, Structs, and Arrays. 1. Juli 2011

Fast Introduction to Object Oriented Programming and C++

CS24 Week 3 Lecture 1

a data type is Types

CS2141 Software Development using C/C++ C++ Basics

Principles of Programming Pointers, Dynamic Memory Allocation, Character Arrays, and Buffer Overruns

by Pearson Education, Inc. All Rights Reserved.

CA31-1K DIS. Pointers. TA: You Lu

EL2310 Scientific Programming

CHAPTER 7 OBJECTS AND CLASSES

Discussion 1E. Jie(Jay) Wang Week 10 Dec.2

Chapter 10 Pointers and Dynamic Arrays. GEDB030 Computer Programming for Engineers Fall 2017 Euiseong Seo

And Even More and More C++ Fundamentals of Computer Science

Classes, The Rest of the Story

Pointers and Terminal Control

Dynamic Data Structures. CSCI 112: Programming in C

CS3157: Advanced Programming. Outline

CS

CSC 211 Intermediate Programming. Arrays & Pointers

Constants, References

C++ For Science and Engineering Lecture 15

Run Time Environment

Programming II (CS300)

Absolute C++ Walter Savitch

377 Student Guide to C++

Fundamental Concepts and Definitions

Program template-smart-pointers.cc

The issues. Programming in C++ Common storage modes. Static storage in C++ Session 8 Memory Management

A brief introduction to C programming for Java programmers

OBJECT ORIENTED PROGRAMMING

Lecture 2, September 4

C++ Primer for CS175

Dynamic Allocation in C

CA341 - Comparative Programming Languages

Outline. 1 Function calls and parameter passing. 2 Pointers, arrays, and references. 5 Declarations, scope, and lifetimes 6 I/O

AN OVERVIEW OF C++ 1

Chapter 10. Pointers and Dynamic Arrays. Copyright 2016 Pearson, Inc. All rights reserved.

C10: Garbage Collection and Constructors

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

Homework 4. Any questions?

More Functions. Pass by Value. Example: Exchange two numbers. Storage Classes. Passing Parameters by Reference. Pass by value and by reference

Complex data types Structures Defined types Structures and functions Structures and pointers (Very) brief introduction to the STL

Object Oriented Software Design II

Reference operator (&)

Classes and Objects. Class scope: - private members are only accessible by the class methods.

Linked List using a Sentinel

ALL ABOUT POINTERS C/C++ POINTERS

Lecture 7. Log into Linux New documents posted to course webpage

Reference Parameters A reference parameter is an alias for its corresponding argument in the function call. Use the ampersand (&) to indicate that


In Java we have the keyword null, which is the value of an uninitialized reference type

Assumptions. History

COMS W3101 Programming Language: C++ (Fall 2016) Ramana Isukapalli

Pointers. Reference operator (&) ted = &andy;

Problem Solving with C++

12. Pointers Address-of operator (&)

Week 3: Pointers (Part 2)

8. The C++ language, 1. Programming and Algorithms II Degree in Bioinformatics Fall 2017

Chapter 8. Operator Overloading, Friends, and References. Copyright 2010 Pearson Addison-Wesley. All rights reserved

04-17 Discussion Notes

Object Oriented Design

CS 31 Discussion 1A, Week 8. Zengwen Yuan (zyuan [at] cs.ucla.edu) Humanities A65, Friday 10:00 11:50 a.m.

MM1_ doc Page E-1 of 12 Rüdiger Siol :21

Mastering Data Abstraction or Get nit-picky on design advantages

Scott Gibson. Pointers & Dynamic Memory. Pre & Co Requisites. Random Access Memory. Data Types. Atomic Type Sizes

C++_ MARKS 40 MIN

CS107 Handout 08 Spring 2007 April 9, 2007 The Ins and Outs of C Arrays

Modern C++ for Computer Vision and Image Processing. Igor Bogoslavskyi

Transcription:

Object Reference and Memory Allocation Questions: 1

1. What is the difference between the following declarations? const T* p; T* const p = new T(..constructor args..); 2

2. Is the following C++ syntax legal? T* const p; p = new T(...constructor arguments...); 3

3. Is the following C++ syntax legal? const T* p; p = new T(...constructor args... ); *p = T(... constructor args... ); 4

4. Is the following C++ syntax legal? const T* p; const T* q; p = new T(...constructor args... ); q = p; p = new T(... constructor args... ); 5

5. Is it true or false that a Java array behaves like any other class type object when it comes to polymorphism? 6

6. If you absolutely must use a macro in C++, what must you do immediately afterwords? 7

Object Reference in C++ SPECIAL NOTE: If we had to choose one word on which we could bestow the honor of being the source of the greatest confusion between C++ and Java, that word would be reference. What it means in Java is not the same thing as what it means in C++. 8

An object reference in C++ is merely an alternative name for an object or a variable. In C++, the notation T& means a reference to an object of type T. For example, I could say int i = 2; int& r = i; With the second declaration, the variable r is simply another name for the variable i. So if we say r = 3; we are also causing the value of i to become 3. You might think of r as serving as a sort of alias for i. 9

If you apply the address operator & to r, you will get the address of i: int* q = &r; will cause q to point to i. That is, if we were to declare int* p = &i; the pointers p and q would point to exactly the same location in the memory. 10

If we incremented r by r++; we would actually be incrementing i and achieving i++. 11

The following declaration is an error: int& r = 100; /* WRONG */ That s because the initializer for a T& type must be an object of type T whose address can be ascertained. The constant 100 has no address associated with it. This constraint does not apply to a reference of const T& type. For example, it would be legal to say const int& r = 100; 12

You cannot change the object of a reference. Here is an example: int i = 3; int j = 100; int& r = i; r++; // i is now 4 r = j; // i is now 100 r++; // i is now 101 With these declarations, r remains a reference to i no matter what happens subsequently. 13

You can even have a reference to a pointer type, as the following example illustrates: int i = 3; int* p = &i; int*& q = p; // q serves as a reference to the int* varia cout << *q; // will output 3 *q = 100; // i is now 100 cout << *p; // will output 100 cout << i; // will output 100 14

#include <iostream> #include <string> class User { public: string name; int age; User( string nam, int a ) { name = nam; age = a; } }; int main() { User u1( "Melinda", 87 ); User* u2 = new User( "Belinda", 129 ); User& u3 = u1; const User& u4 = User( "Tralinda", 187 ); cout << "u1 s name is " << u1.name << endl; cout << "u2 s name is " << u2->name << endl; cout << "u3 s name is " << u3.name << endl; cout << "u4 s name is " << u4.name << endl; // Melinda // Belinda // Melinda // Tralinda } User* p = &u1; User* q = &u3; cout << "Pointer p point to User " << p->name << endl; // Melinda cout << "Pointer q point to User " << q->name << endl; // Melinda 15

Object Reference in Java While a class-type object in C++ can be accessed directly, or via a pointer, or via a reference, in Java there is only one mode of accessing non-primitive objects by reference. But the concept reference in Java does not have the same meaning as reference in C++. While a C++ reference is simply another name for an object that was created previously, a Java reference is really more like a disguised pointer in C++. 16

class User { String name; int age; User( String nam, int a ) { name = nam; age = a; } } User u = new User( "Orpheus", 109 ); The C++ analogy here would be: User* p = new User( "Orpheus", 109 ); which would cause p to hold a pointer to an object of type User. 17

Memory Allocation in C++ The operator new allocates memory on the heap in C++: string* str = new string( "hello" ); int* p = new int[500]; 18

It is interesting to know that the operator new actually invokes the following special memory allocation functions: void* operator new( size_t size ); void* operator new[] (size_t size ); For the case of appropriating memory for a single object, the operator new does the following three things 1. The operator new first figures out the size of the object for supplying it as the argument to the function operator new(). 2. The operator new then invokes the function operator new() 3. And, lastly, the operator new casts the void* returned by the function operator new() to string*. 19

The operator new[] goes through the same three steps for the case of arrays, except that now the operator must figure out the memory needed by the entire array before invoking the memory allocation function. 20

Memory is deallocated by using the delete operator for the case of a single object and the delete[] operator for the case of arrays, as in the following examples string* str = new string(buffer); delete str; int* p = new int[1000]; delete[] p; It is critical to bear in mind that after such memory deallocation, the pointer, str in the first case and p in the second case, is still pointing to the original location in memory. A pointer left in this state if dereferenced inadvertently later in the program would lead to a program crash. 21

The operators delete and delete[] actually invoke the following memory deallocation functions: void operator delete( void* ); void operator delete[]( void* ); 22

In the absence of any automatic garbage collection, for every call to new there must exist somewhere in the code a call to delete and for every call to new[] there must exist somewhere later a call to delete[]. Unlike Java, C++ does not come with any guaranteed way for cleaning out unreferenced objects. 23

Memory Allocation in Java All objects, including arrays, in Java are created by using the operator new. This operator constructs an object of a given class and returns a reference to it. 24

If we first define a User class by class User { String name; int age; User( String nam, int a ) { name = nam; age = a; } } we could then a create an object of type User by User u = new User( Zygot, 38 ); //(A) 25

An array of, say, 100 Users would be created by User[] uaarr = new User[ 100 ]; //(B) and an array of a primitive type, say ints, would be created by int[] arr = new int[ 100 ]; //(C) 26

As was the case with C++, memory allocated for new objects created in this manner comes from a part of the system memory known as the heap. 27

Because of automatic garbage collection, Java does not need a delete like operator to free up the memory that belongs to objects that are no longer referenced. 28

Structures in C++ Simple structures in C++ look very much like structures in C. Example: struct User { string name; int age; }; In C, the identifier User is called a structure tag that can be used to declare a variable, say, u, to be of type User by struct User u; However, in C++, User is a new type directly. And we can say User u; 29

A structure in C++ can be thought of as a light-weight C++ class. We can use the dot or the > notation to access the members of a structure, as in void f() { User u; u.name = "Orpheus"; u.age = 89; } This function creates a new user, u, whose name is Orpheus and whose age is 89, the rest of the members remaining uninitialized. 30

One can even embed functions inside the definition of a structure. struct User { string name; int age; void print() { cout << "User " << name << " is of age " << age << endl; } }; u.print(); 31

We can even engage in data hiding by declaring private some of the members of a structure. The default access privilege of a structure member is public, which is opposite of what it is for a class. 32

struct User { private: string name; int age; public: User( string s1, int yy ) { name = s1; age = yy1; } void print() { cout << "User " << name << " is of age " << age << endl; } }; User u( "Zaphod", 27 ); User* p = new User( "Zaphod", 27 ); 33

Quick initialization of an array of structures 34

#include <iostream> using namespace std; struct User { char* name; int age; short rank; }; void print( User*, int ); int main() { User usr_list[] = { "Bigshot, I. R. ", 39, 1, "Allears, U. B, ", 29, 100, "Moonstruck, H. I.", 58, 45 }; int size = sizeof( usr_list ) / sizeof( usr_list[0] ); print( usr_list, size ); } return 0; void print( User* up, int n ) { for ( int i=0; i < n; i++ ) cout << up[i].name << \t << up[i].age << \t << up[i].rank << \n ; } 35