Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

Useful C Macros

Name: Anonymous 2010-01-12 15:24

Let's share the macros we use that make our C code really EXPERT!!

Right now I'm specifically interested in macros to add half-assed object orientation. Thanks!!

I'll contribute myself:
#define mainstart int main(int argc, char **argv) {

Name: Anonymous 2010-01-12 15:33

I wrote some macros to add OO "support" to C, it took about 1 page of C code. It's really simple stuff that anyone could do if they knew how vtables work, but I think that the vtable aproach is flawed by itself, all you need is just tagged structures and functions to operate on them. More interesting macros would be things like foreach or DOLIST. Too bad C macros are very limited (just text replacement) and can't do the things real macros that Lisp has (you have the full power of the environment when you write a macro, which makes writing compilers quite fun and easy).

Name: Anonymous 2010-01-12 16:31

Here's some excellent OO macros:
http://www.cs.indiana.edu/hyplan/dfried/ooo.pdf

Name: Anonymous 2010-01-12 16:40

#define niggers

Name: Anonymous 2010-01-12 16:47

>>3
Which part of Useful C Macros did you not understand?

Name: Anonymous 2010-01-12 16:48

>>5
What part of Useful C Macros can be understood?

Name: Anonymous 2010-01-12 17:11

>>6
s/what/which/si

Name: Anonymous 2010-01-12 17:12

>>5
The part where you claimed that a C Macro can be useful ;)

Name: Anonymous 2010-01-12 17:20

>>8
Please show where the OP claimed that a C Macro can be useful.

The OP was asking for proof that a C Macro can be useful.

Name: Anonymous 2010-01-12 17:28

>>6
In your case, it's clearly not the one where the s is bold.

Name: Anonymous 2010-01-12 19:37

#define 1 0

Name: Anonymous 2010-01-12 19:41

#define printf send_to_mother_china

Name: Anonymous 2010-01-12 22:33

#if DEBUG
#define log(...) fprintf(stderr, __VA_ARGS__)
#else
#define log(...) /* nothing */
#endif

Name: Anonymous 2010-01-12 22:35

>>2
do share

Name: Anonymous 2010-01-13 1:52

>>13
Purely awesome.

Name: Anonymous 2010-01-13 7:39

>>15
JUNIOR PROGRAMMER

Name: Anonymous 2010-01-13 8:35

>>13
This is what I usually use:
#ifdef DEBUG
#   undef DEBUG
#   define DEBUG(f, a...) { \
        fprintf(stderr, "Debug (%s, %d): %s:", \
            __FILE__, __LINE__, __func__); \
        fprintf(stderr, f, ## a); \
    }
#else
#   define DEBUG(...) /**/
#endif

Name: Anonymous 2010-01-13 9:00


#ifdef DEBUG
# define ASSERT(e)    ( ((e) || assert(__FILE__, __LINE__) )
#else
# define ASSERT(e)    ( __assume(e) )
#endif


Used as an example in the reference: http://msdn.microsoft.com/en-us/library/1b3fsfxw.aspx

Having played with __assume a little (assuming some crazy shit and looking at the assembly listing) I can't begin to imagine disastrous effects this EXPERT PRACTICE would produce in a real code. Truly, this is a sanity destroyer worth being put in the Necronomicon.

Name: Anonymous 2010-01-13 9:24

ASSUME MY ANUS

Name: Anonymous 2010-01-13 9:47

>>18
That's why, if you read the comment before that code listing, it explains that you should only use that if failing the assertion would already lead to undefined behaviour. So it's not any more dangerous if you use it correctly.

You could try defining both assert() and assume() which do the same in debug mode, but in release the former disappears and the latter __assume()s.

Name: Anonymous 2010-01-13 16:18

>>18
What kind of assumptions did the compiler make use of? I see their 'default unreachable' example, and I assume you can assume lack of pointer aliasing and the like.

Name: Anonymous 2010-01-13 18:31

>>21
restrict works better for assuming no pointer aliasing. It's well supported as an extension by many compilers, though most don't enable the C99 keyword by default. __restrict works in msvc, __restrict__ in everything else, and gcc supports both. In keeping with the topic, here's a preprocessor macro I use in my own code:

#if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L
  #if defined(_WIN32)
    #define restrict __restrict
  #else
    #define restrict __restrict__
  #endif
#endif

Name: Anonymous 2010-01-13 20:41


#if defined(_WIN32)
  #error LOL SWITCH TO LUNIX
#else

Name: Anonymous 2010-01-13 23:15

#define swap(a, b, type) do {type t = a; a = b; b = t;} while (0)

or, if you don't care about portability:

#define swap(a, b) do {typeof(a) t = a; a = b; b = t;} while (0)

Name: =+=*=F=R=O=Z=E=N==V=O=I=D=*=+= 2010-01-14 1:08

I personally use Notepad2 and avoid any "makefiles"(which i consider bloated and poor design)

I use one include file for everything.
#include "void.h" //handles all the common functions,#defines,#ifdefs and #includes

Anything which uses makefiles is defective Sepples-level shit.
All the C code posts on my blog are void.h merged with somefile.c

See: http://dis.4chan.org/read/prog/1250330533/10,12,18,21

Name: Anonymous 2010-01-14 1:54

>>24

Why the 'do while'? Wouldn't a new scope work just as well?

Name: Anonymous 2010-01-14 3:46

>>26
It's C BEST MACRO PRACTICES.

Name: Anonymous 2010-01-14 11:29

>>26
The 'do while' still needs a semicolon to end the statement, whereas a scope would not. This means that if(foo) swap(int, a, b); else bar(); will work with >24's solution, but not with a simple scope.

Name: Anonymous 2010-01-14 11:43

>>28
DO WHILE MY ANUS

Name: Anonymous 2010-01-14 11:50

done

Name: Anonymous 2010-01-14 11:57

Here is a useful c macro.
(define f
  (λ()(error "f")))


(define-syntax c
  (syntax-rules ()
    ((c) (f))
    ((c ?x) ?x)
    ((c ?x ?y)
     (let ((old-f f))
       ((call-with-current-continuation
         (lambda (cc)
           (set! f
                 (lambda ()
                   (set! f old-f)
                   (cc (lambda () ?y))))
           (lambda () ?x))))))
    ((c ?x ?rest ...)
     (c ?x (c ?rest ...)))))

(let ((x (c 1 2 3 4 5))
      (y (c 1 2 3 4 5))
      (z (c 1 2 3 4 5)))
  (if (= 14 (+ x y z))
      (list x y z)
      (c)))
=> (4 5 5)

Name: Anonymous 2010-01-14 13:10

>>31
That looks suspiciously like my call/cc example from the other thread.

Name: Anonymous 2011-02-03 0:54

Name: !tNxltIGE7Q 2011-05-04 10:47

test

Don't change these.
Name: Email:
Entire Thread Thread List