Thread: Guess the code!

  1. #76
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    An easy one:

    Code:
    int a[] = {11,12,13,14,15};
    int* ap = a - 1;
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  2. #77
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    That's just evil.
    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.*

  3. #78
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    And yet that's correct
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  4. #79
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Are you absolutely sure? The standard is far more precise about what operations are allowed on pointers and when than what any implementation actually enforces.

    Hmm, according to the C faq link it's not correct.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #80
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    I meant Dave was correct on how he interpreted the code. It's a way to implement a unit-based array.

    EDIT: I was actually unware at the fact that despite there existing no dereferencing it could still produce undefined behavior. But Dave hit it on the nail at the code usage.
    Last edited by Mario F.; 11-11-2006 at 01:01 PM.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  6. #81
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Quote Originally Posted by Mario F.
    An easy one:

    Code:
    int a[] = {11,12,13,14,15};
    int* ap = a - 1;
    But is there actually systems that behaves differenty than expected, trying to "wrap around" for example?

  7. #82
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    > But is there actually systems that behaves differenty than expected, trying to "wrap around" for example?

    Some people don't like to admit they were wrong, apparently.

    While the link provided by Dave explains why it is undefined behavior, the following link from the Numerical Recipes book website shows an extract of the new Numerical Recipes for C++ book. Take a look at page 10. It's worth your time... although probably make you mad.

    http://www.numerical-recipes.com/cpppages/chap1sel.pdf
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  8. #83
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    For fun:
    Code:
    void foo(unsigned int value)
    {
       unsigned int bit;
       for ( bit = /* msb */(~0U >> 1) + 1; bit > 0; bit >>= 1 )
       {
          putchar("01"[!!(value & bit)]);
       }
       putchar('\n');
    }
    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.*

  9. #84
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Ain't that just a base 2 itoa, printing directly?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  10. #85
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Mario F: I think you missunderstod me, I was just being curious.
    Last edited by TriKri; 11-11-2006 at 06:33 PM.

  11. #86
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Ireland
    Posts
    8,446
    Quote Originally Posted by TriKri
    Mario F: I think you missunderstod me, I was just being curious.
    Oh. I have. Sorry. I read that as a rethorical question.
    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  12. #87
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by CornedBee
    Ain't that just a base 2 itoa, printing directly?
    Yeah. I was kind of shooting for something along the line of, "the !!(value & bit) results in a one or a zero if the selected bit is set in value or not, so you can safely index into the string literal", or something.
    Code:
    putchar("01"[!!(value & bit)]);
    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.*

  13. #88
    Registered User
    Join Date
    Jun 2004
    Posts
    201
    Quote Originally Posted by Dave_Sinkula
    Yeah. I was kind of shooting for something along the line of, "the !!(value & bit) results in a one or a zero if the selected bit is set in value or not, so you can safely index into the string literal" [/code]
    what if you'd do (value & bit) != 0 ? isnt that either 0 or 1 ?

  14. #89
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    But!! isn't!! double!! exclaimation!! so!! leet!! ??!!
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  15. #90
    MFC killed my cat! manutd's Avatar
    Join Date
    Sep 2006
    Location
    Boston, Massachusetts
    Posts
    870
    Ah! Not the leet thing again!
    Silence is better than unmeaning words.
    - Pythagoras
    My blog

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Proposal: Code colouring
    By Perspective in forum A Brief History of Cprogramming.com
    Replies: 28
    Last Post: 05-14-2007, 07:23 AM
  2. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 04:17 PM
  3. banking2
    By sweet2awy in forum C++ Programming
    Replies: 7
    Last Post: 08-01-2003, 03:01 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 0
    Last Post: 02-21-2002, 06:05 PM