Question: ASE DO NOT COPY PASTE FROM CHEGG/OTHERWISE I'LL REPORT YOU OR GIVE OU A DISLIKE [Q-10] If I were to copy a pointer... int $star

ASE DO NOT COPY PASTE FROM CHEGG/OTHERWISE I'LL REPORT YOU OR GIVE OU A DISLIKE

ASE DO NOT COPY PASTE FROM CHEGG/OTHERWISE I'LL REPORT YOU OR GIVE

[Q-10] If I were to copy a pointer... int $\star x=$ new ptr; int $* y=x :$ ... I have done what is called a "shallow copy". That is, I have simply copied the memory address contained in $x$. In other words, "x and $y$ point to the same thing". A downstream effect of this is that if I change the value in memory that $\mathrm{x}$ points to, then I will 11.32 Deep Copy If I were to copy a pointer... int * X = new ptr, int * Y = x; ... I have done what is called a "shallow copy'. That is, I have simply copied the memory address contained in x. In other words, "x and y point to the same thing". A downstream effect of this is that if I change the value in memory that x points to, then I will also change it for y. For example... int * X = new ptr; *x = 22; int *y = x; *y = 23; // the value in memory that x points to is also now 23 Sometimes this shallow copy situation is preferred (that x and y point to the same thing). An alternative to a shallow copy is the deep copy. A deep copy is where you create new memory and you copy the contents of the memory instead of just the pointers. A deep copy in the example above would look like the following: int * X = new ptr, *x = 22; int * y = new ptr, *y = *x; // now the pointers point to two separate memory = locations... so if I update the value that y points to... y = 23; // now the memory x points to contains "22" and the memory y points to contains "23" since x and y point to separate pieces of memory This concept is fairly simple for small pieces of memory, but as the data structure becomes more complex, so does the deep copy. For example: int size = 5; // load x with data int * x = new ptr[size]; for (int i = 0; i

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!