Adobe Interview Question
Software Engineer / DevelopersI disagree with #1, because you can easily get around with name clash by defining a scope using '{' and '}', e.g.,
#define SWAP(a,b,type) { type tmp=(a); (a)=(b); (b)=tmp; }
w.r.t, #2 you can let the user pass in the type as shown above.
This code works as least well for most types in C, except when you want deep copy with pointers, which is another story BTW.
General needs a definition; this might be a class object as well.
Use templates if that's true.
else if normal built in datatypes that XOR is fine.
#define SWAP(a,b) {a=a^b;b = a^b;a= a^b;}
Akash
tech-queries.blogspot.com
Hi All,
The idea here is to pass the parameters as void parameters as well as the size of the data type that needs to be swapped. Then we need to use memcpy with a temporary void pointer. here is how a code might look like.
void swap(void* a, void* b, int size){
void * temp =NULL;
memcpy(temp, a, size); //copy size number of bytes starting from a to temp
memcpy(a, b, size); //copy size number of bytes starting from b to
memcpy(b, temp, size); //copy size number of bytes starting from temp to b
}
Please let me know if this is correct or needs some manipulations.
your answer seems very effective...but we had to write a macro if suppose i write this as macro then will this be okay??
#define SWAP(x,y) { void *temp=null; void *a; void *b; a=&x; b=&y; memcpy(temp,a,sizeof(x)); memcpy(a,b,sizeof(y)); memcpy(b,temp,sizeof(x));}
wondering how you are able to copy to a NULL pointer.this solution will result in seg. fault.
# define swap(x,y) /
{ void *_tmp=malloc(sizeof(x)); /
void *_x=&x; void *_y=&y; /
memcpy(tmp,_x,sizeof(x));/
memcpy(_x,_y,sizeof(x));/
memcpy(_y,_tmp,sizeof(x));}
and in GCC we can do :
#define swap(x,y) {typeof(x) _tmp=x,x=y,y=_tmp}
#include <stdio.h>
#define SWAP(x, y) do { typeof(x) temp = x; x = y; y = temp; } while (0)
int main(void)
{
int a = 10000, b = 2;
float x = 99999.0f, y = 2.0f;
int *pa = &a;
int *pb = &b;
printf("BEFORE:\n");
printf("a = %d, b = %d\n", a, b);
printf("x = %f, y = %f\n", x, y);
printf("pa = %p, pb = %p\n", pa, pb);
SWAP(a, b); // swap ints
SWAP(x, y); // swap floats
SWAP(pa, pb); // swap pointers
printf("AFTER:\n");
printf("a = %d, b = %d\n", a, b);
printf("x = %f, y = %f\n", x, y);
printf("pa = %p, pb = %p\n", pa, pb);
return 0;
}
Hope this code is easily understandable and works for all possible cases...
Here is the code form swapping using generic macro...pertaining to this qs all the scepticism shown by many is disapproved.
#include<stdio.h>
#define swap(A,B) A=A+B;B=A-B;A=A-B;
void main()
{
char a='c',b='d';
//int p=5,q=10;
//swap(p,q);
//float r=5.0,s=10.5;
//swap(r,s);
//printf("p=%d\tq=%d\n",p,q);
//printf("r=%f\ts=%f\n",r,s);
//swap(a,b);
//printf("a=%c\tb=%c\n",a,b);
char *ptr1,*ptr2;
ptr1=&a;ptr2=&b;
swap(*ptr1,*ptr2);
printf("a=%c\tb=%c\n",*ptr1,*ptr2);
}
There is no good answer to this question. If the values are integers, a well-known trick using exclusive-OR could perhaps be used, but it will not work for floating-point values or pointers, or if the two values are the same variable. If the macro is intended to be used on values of arbitrary type (the usual goal), any solution involving a temporary variable is problematical, because:
- Anonymous September 17, 2010* It's hard to give the temporary a name that won't clash with anything. (Any name you pick might be the actual name of one of the variables being swapped. If you tried using ## to concatenate the names of the two actual arguments, to ensure that it won't match either one, it might still not be unique if the concatenated name is longer than 31 characters, and it wouldn't let you swap things like a[i] that aren't simple identifiers. You could probably get away with using a name like _tmp in the ``no man's land'' between the user and implementation namespaces;
* Either it can't be declared with the right type (because standard C does not provide a typeof operator), or (if it copies objects byte-by-byte, perhaps with memcpy, to a temporary array sized with sizeof) the macro can't be used on operands which are declared register.
The best all-around solution is probably to forget about using a macro, unless you're willing to pass in the type as a third argument. (Also, if you're trying to swap entire structures or arrays, you probably want to exchange pointers instead.) If you're worried about the use of an ugly temporary, and know that your machine provides an efficient exchange instruction, convince your compiler vendor to recognize the standard three-assignment swap idiom in the optimization phase.
If you're consumed by a passionate desire to solve this problem once and for all, please reconsider; there are better problems worthier of your energies.