Microsoft Interview Question
Software Development ManagersCountry: India
Interview Type: In-Person
std::swap is defined as
template<typename _Tp>
inline void
swap(_Tp& __a, _Tp& __b);
The function matching the type (Node *) is called instead of generic type.
Try swap((void*)a1, (void*)b1);
This code is not working you forgot to add header file for malloc. You will have to add
#include<malloc.h>
in the starting of code.
after that code is working fine. Complete running code at my terminal.
#include<iostream>
#include<malloc.h>
using namespace std;
struct node{
int a,b;
};
typedef struct node Node;
void swap(void *a,void *b){
void *temp;
temp=a;
a=b;
b=temp;
}
int main() {
Node *a1,*b1;
a1=(Node*)malloc(sizeof(Node));
b1=(Node*)malloc(sizeof(Node));
a1->a=10;
a1->b=20;
b1->a=30;
b1->b=40;
cout<<a1->a<<" "<<a1->b<<endl;
cout<<b1->a<<" "<<b1->b<<endl;
swap(a1,b1);
cout<<a1->a<<" "<<a1->b<<endl;
cout<<b1->a<<" "<<b1->b;
return 0;
}
Your swap code doesn't work. Actually, the parameter passed through the function is passed by value, instead of refence, even if it's a pointer. As a result, what your code does is just to swap the two temporary pointers, which are destroyed when they're out of function scope.
flag == 1 is the right code
As seen in this output... the object P now holds the values 30 40
Before Pointer p1: 1020
------>Before Object P : 1020
-In written function
-Switch Values physically
After Pointer p1: 3040
------>After Object P : 3040
flag ==2 does nothing. As you said its just passed by value and thrown out
i left it in so i could see the outputs and compare
Before Pointer p1: 1020
------------>Before Object P : 1020
-In written function
-Does nothing in code
After Pointer p1: 1020
------------->After Object P : 1020
What worked for me
1.Explicitly calling swap using swap((void *)a,(void *)b);
2.Using memcpy to physically change contents of the objects.
Code below. Tested on c++ in codepad.com
//Swapping objects using void pointers
#include <iostream>
#include <stdlib.h>
using namespace std;
struct node
{
int a,b;
};
typedef struct node Node;
void swap(void *a, void *b, int flag=1)
{
cout<<"\n-In written function";
//CORRECT CODE
if (flag==1)
{
//Physically replaces data while pointers point to same location
cout<<"\n-Switch Values physically";
void *temp = new Node;
memcpy(temp,a,sizeof(Node));
memcpy(a,b,sizeof(Node));
memcpy(b,temp,sizeof(Node));
}
//INCORRECT CODE
else if (flag==2)
{
//Exchanges what pointers are pointing to
cout<<"\n-Does nothing in code";
void *temp;
temp =a;
a=b;
b=temp;
}
else
{
cout<<"\n-Wrong choice nothing done";
}
}
int main()
{
Node P,Q;
Node *p1 = &P;
Node *p2 = &Q;
p1->a = 10; p1->b = 20;
p2->a = 30; p2->b = 40;
cout<<"\n Before Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
cout<<"\n Before Objects P : "<<P.a<<P.b;
cout<<"\n In built swap called. Not the one we wrote. Changes what the pointers point to. Uses template to accomplish. Wise.";
swap(p1,p2); //Calls build in function from std. No String in output.
cout<<"\n After Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
cout<<"\n After Object P : "<<P.a<<P.b;
swap(p1,p2); //swaps back
//Correct Run
cout<<"\n\n Before Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
cout<< "\n Before Object P : "<<P.a<<P.b;
swap((void *)p1,(void *)p2,1); //Explicit call to our function
cout<<"\n After Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
cout<<"\n After Object P : "<<P.a<<P.b;
swap((void *)p1,(void *)p2,1); //swaps back
//Incorrect Run
cout<<"\n\n Before Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
cout <<"\n Before Object P : "<<P.a<<P.b;
swap((void *)p1,(void *)p2,2); //Explicit call to our function
cout<<"\n After Pointer p1: "<<((Node *)p1)->a<<((Node *)p1)->b;
cout<<"\n After Object P : "<<P.a<<P.b;
return 0;
}
Output
//IN BUILT SWAP FUNCTION CALLED INSTEAD OF THE ONE ABOVE
Before Pointer p1: 1020
Before Objects P : 1020
In built swap called. Not the one we wrote. Changes what the pointers point to. Uses template to accomplish.Wise.
After Pointer p1: 3040
After Object P : 1020
//CORRECT RESULT
Before Pointer p1: 1020
Before Object P : 1020
-In written function
-Switch Values physically
After Pointer p1: 3040
After Object P : 3040
//SWAP BACK IGNORE
-In written function
-Switch Values physically
//INCORRECT RESULT
Before Pointer p1: 1020
Before Object P : 1020
-In written function
-Does nothing in code
After Pointer p1: 1020
After Object P : 1020
Do let me know if you have any comments.
This is because you are doing a using namespace std, which I believe makes the code use std::swap, rather than the swap you defined.
Try this:
- Loler March 29, 2013