Glenn Maynard wrote:
#ifdef SDL_IS_LIL_ENDIAN
value = *((Uint16 *) byte_ptr);
#else
value = byte_ptr[0] | (byte_ptr[1] << 8);
#endif
At a second reading, I don’t understand the purpose of the above unless
SDL_IS_LIL_ENDIAN should read as SDL_IS_BIG_ENDIAN.
SDL_BYTEORDER, SDL_LIL_ENDIAN and SDL_BIG_ENDIAN are compile-time
constants. “if(SDL_BYTEORDER == SDL_LIL_ENDIAN)” becomes
"if(1234 == 1234)" or “if(1234 == 4321)”. If it’s false, the unused
code won’t be included at all (with a sane compiler), and there is
no conditional at all.
Well, if you use preprocessor statements, the compiler can be as simple
as possible because it won’t even see the code which was already
thrown away by the preprocessor.
#include <stdio.h>
main()
{
if( 1 == 0 )
printf(“hello world\n”);
}
01:23am glenn at zewt/3 [~] g++ foobar.cc
It’s seldom a good idea to compile C code with a C++ compiler. There are
enough corner cases where the same code has different semantics.
Harness? You seem to think inline preprocessor directives are a good
thing. They’re not!
As long as people know what they’re doing, you can’t blame them.
Clean code has as few #ifs in the source as possible; they’re ugly and
cumbersome.
There’s no benefit of replacing all #if's with
if’s. Preprocessor
directives are only bad if they tear the C code too much apart. In
that case it’s much better to write a complete function or even a
module for different cases or just restructure the code.
Once you understand compiler optimizations, they’re very rarely needed,
especially with C++'s templates (“min” and “max” should not be macros!).
C99 has inline and if you can accept a C99 compiler as requirement, you
can, of course, use this feature to replace macros by inline functions
for the benefit of type checking. Unfortunately, GCC introduced inline
for C before C99 and therefore both have different semantics, unless
you use -std=c99 for GCC 3.x.
Last but not least, macros shall always be in all-caps (MIN, MAX) to
remind you bewaring side-effects unless it doesn’t matter whether
it’s a macro or not (macros don’t have addresses, of course) like
#define MAGIC_NUMBER 0xDEADBEEFU
In this case it’s much better to use `enum’ though because you’ll
see the symbolic values in the debugger instead of the raw numbers
and it also allows a stronger type system if you want to.
The nice thing about the MIN and MAX macros is, that it doesn’t
matter whether you use it with addresses, ints, floats etc. and you
only have to write them once. You can do that with C++, you can’t
do it with Java (unless you use a pre-processor which is quite possible).> On Fri, Jan 16, 2004 at 04:00:16PM -0600, Jason Hoffoss wrote:
–
Christian
-------------- next part --------------
A non-text attachment was scrubbed…
Name: not available
Type: application/pgp-signature
Size: 186 bytes
Desc: not available
URL: http://lists.libsdl.org/pipermail/sdl-libsdl.org/attachments/20040122/add40bc5/attachment.pgp