Your code works – as far as I can see – correctly now. But I would rewrite it a bit to improve the readability (and with it the maintainability) of the code.
FirstThe function name reverse()
is quite general. That could refer
to reversing an array, thestring, or whatever. A better name might be
reverseArray()
.
The repeated cast (char*)object
can be avoided by defining
a variable
void reversereverseArray(void *objects, int size, int length) {
char *ptr = objects;
for (int i = 0, j = length - 1; i < j; i++, j--) {
swapObjects(ptr + size * i, ptr + size * j, size);
}
}
Your code works – as far as I can see – correctly now. But I would rewrite it to improve the readability (and with it the maintainability) of the code.
First, the repeated cast (char*)object
can be avoided by defining
a variable
void reverse(void *objects, int size, int length) {
char *ptr = objects;
for (int i = 0, j = length - 1; i < j; i++, j--) {
swapObjects(ptr + size * i, ptr + size * j, size);
}
}
Your code works – as far as I can see – correctly. But I would rewrite it a bit to improve the readability (and with it the maintainability) of the code.
The function name reverse()
is quite general. That could refer
to reversing an array, string, or whatever. A better name might be
reverseArray()
.
The repeated cast (char*)object
can be avoided by defining
a variable
void reverseArray(void *objects, int size, int length) {
char *ptr = objects;
for (int i = 0, j = length - 1; i < j; i++, j--) {
swapObjects(ptr + size * i, ptr + size * j, size);
}
}
Using youryour method, swapObjects()
would be:
static inline void swapObjects(char *o1, char *o2, int size) {
for (int ck = 0; ck < size; c++, o1++, o2++k++) {
*o1o1[k] = *o1o1[k] ^ *o2;o2[k];
*o2o2[k] = *o1o1[k] ^ *o2;o2[k];
*o1o1[k] = *o1o1[k] ^ *o2;o2[k];
}
}
static inline void swapObjects(char *o1, char *o2, int size) {
for (int ck = 0; ck < size; c++, o1++, o2++k++) {
char tmp = *o1;o1[k];
*o1o1[k] = *o2;o2[k];
*o2o2[k] = tmp;
}
}
which is much simpler (no XOR operations and less memory reads and
writes). (ItIt is possible to make the swap operation faster by using
int
or long
as temporary storage, but this requires a careful
check of the sizes and memory alignments.)
Here you can get rid of the subcripts by increasing the passed pointers
static inline void swapObjects(char *o1, char *o2, int size) {
for (int k = 0; k < size; k++, o1++, o2++) {
char tmp = *o1;
*o1 = *o2;
*o2 = tmp;
}
}
but that is a matter of taste. I don't think that it makes a difference if you compile the code with optimizations switched on.
Using your method, swapObjects()
would be:
static inline void swapObjects(char *o1, char *o2, int size) {
for (int c = 0; c < size; c++, o1++, o2++) {
*o1 = *o1 ^ *o2;
*o2 = *o1 ^ *o2;
*o1 = *o1 ^ *o2;
}
}
static inline void swapObjects(char *o1, char *o2, int size) {
for (int c = 0; c < size; c++, o1++, o2++) {
char tmp = *o1;
*o1 = *o2;
*o2 = tmp;
}
}
which is much simpler (no XOR operations and less memory reads and
writes). (It is possible to make the swap operation faster by using
int
or long
as temporary storage, but this requires a careful
check of the sizes and memory alignments.)
Using your method, swapObjects()
would be:
static inline void swapObjects(char *o1, char *o2, int size) {
for (int k = 0; k < size; k++) {
o1[k] = o1[k] ^ o2[k];
o2[k] = o1[k] ^ o2[k];
o1[k] = o1[k] ^ o2[k];
}
}
static inline void swapObjects(char *o1, char *o2, int size) {
for (int k = 0; k < size; k++) {
char tmp = o1[k];
o1[k] = o2[k];
o2[k] = tmp;
}
}
which is much simpler (no XOR operations and less memory reads and
writes). It is possible to make the swap operation faster by using
int
or long
as temporary storage, but this requires a careful
check of the sizes and memory alignments.
Here you can get rid of the subcripts by increasing the passed pointers
static inline void swapObjects(char *o1, char *o2, int size) {
for (int k = 0; k < size; k++, o1++, o2++) {
char tmp = *o1;
*o1 = *o2;
*o2 = tmp;
}
}
but that is a matter of taste. I don't think that it makes a difference if you compile the code with optimizations switched on.
Your code works – as far as I can see – correctly now. But I would rewrite it to improve the readability (and with it the maintainability) of the code.
First, the repeated cast (char*)object
can be avoided by defining
a variable
char *ptr = objects;
once. Next, the address calculations can be simplified by separating the swapping of two objects into a helper function:
void reverse(void *objects, int size, int length) {
char *ptr = objects;
for (int i = 0, j = length - 1; i < j; i++, j--) {
swapObjects(ptr + size * i, ptr + size * j, size);
}
}
whereNote also how i
and j
are declared locally to the for-loop.
Using your method, swapObjects()
iswould be:
static inline void swapObjects(char *o1, char *o2, int size) {
for (int c = 0; c < size; c++, o1++, o2++) {
*o1 = *o1 ^ *o2;
*o2 = *o1 ^ *o2;
*o1 = *o1 ^ *o2;
}
}
However, I can see no advantage in this "magically swap two bytes without temporary storage" and would rewrite this simplyit as
static inline void swapObjects(char *o1, char *o2, int size) {
for (int c = 0; c < size; c++, o1++, o2++) {
char tmp = *o1;
*o1 = *o2;
*o2 = tmp;
}
}
which is much simpler (no XOR operations and less memory reads and
writes). (It is possible to make the swap operation faster by using
int
or long
as temporary storage, but this requires a careful
check of the sizes and memory alignments.)
In addition, I would use size_t
as type for the size
and length
parameters. size_t
is what sizeof
returns, and is the correct type to describe the size
of an object in memory, see for example the declarations
of malloc()
and, calloc()
or strlen()
.
Your code works – as far as I can see – correctly now. But I would rewrite it to improve the readability (and with it the maintainability) of the code.
First, the repeated cast (char*)object
can be avoided by defining
a variable
char *ptr = objects;
once. Next, the address calculations can be simplified by separating the swapping of two objects into a helper function:
void reverse(void *objects, int size, int length) {
char *ptr = objects;
for (int i = 0, j = length - 1; i < j; i++, j--) {
swapObjects(ptr + size * i, ptr + size * j, size);
}
}
where swapObjects()
is:
static inline void swapObjects(char *o1, char *o2, int size) {
for (int c = 0; c < size; c++, o1++, o2++) {
*o1 = *o1 ^ *o2;
*o2 = *o1 ^ *o2;
*o1 = *o1 ^ *o2;
}
}
However, I can see no advantage in this "magically swap two bytes without temporary storage" and would rewrite this simply as
static inline void swapObjects(char *o1, char *o2, int size) {
for (int c = 0; c < size; c++, o1++, o2++) {
char tmp = *o1;
*o1 = *o2;
*o2 = tmp;
}
}
which is much simpler (no XOR operations and less memory reads and
writes). (It is possible to make the swap operation faster by using
int
or long
as temporary storage, but this requires a careful
check of the sizes and memory alignments.)
In addition, I would use size_t
as type for the size
and length
parameters. size_t
is the correct type to describe the size
of an object in memory, see for example the declarations
of malloc()
and calloc()
.
Your code works – as far as I can see – correctly now. But I would rewrite it to improve the readability (and with it the maintainability) of the code.
First, the repeated cast (char*)object
can be avoided by defining
a variable
char *ptr = objects;
once. Next, the address calculations can be simplified by separating the swapping of two objects into a helper function:
void reverse(void *objects, int size, int length) {
char *ptr = objects;
for (int i = 0, j = length - 1; i < j; i++, j--) {
swapObjects(ptr + size * i, ptr + size * j, size);
}
}
Note also how i
and j
are declared locally to the for-loop.
Using your method, swapObjects()
would be:
static inline void swapObjects(char *o1, char *o2, int size) {
for (int c = 0; c < size; c++, o1++, o2++) {
*o1 = *o1 ^ *o2;
*o2 = *o1 ^ *o2;
*o1 = *o1 ^ *o2;
}
}
However, I can see no advantage in this "magically swap two bytes without temporary storage" and would rewrite it as
static inline void swapObjects(char *o1, char *o2, int size) {
for (int c = 0; c < size; c++, o1++, o2++) {
char tmp = *o1;
*o1 = *o2;
*o2 = tmp;
}
}
which is much simpler (no XOR operations and less memory reads and
writes). (It is possible to make the swap operation faster by using
int
or long
as temporary storage, but this requires a careful
check of the sizes and memory alignments.)
In addition, I would use size_t
as type for the size
and length
parameters. size_t
is what sizeof
returns, and is the correct type to describe the size
of an object in memory, see for example the declarations
of malloc()
, calloc()
or strlen()
.