Thread: General Preprocessor Advice

  1. #1
    Old Fashioned
    Join Date
    Nov 2016

    General Preprocessor Advice

    I have a strange relationship with the C preprocessor. Namely, I don't quite understand when I would want to use it over regular language constructs. I do use it, but it's usually very basic macros like #define SOME_NUMBER 51 or #define MEMORY_READ 0x8001, or the #ifndef type stuff for the header files.

    However, I do see some C programmers using it A LOT throughout the code. Common things that I see if #ifdef _W32 #endif in the middle of code, or stuff like this:

    #define ZeroStruct(Instance) ZeroSize(sizeof(Instance), &(Instance))#define ZeroArray(Count, Pointer) ZeroSize(Count*sizeof((Pointer)[0]), Pointer)inline voidZeroSize(memory_index Size, void *Ptr){    uint8 *Byte = (uint8 *)Ptr;    while(Size--)    {        *Byte++ = 0;    }}
    The question is why would a programmer choose to do this over avoiding the usage of the preprocessor? Is it "bad practice" to inline a lot of preprocessor stuff in C? Any general guidance would be appreciated. Thanks.
    If I was homeless and jobless, I would take my laptop to a wifi source and write C for fun all day. It's the same thing I enjoy now!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    That looked like one huge macro, but it turns out that the code in your post had some formatting issues so it is actually:
    #define ZeroStruct(Instance) ZeroSize(sizeof(Instance), &(Instance))
    #define ZeroArray(Count, Pointer) ZeroSize(Count * sizeof((Pointer)[0]), Pointer)
    inline void ZeroSize(memory_index Size, void *Ptr)
        uint8 *Byte = (uint8 *)Ptr;
        while (Size--)
            *Byte++ = 0;
    It is easy to understand why ZeroStruct is a function-style macro: it is supposed to work with arguments of different types, and hence if you don't want to repeat writing the same function for each struct type, you would need to have a void* parameter for the function, in which case you would have to pass the size too, upon which you might as well call ZeroSize directly. Hence, a function-style macro allows the "caller" to pass the struct object directly, regardless of its type.

    ZeroArray has a similiar idea: here, Pointer can point to the first element of arrays of varying element types. If you wish to write it as a function, it would be something like:
    void ZeroArray(size_t Count, void *Pointer, size_t ElementSize)
        ZeroSize(Count * ElementSize, Pointer);
    and you would call it like this:
    ZeroArray(100, SomeArray, sizeof(SomeArray[0]));
    contrast with the macro, where you don't have to pass the element size because the macro would be replaced with code in the context of the macro invocation, hence the element size can be determined from the sizeof within the macro:
    ZeroArray(100, SomeArray);
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. General Advice
    By ph5 in forum C Programming
    Replies: 3
    Last Post: 02-23-2011, 04:16 AM
  2. Programming - general advice
    By progcomputeach in forum Projects and Job Recruitment
    Replies: 8
    Last Post: 08-03-2010, 12:16 PM
  3. Fortran programmer new to c++. Looking for general advice.
    By Duff-Man in forum C++ Programming
    Replies: 12
    Last Post: 10-14-2009, 12:33 AM
  4. General Advice (RTS Game)
    By b00l34n in forum Game Programming
    Replies: 2
    Last Post: 05-07-2005, 12:34 PM
  5. general advice on jobs
    By Unregistered in forum A Brief History of
    Replies: 3
    Last Post: 07-25-2002, 08:36 PM

Tags for this Thread