Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

C++ and must be with shallow copy. Chapter 10 Pointers Part 1 Create an int i with a value of 7. Create a pointer to

C++ and must be with shallow copy.

Chapter 10 Pointers

Part 1

Create an int i with a value of 7. Create a pointer to integer pi. Point your pointer pi to your int variable i. Print out your pointer, the address of your pointer and a dereference of your pointer.

Create a pointer to your integer pointer ppi. Point it to your pointer to int pi. Print out ppi, the address of ppi, a dereference to ppi and a double dereference to ppi.

Part 2

Understanding deep vs. shallow copy is essential for a programmer. You will get into severe problems trying to code if you do not understand it.

The essence of the problem is that 2 objects, which should have independent memory storage, accidently wind up sharing memory.

I want you to wrap a character array (and array with 'a','b','c','d','e' is strictly speaking not a string since it does not end in '\0') with a class (this is just a class that contains an array) and then properly(in Deep) and improperly (in Shallow) assign memory.

Make a class WrapArrayDeep that has a private pointer to char. Your default constructor should allocate an array of size 5. You should have a copy constructor that does a deep copy. (allocates a new array)

Your WrapArrayDeep class should start like:

class WrapArrayDeep{

char *pch;

WrapArrayDeep(){

pch = new char[5];

*pch = 97; //etc.

}

WrapArrayDeep(WrapArrayDeep wad){

// correct copy constructor.

}

}

Make a similar class, WrapArrayShallow, that has an improper copy constructor that causes your copy to point to the array in the source object. (instead of making a new array, have pch point to the original array)

Demonstrate the difference between the classes use

WrapArrayDeep wad1, *wad2;

for the variables holding your WrapArrayDeeps and for WrapArrayShallow:

WrapArrayShallow was1, *was2;

Be sure to include a destructor in each class note it must be an ARRAY destructor put a cout in the destructor showing it was called..

In WrapArrayDeep:

Use pointer arithmetic to load your array with ASCII values for letters.

*pca = 97;

*(pca+1) = 98;

etc.

Use array notation to print your array.

for(int I = 0; I < 5; I++)

cout << pca[i] << endl;

In WrapArrayShallow:

Use array notation to load your array with char data.

pca[0]='v';

pca[1]='w';

etc

Use pointer arithmetic to print your array.

for(int I = 0; I < 5; i++)

cout << *(pca + 1) << endl;

Example Output:

this program section uses 3 variables

i = 7, pi a pointer to i and ppi a pointer to pi

pi = 002EF738

dereference pi 7

address of pi 002EF744

address of i 002EF738

ppi = 002EF744

dereference of ppi 002EF738

address of ppi 002EF72C

double dereference of ppi

7

this section instantiates a wrapper class for a dynamic array of 5 elements

WrapArrayDeep 1

a b c d e

WrapArrayDeep 2 created using the copy constructor on 1

a b c d e

after changing the contents of WrapArrayDeep 1, 1 and 3 =

{ | } ~

a b c d e

Now doing the same thing with WrapArrayShallow

wrapArrayShallow 1

a b c d e

wrapArrayShallow 2 created using the copy constructor on 1

a b c d e

after changing the contents of WrapArrayShallow 1, 1 and 3 =

{ | } ~

{ | } ~

calling destructor for WrapArrayShallow

calling destructor for WrapArrayShallow ***** this may or may not work depending on your compiler

calling destructor for WrapArrayDeep

calling destructor for WrapArrayDeep

Press any key to continue . . .

***** If this crashes your program simply remove it.

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transact SQL Cookbook Help For Database Programmers

Authors: Ales Spetic, Jonathan Gennick

1st Edition

1565927567, 978-1565927568

More Books

Students also viewed these Databases questions