S
Stanley Rice
Hello all
I wrote a "generic" swap function, which can swap some type, such as
int, double, long. But I don't know how to evaluate this function, and
don't know whether it's a good idea to use such a function. I hope you
can give me some suggestions.
void swap(void *left, void *right, size_t siz)
{
char *buf;
buf = (char *)malloc(sizeof(*buf) * siz);
memcpy(buf, left, siz);
memcpy(left, right, siz);
memcpy(right, buf, siz);
free(buf);
}
call this function in the following ways:
int a = 5;
int b = 4;
swap(&a, &b, sizeof(a));
Thanks.
I wrote a "generic" swap function, which can swap some type, such as
int, double, long. But I don't know how to evaluate this function, and
don't know whether it's a good idea to use such a function. I hope you
can give me some suggestions.
void swap(void *left, void *right, size_t siz)
{
char *buf;
buf = (char *)malloc(sizeof(*buf) * siz);
memcpy(buf, left, siz);
memcpy(left, right, siz);
memcpy(right, buf, siz);
free(buf);
}
call this function in the following ways:
int a = 5;
int b = 4;
swap(&a, &b, sizeof(a));
Thanks.