C diamonds and perls :)

This is a discussion on C diamonds and perls :) within the A Brief History of Cprogramming.com forums, part of the Community Boards category; Please submit your own diamonds you've found while searching through "alien" source code. Here are some of what I've discovered ...

  1. #1
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463

    Talking C diamonds and perls :)

    Please submit your own diamonds you've found while searching through "alien" source code. Here are some of what I've discovered

    1.)
    Code:
    return -false;
    2.)
    Code:
    switch ( value ):
    {
    case CASE1: ...
    default : ...
    case CASE2: ...
    case CASE3: ...
    }
    ...to be continued :)

  2. #2
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Wow!
    Nice

    The author must have had a terrible headache or heartburn or both when he "invented" that switch.

    I'd never write such a switch, rather optimize it in assembly...

  3. #3
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,139
    When I rewrote a client-server system, I came across this little pearl:

    Code:
    BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
    {
    	//
    	// disable Windows-Style WS_MAXIMIZEBOX 
    	//
    	unsigned long ul = cs.style;
    
    	_asm 
                   {
    		push eax
    		push ebx
    		mov eax,65536
    		or  eax,0x40000
    		xor eax,0xffffffff
    		mov ebx,eax
    		mov eax,ul
    		and eax,ebx
    		mov ul,eax
    		pop ebx
    		pop eax
    	}
    
    	cs.style=ul;
    		
    	return CFrameWnd::PreCreateWindow(cs);
    }
    Is there anything worse than using assembler in a high level MFC/C++ program to do something totally not worth optimizing that could have been much easier to understand and about a dozen lines less code if it had been coded in simple C++ ?

    This code snippet was taken as a prime example for not using different languages in one file in our coding standards.
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  4. #4
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Originally posted by nvoigt

    This code snippet was taken as a prime example for not using different languages in one file in our coding standards.
    I do not prefer mixing Asm with C either, but I'd rather optimize something in Assembly, instead of using such switch-es (listing from the link presented by Salem):

    Code:
    switch(count%8)
    {
        case 0: 
        do
        {   
            *to = *from++;
            case 7: *to = *from++;
            case 6: *to = *from++;
            case 5: *to = *from++;
            case 4: *to = *from++;
            case 3: *to = *from++;
            case 2: *to = *from++;
            case 1: *to = *from++;
        } while( --n > 0 );
    }

  5. #5
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Check these out!

    Both of them will compile, the first one outputs a poem, the second one is just beautiful :D

    Code:
    #include <stdio.h>
    
    main(int t,int _,char *a)
    {
    return!0<t?t<3?main(-79,-13,a+main(-87,1-_,main(-86,0,a+1)+a)):
    1,t<_?main(t+1,_,a):3,main(-94,-27+t,a)&&t==2?_<13?
    main(2,_+1,"%s %d %d\n"):9:16:t<0?t<-72?main(_,t,
    "@n'+,#'/*{}w+/w#cdnr/+,{}r/*de}+,/*{*+,/w{%+,/w#q#n+,/#{l+,/n{n+,/+#n+,/#\
    ;#q#n+,/+k#;*+,/'r :'d*'3,}{w+K w'K:'+}e#';dq#'l \
    q#'+d'K#!/+k#;q#'r}eKK#}w'r}eKK{nl]'/#;#q#n'){)#}w'){){nl]'/+#n';d}rw'\n\
    i;# \
    ){nl]!/n{n#'; r{#w'r nc{nl]'/#{l,+'K {rw' iK{;[{nl]'/w#q#n'wk nw' \
    iwk{KK{nl]!/w{%'l##w#' i; :{nl]'/*{q#'ld;r'}{nlwb!/*de}'c \
    ;;{nl'-{}rw]'/+,}##'*}#nc,',#nw]'/+kd'+e}+;#'rdq#w! nr'/ ') }+}{rl#'{n'\n\
    ')#\n\
    \
    }'+}##(!!/")
    :t<-50?_==*a?putchar(31[a]):main(-65,_,a+1):main((*a=='/')+t,_,a+1)
    :0<t?main(2,2,"%s"):*a=='/'||main(0,main(-61,*a,
    "!ek;dc i@bK'(q)-[w]*%n+r3#l,{}:\nuwloca-O;m .vpbks,fxntdCeghiry"),a+1);
    }
    
    --------------------------------------------------------------------------------
    
                                    char rahc
                                       [ ]
                                        =
                                      "\n/"
                                        ,
                                    redivider
                                       [ ]
                                        =
                           "Able was I ere I saw elbA"
                                        ,
                                        *
                                 deliver,reviled
                                        =
                                       1+1
                                        ,
                                   niam ; main
                                       ( )
                                      {/*\}
                                       \*/
                                     int tni
                                        =
                                       0x0
                                        ,
                                 rahctup,putchar
                                       ( )
                               ,LACEDx0 = 0xDECAL,
                                    rof ; for
                                 (;(int) (tni);)
                                   (int) (tni)
                              = reviled ; deliver =
                                    redivider
                                        ;
    for ((int)(tni)++,++reviled;reviled* *deliver;deliver++,++(int)(tni)) rof
                                        =
                                 (int) -1- (tni)
                              ;reviled--;--deliver;
                                 (tni)  =  (int)
                              - 0xDECAL + LACEDx0 -
                                    rof ; for
           (reviled--,(int)--(tni);(int) (tni);(int)--(tni),--deliver)
                                rahctup = putchar
                               (reviled* *deliver)
                                        ;
                                rahctup * putchar
                                ((char) * (rahc))
                                        ;
                                       /*\
                                      {\*/}

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    Here are three that I like.
    Code:
    #include <stdio.h>
    #define WYSIWYG(x)   #x, (x)
    int main(void)
    {
       unsigned long mask, data = 0x12345678UL;
       printf("%s = %08lx\n", WYSIWYG(data));
       for(mask = (-1UL >> 1) + 1; mask; mask >>= 1)
       {
          printf("%s = %08lx, ", WYSIWYG(mask));
          printf("%s = %d\n",    WYSIWYG(!!(data & mask)));
       }
       return(0);
    }
    /* my output
    data = 12345678
    mask = 80000000, !!(data & mask) = 0
    mask = 40000000, !!(data & mask) = 0
    mask = 20000000, !!(data & mask) = 0
    mask = 10000000, !!(data & mask) = 1
    mask = 08000000, !!(data & mask) = 0
    mask = 04000000, !!(data & mask) = 0
    mask = 02000000, !!(data & mask) = 1
    mask = 01000000, !!(data & mask) = 0
    mask = 00800000, !!(data & mask) = 0
    mask = 00400000, !!(data & mask) = 0
    mask = 00200000, !!(data & mask) = 1
    mask = 00100000, !!(data & mask) = 1
    mask = 00080000, !!(data & mask) = 0
    mask = 00040000, !!(data & mask) = 1
    mask = 00020000, !!(data & mask) = 0
    mask = 00010000, !!(data & mask) = 0
    mask = 00008000, !!(data & mask) = 0
    mask = 00004000, !!(data & mask) = 1
    mask = 00002000, !!(data & mask) = 0
    mask = 00001000, !!(data & mask) = 1
    mask = 00000800, !!(data & mask) = 0
    mask = 00000400, !!(data & mask) = 1
    mask = 00000200, !!(data & mask) = 1
    mask = 00000100, !!(data & mask) = 0
    mask = 00000080, !!(data & mask) = 0
    mask = 00000040, !!(data & mask) = 1
    mask = 00000020, !!(data & mask) = 1
    mask = 00000010, !!(data & mask) = 1
    mask = 00000008, !!(data & mask) = 1
    mask = 00000004, !!(data & mask) = 0
    mask = 00000002, !!(data & mask) = 0
    mask = 00000001, !!(data & mask) = 0
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    The Pantless Man CheesyMoo's Avatar
    Join Date
    Jan 2003
    Posts
    262
    I'm sorry but, what the hell are you talking about?
    If you ever need a hug, just ask.

  8. #8
    Lipsish
    Guest

    Unhappy

    I don't think I'll ever understand this obfuscated code thing. How do people come up with this stuff?

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21