A
Army1987
Ian Collins said:Have you tried to compile this?
Or this?
No... whoops...
Ian Collins said:Have you tried to compile this?
Or this?
It was new to all of us, once. Those of us who understand programming
quickly learnt that such schoolboy tricks are not worth looking for.
Vishesh said:a = a+b
b = a-b;
a = a-b;
if (sizeof *a != sizeof *b) {
No, you can't take the sizeof void.Daniel said:At about the time of 4/5/2007 4:10 AM, Army1987 stated the following:
I think you mean sizeof(*a) and sizeof(*b), but does that construct even
work for a void *?
Daniel said:To the OP:
#define MAXSWAP 32 /* maximum size of variable swap */
int swap(void *a, void *b, int size)
{
char temp[MAXSWAP]; /* temp buffer */
if (size > MAXSWAP) return(-1);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return(0);
}
Now what's wrong with that?
Or even in modern C:
void swap( void *a, void *b, size_t size )
{
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
Define better!Daniel said:At about the time of 4/5/2007 12:20 AM, Ian Collins stated the following:
Daniel said:To the OP:
#define MAXSWAP 32 /* maximum size of variable swap */
int swap(void *a, void *b, int size)
{
char temp[MAXSWAP]; /* temp buffer */
if (size > MAXSWAP) return(-1);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return(0);
}
Now what's wrong with that?
Or even in modern C:
void swap( void *a, void *b, size_t size )
{
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
I just came up with one even better....
int swap(void *a, void *b, size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) return(-1);
memcpy(ptr, a, size);
memcpy(a, b, size);
memcpy(b, ptr, size);
free(ptr);
return(0);
As would mine.}
That will swap *ANY* 2 data types of the same type, structs included.
Define better!Daniel said:At about the time of 4/5/2007 12:20 AM, Ian Collins stated the following:
I just came up with one even better....Daniel Rudy wrote:
To the OP:
#define MAXSWAP 32 /* maximum size of variable swap */
int swap(void *a, void *b, int size)
{
char temp[MAXSWAP]; /* temp buffer */
if (size > MAXSWAP) return(-1);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return(0);
}
Now what's wrong with that?
Or even in modern C:
void swap( void *a, void *b, size_t size )
{
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
int swap(void *a, void *b, size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) return(-1);
memcpy(ptr, a, size);
memcpy(a, b, size);
memcpy(b, ptr, size);
free(ptr);
return(0);
return isn't a function. Why not make the function void?
As would mine.
At about the time of 4/5/2007 4:10 AM, Army1987 stated the following:
I think you mean sizeof(*a) and sizeof(*b)
(Why(?))
, but does that construct
even work for a void *?
I just came up with one even better....
int swap(void *a, void *b, size_t size)
{
void *ptr;
ptr = malloc(size);
if (ptr == NULL) return(-1);
memcpy(ptr, a, size);
memcpy(a, b, size);
memcpy(b, ptr, size);
free(ptr);
return(0);
}
That will swap *ANY* 2 data types of the same type, structs included.
viv342 said:
Pointlessly.
Temps are cheap, and the resulting code will be quicker and easier to
read than a hack. And, in any case, the hack for which you are
stretching only works for particular kinds of variable under particular
conditions.
Also interesting is what Sun cc does with main if I add:Dave said:Temps are cheap, and the resulting code will be quicker and easier to
read than a hack. And, in any case, the hack for which you are
stretching only works for particular kinds of variable under particular
conditions.
Alternatively, write the code in a way that makes sense and use an
optimizing compiler:
--------
dave@goofy:~/clc (0) $ cat swap.c
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
dave@goofy:~/clc (0) $ gcc -W -Wall -ansi -pedantic -O -S swap.c
dave@goofy:~/clc (0) $ cat swap.s
[Some framing removed, this is the generated code for the actual function
in its entirety]
swap:
pushl %ebp
movl %esp, %ebp
pushl %ebx
movl 8(%ebp), %edx
movl 12(%ebp), %ecx
movl (%edx), %ebx
movl (%ecx), %eax
movl %eax, (%edx)
movl %ebx, (%ecx)
movl (%esp), %ebx
leave
ret
Daniel said:To the OP:#define MAXSWAP 32 /* maximum size of variable swap */
int swap(void *a, void *b, int size)
{
char temp[MAXSWAP]; /* temp buffer */if (size > MAXSWAP) return(-1);
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
return(0);
}Now what's wrong with that?
Or even in modern C:
void swap( void *a, void *b, size_t size )
{
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
Daniel Rudy said:
Well, it might. Or it might not.
This is a classic case of a situation where malloc can fail and yet you
can recover and perform your task properly, albeit in a slightly
clumsier way than would be possible with the successful malloc call. To
return an error is a mistake, I think.
At about the time of 4/5/2007 2:05 PM, Ian Collins stated the following:
It needs to return an error indicator to the caller if malloc fails?
viv342 said:
Pointlessly.
Temps are cheap, and the resulting code will be quicker and easier to
read than a hack. And, in any case, the hack for which you are
stretching only works for particular kinds of variable under particular
conditions.
Alternatively, write the code in a way that makes sense and use an
optimizing compiler:
--------
dave@goofy:~/clc (0) $ cat swap.c
void swap(int *a,int *b)
{
int t=*a;
*a=*b;
*b=t;
}
dave@goofy:~/clc (0) $ gcc -W -Wall -ansi -pedantic -O -S swap.c
dave@goofy:~/clc (0) $ cat swap.s
[Some framing removed, this is the generated code for the actual function
in its entirety]
swap:
pushl %ebp
movl %esp, %ebp
pushl %ebx
movl 8(%ebp), %edx
movl 12(%ebp), %ecx
movl (%edx), %ebx
movl (%ecx), %eax
movl %eax, (%edx)
movl %ebx, (%ecx)
movl (%esp), %ebx
leave
ret
Daniel Rudy said:At about the time of 4/5/2007 4:10 AM, Army1987 stated the following:
I think you mean sizeof(*a) and sizeof(*b), but does that construct even
work for a void *?
Coos Haak said:Op Thu, 05 Apr 2007 22:08:12 GMT schreef Daniel Rudy:
Perhaps, but you haven't answered why do you think return is a function?!
user923005 said:Or even in modern C:
void swap( void *a, void *b, size_t size )
{
char temp[size];
memcpy(temp, a, size);
memcpy(a, b, size);
memcpy(b, temp, size);
}
[...]Certainly, the most elegant C version (due to C99 semantics).
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.