answersLogoWhite

0

To swap two objects, A and B, you need a temporary object, T. top act as an intermediate.

A traditional swap uses the following algorithm:

Copy A to T

Copy B to A

Copy T to B

The problem with this is that it is highly inefficient. Think about what is happening to the original objects at each stage:

Copy A to T: there are now 2 copies of A in memory and one of B Copy B to A: there are now 2 copies of B in memory and one of A Copy T to B: there are now 2 copies of A in memory and one of B

Even when the swap is physically complete we've still got to destroy that one extra copy of the original A. But why are we even making copies at all? When people swap object they don't make copies of those objects, they simply transfer ownership and that's the same concept we should be emulating in code. Instead of copying objects, we should be moving them:

Move A to T

Move B to A

Move T to B

We still need a temporary object in order to mediate the swap, of course, however at every stage of the swap there's only ever one instance of A and one of B in memory.

Note that when we say move we're not physically moving objects in memory, nor are we copying them. What we're actually doing is swapping resources. That is, when we move A to T, A passes ownership of its resources to T and in return receives ownership of T's resources. But since T shouldn't own any resources to begin with, neither will A when the move is complete -- and that's exactly what we want.

User Avatar

Wiki User

10y ago

What else can I help you with?

Related Questions

A program to swap values using pass by value?

#include<conio.h> main() { int a,b; scanf("%d%d",&a,&b); printf("Before swapping A= %d B=%d",a,b); swap(a,b); getch(); } void swap(int a,int b) { int c; c=a; a=b; b=c; printf("After swapping A= %d B=%d",a,b); }


What is the procedure of getting admission in Pharm-D and shedule in Pakistan?

You can get admission in Zahor Medical University in Pakistan in MD program after Pharm-D


What is program in c to swap entered two numbers without using third variable?

The required c program is given below /*Swapping(interchange) the two entered numbers*/ #include<stdio.h> main() { /*Without using third variable*/ int a,b,t; printf("Enter a:"); scanf("%d",&a); printf("Enter b:"); scanf("%d",&b); a=a+b; b=a-b; a=a-b; printf("\n After swapping without using third variable"); printf("\na=%d\nb=%d",a,b); }


What is the flowchart for swapping two variables?

include#includevoid main(){int x, y, temp;printf("Enter the value of x and y ");scanf("d", &x, &y);printf("Before Swapping x = %d\ny = %d\n",x,y);temp = x;x = y;y = temp;printf("After Swapping x = %d\ny = %d\n",x,y);getch();return 0;}Do you happen to know what a flow-chart is?


How can you highlight the output of a c program?

make d screen of turbo C small and right click on d left most corner wid d right button of the mouse then select edit n then click mark and then again follow d same procedure to copy it.....


C program for swapping two values without temporary value?

#include<stdio.h> void main() { int a,b; printf("enter number 1\n"); scanf("%d",&a); printf("enter number 2\n"); scanf("%d",&b); printf("the swapped result is\n"); a=a^b; b=a^b; a=a^b; printf("%d\n",a); printf("%d\n",b); }


What has the author Thomas D Rowe written?

Thomas D. Rowe has written: 'Civil procedure' -- subject(s): Cases, Civil procedure 'Civil procedure' -- subject(s): Cases, Civil procedure


How long after a D and C procedure can you try to conceive again?

how long after a d and c procedure normal periods come


C program to swap three variables?

#include<stdio.h> #include<conio.h> void main() { int a=2,b=3; swap(a,b); printf("%d%d",a,b); getch() } swap(int *x,int *y) { int t; t=*x; *x=*y; *y=t; printf("%d%d",x,y); }


What procedure is done if you have miscarriage?

A D&C, Dilation and Curettage, which is a procedure to scrape the uterus.


How do you write a c program to swap the values of two variables in unix?

#include<stdio.h> void main() { int a=2,b=4; printf("Program for swapping two numbers "); printf("Numbers before swapping"); printf("a=%d and b=%d",a,b); a=((a+b)-(b=a)); printf("Numbers after swapping"); printf("a=%d and b=%d",a,b); getch(); }


Do you need PAT for D and C procedure?

At most hospitals you would need PAT, or pre-admission testing, before a D&C procedure. Your doctor will tell you what you need to have done before the procedure.