Pointers and Dynamic Memory Management 2 - PowerPoint PPT Presentation

1 / 18
About This Presentation
Title:

Pointers and Dynamic Memory Management 2

Description:

the allocation of memory cells in performed in this constructor ... Identified by a tilde (~) preceding the name of the class. complement of the constructor ... – PowerPoint PPT presentation

Number of Views:81
Avg rating:3.0/5.0
Slides: 19
Provided by: anne277
Category:

less

Transcript and Presenter's Notes

Title: Pointers and Dynamic Memory Management 2


1
Pointers and Dynamic Memory Management - 2
  • CSC 2110 - Data Structures Abstraction
  • Spring/Summer 2001
  • Wayne State University
  • Instructor Anne-Marie Bosneag

2
Content
  • Example - IntVec
  • Constructor, destructor
  • Copy semantics - shallow deep copy

3
IntVec Class
  • An instance represents an integer vector
  • parameterized constructor construct an instance
    of a vector with specified dimensions
  • the allocation of memory cells in performed in
    this constructor
  • IntVec alpha(10)//size specified in advance,
    similar to arrays
  • int n cin gtgt n IntVec beta(n)
  • //the size is decided at run time. flexible
  • copy constructor
  • aggregate assignment
  • destructor automatically free allocated memory

4
IntVec class specification
  • //vector.h
  • class IntVec
  • public
  • void operator(IntVec vec 2)//overloaded
    aggregate // assignment
  • int operator(int i) const//element
    selection
  • IntVec(int numEls) // create an instance of
    numEls // dimensions
  • IntVect(const IntVec vec2) //copy constructor
  • IntVec()//destructor
  • private
  • int vec//pointer to elements
  • int size//number of elements

5
Private members
  • vec is a pointer to an array of integers with
    size elements in the heap
  • size the lenght of the vector
  • the array is allocated and its address is
    assigned to vec when the parameterized
    constructor is invoked and size is set by the
    parameter
  • allocated memory (pointed to by vec) should be
    deallocated when it is no longer needed
  • implicitly done by the destructor, a member
    function

6
Parameterized constructor
  • IntVecIntVec(int numEls)
  • if (numElslt1)
  • exit (1)
  • vecnew intnumEls
  • sizenumEls
  • int n
  • cin gtgt n //suppose 7
  • IntVec beta(n)
  • parameterized constructor is invoked with
    parameter 7.
  • An array of 7 integers is allocated in heap and
    its address is returned to betas member vec
  • betas member size is set to 7

7
Destructor function
  • Identified by a tilde () preceding the name of
    the class
  • complement of the constructor
  • deallocates memory allocated by constructor
  • invoked when the class instance is destroyed
  • a class instance is destroyed when it goes out of
    scope the control exits the block in which it is
    declared
  • for IntVec class, the destructor is IntVec()

8
Destructor for IntVec
  • Destructor of an object is invoked when it is out
    of scope
  • void func_a()
  • int n
  • IntVec alpha(n)//memory allocated this point
  • //destructor invoked here
  • Free the memory pointed by vec, which was
    allocated by the constructor
  • IntVecIntVec()
  • delete vec

9
Use of class destructors
  • An automatically invoked member function when the
    object is destroyed
  • Whenever the creation of a class object involves
    heap allocation, a destructor should be provided
  • No parameters and no data types for destructors

10
Overloading subscript operator
  • To access elements in the dynamic array using
  • int IntVecoperator(int I)
  • if (Ilt0Igtsize)
  • cerr ltlt
  • exit(1)
  • return vecI
  • void main(void)
  • IntVec someVec(40)
  • int nsomeVec5 //1
  • someVec720//2
  • in line 1, someVec5 is implicitly dereferenced,
    so value of 6th element in the array is assigned
    to n
  • in line 2, the 8the element is assigned with 20

11
Overloading the assignment operator
  • The IntVec contains a pointer member
  • the built-in operator copies the class members,
    not data
  • myVecyourVec//the vec pointer in yourVec is
    assigned to vec in myVec, so they will point to
    the same thing
  • result in shallow copy
  • causes inaccessible objects when class has
    pointer members
  • if copy of data is desired, the should be
    overloaded
  • myVecyourVec //make the values in vec of myVec
    equal to those in yourVec, but they are separate
    objects
  • deep copy

12
Shallow Copy Deep Copy
Consider myvec 0 1 2 vec
16 24 8 size 3
yourvec 0 1 2 vec
7 63 92 size 3
13
After the instruction myvec
yourvec Assume we have not overloaded the
operator
myvec 0 1 2 vec
16 24 8 size 3
yourvec 0 1 2 vec
7 63 92 size 3
In shallow copy -- the pointers, but not the
pointed-to data -- are copied. What we want is a
deep copy operation.
14
After the instruction myvec yourvec Which
is equivalent to myvec.operator(yourvec)
myvec 0 1 2 vec
7 63 92 size 3
yourvec 0 1 2 vec
7 63 92 size 3
15
  • C by default performs initialization
  • using shallow copy semantics.
  • To handle this situation we use copy-
  • constructor.
  • In a class declaration, its prototype
  • has the following form
  • class someClass
  • ...
  • someClass (const someClass )

16
Overloaded implementation
  • Suppose intVec1intVec2
  • Assign the values in the array of another vector
    (intVec2) to this vector (intVec1) if their sizes
    are the same
  • in the for loop, copy the element values
    one-by-one from intVec2 to intVec1
  • void IntVecoperator (IntVec intVec2)
  • if (size!intVec2.size)
  • cout ltlt
  • exit(1)
  • int I
  • for (I0 Iltsize I)
  • vecIintVec2.vecI

17
Copy constructor
  • Creates an instance of a class
  • the new created instance is initialized to the
    same values as another instance
  • create a new instance of a class based on an
    existing instance of the same class
  • deep copy
  • the copy constructor takes the existing instance
    as the parameter
  • prototype someClass(const someClass)
  • invoke IntVec newVec(oldVec)

18
Copy constructor
  • Suppose IntVec newVec(oldVec)
  • Make the size of new object newVec equal to size
    of oldVec
  • allocate memory cells according to size and
    return to vec
  • copy values of vec in oldVec one-by one to newVec
  • //implementation
  • IntVecIntVec(const IntVec intVec2)
  • sizeintVec2.size
  • vecnew intsize
  • int I
  • for (I0 Iltsize I)
  • vecIintVec2.vecI
Write a Comment
User Comments (0)
About PowerShow.com