CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory...

16
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation

Transcript of CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory...

Page 1: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

CS212: Object Oriented Analysis and DesignLecture 7: Arrays, Pointers and

Dynamic Memory Allocation

Page 2: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Recap of Lecture 6

• Friend classes and member functions

• Constructors

• Implicit and explicit calling

• Default constructors

• Destructors

Page 3: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Outline of Lecture 7

• Arrays of Objects

• Pointers to object

• The this pointer

• Pointer to class members

• Dynamic memory allocation

Page 4: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Arrays of Objects

• It is possible to have arrays of objects

• The syntax for declaring and using an object array is exactly the same as it is for any other type of array.

• Looks similar to array of structures

• Special case for single element constructors

• Intend to create both initialized and uninitialized arrays of objects

Class-name object-name [sizeOfArray];

Page 5: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Pointers to Objects

• Just as you can have pointers to other types of variables, you can have pointers to objects.

• When accessing members of a class given a pointer to an object, use the arrow (->) operator instead of the dot (.) operator.

• Must initialize the pointer before using it.

• Pointer arithmetic is relative to the base type of the pointer.

Page 6: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

The this Pointer

• The Problem: “Yes, inside of a class you have access to all of the private data, but how do you access the object itself like a client would?”

• Each object maintains a pointer to itself which is called the “this” pointer.

• Each object can determine it’s own address by using the “this” keyword.

• Avoids the overhead of passing parameters but still enforces the rules of good sound software engineering by using the appropriate class functions.

Page 7: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Pointers to Class Members

• Special type of pointer that "points" generically to a member of a class

• Not to a specific instance of that member in an object

• It is called a pointer to a class member or a pointer-to-member

• Since member pointers are not true pointers, the . and -> cannot be applied to them.

• Pointer-to-member operators .* and –>*.

Page 8: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

References

• A reference is essentially an implicit pointer.

• Three ways it can be used

• as a function parameter,

• as a function return value,

• as a stand-alone reference.

Page 9: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Passing References to Objects

• When an object is passed as an argument to a function, a copy of that object is made.

• When the function terminates, the copy's destructor is called.

• If you do not want the destructor function to be called, simply pass the object by reference.

Page 10: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Returning References

• A function may return a reference.

• This has the rather startling effect

• Allows a function to be used on the left side of an assignment statement!!

Page 11: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Independent Reference

• Most common uses for references - argument, return value

• Declare a reference that is simply a variable

• This type of reference is called an independent reference.

Page 12: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Dynamic memory allocation

• Two dynamic allocation operators: new and delete

• These operators are used to allocate and free memory at run time.

• Dynamic allocation is an important part

• The new operator allocates memory and returns a pointer to the start of it.

• The delete operator frees memory previously allocated using new.

Page 13: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Syntax of new, delete operators

• pointer = new typepointer = new type [number_of_elements]

• delete pointer;

delete[] pointer;

• It is a requested by a program

• To be allocated by the system from the memory heap.

• No guarantee that all requests to allocate memory using operator new are going to be granted by the system.

Page 14: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

How to check memory allocation

• C++ provides two standard mechanisms to check if the allocation was successful

• One is by handling exceptions.

• The other method is known as nothrow

• Instead of throwing a bad_alloc exception or terminating the program, the pointer returned by new is a null pointer

Page 15: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Different uses of dynamic allocation operators

• Initializing Allocated Memory

• Allocating Arrays

• Allocating Objects• When it is created, its constructor function (if it has one) is

called.

• When the object is freed, its destructor function is executed.

Page 16: CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.

Thank youNext Lecture: Function Overloading