K
Kevin D. Quitt
I'm at a new job, with a funky coding standard, so don't blame me for that. I'm
also being looked at as the resident C expert after making some coding
recommendations (like do {...}while (0) for multi-line macros).
I was approached on how to swap the bytes (*sigh*) in a short, using a macro.
This particular compiler does not have the NTOH.. macro set, and they want to
implement them, sort of: they want to pass a pointer to what gets swapped. So
here's what I wrote (where UCHAR and USHORT are the expected typedefs):
#define SWAP_BYTES_AT_POINTER(bp) \
do { \
UCHAR c, *p; \
if ((UCHAR *)bp) { \
p = (UCHAR *)bp; \
c = p[1]; \
p[1] = p[0]; \
p[0] = c; \
} \
} while (0)
They were delighted by this and asked for a macro that implement (their way)
NTOHL; I gave them this:
#define SWAP_SHORTS_AT_POINTER(sp) \
do { \
USHORT c, *p; \
if ((USHORT *)sp) { \
p = (USHORT *)bp; \
c = p[1]; \
p[1] = p[0]; \
p[0] = c; \
SWAP_BYTES_AT_POINTER(p); \
SWAP_BYTES_AT_POINTER(p+1); \
} \
} while (0)
They weren't delighted by this because it segfaults at the first invocation of
SWAP_BYTES_AT_POINTER. On a whim, I changed the names of the variables in
SWAP_SHORTS_AT_POINTER, and the segfault went away. The original also blows up
under Windows, using Visual Studio, so I am led to believe that it isn't a
compiler problem.
I can't see why there's a problem. The variables are local in scope and
shouldn't interfere - right? What is the problem?
And for those out there who insist on seeing the work-around, here it is:
#define SWAP_SHORTS_AT_POINTER(sp) \
do { \
UCHAR c, *p; \
if ((UCHAR *)bp) { \
p = (UCHAR *)bp; \
c = p[0]; \
p[0] = p[3]; \
p[3] = c; \
c = p[1]; \
p[1] = p[2]; \
p[2] = c; \
} \
} while (0)
also being looked at as the resident C expert after making some coding
recommendations (like do {...}while (0) for multi-line macros).
I was approached on how to swap the bytes (*sigh*) in a short, using a macro.
This particular compiler does not have the NTOH.. macro set, and they want to
implement them, sort of: they want to pass a pointer to what gets swapped. So
here's what I wrote (where UCHAR and USHORT are the expected typedefs):
#define SWAP_BYTES_AT_POINTER(bp) \
do { \
UCHAR c, *p; \
if ((UCHAR *)bp) { \
p = (UCHAR *)bp; \
c = p[1]; \
p[1] = p[0]; \
p[0] = c; \
} \
} while (0)
They were delighted by this and asked for a macro that implement (their way)
NTOHL; I gave them this:
#define SWAP_SHORTS_AT_POINTER(sp) \
do { \
USHORT c, *p; \
if ((USHORT *)sp) { \
p = (USHORT *)bp; \
c = p[1]; \
p[1] = p[0]; \
p[0] = c; \
SWAP_BYTES_AT_POINTER(p); \
SWAP_BYTES_AT_POINTER(p+1); \
} \
} while (0)
They weren't delighted by this because it segfaults at the first invocation of
SWAP_BYTES_AT_POINTER. On a whim, I changed the names of the variables in
SWAP_SHORTS_AT_POINTER, and the segfault went away. The original also blows up
under Windows, using Visual Studio, so I am led to believe that it isn't a
compiler problem.
I can't see why there's a problem. The variables are local in scope and
shouldn't interfere - right? What is the problem?
And for those out there who insist on seeing the work-around, here it is:
#define SWAP_SHORTS_AT_POINTER(sp) \
do { \
UCHAR c, *p; \
if ((UCHAR *)bp) { \
p = (UCHAR *)bp; \
c = p[0]; \
p[0] = p[3]; \
p[3] = c; \
c = p[1]; \
p[1] = p[2]; \
p[2] = c; \
} \
} while (0)