1. C++ / Говнокод #14534

    +45

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    for (int i=0; i<N-1; ++i) for (int j=0; j<N-1; ++j)
    		{
    			int& 
    				f1=fields[i  ][j  ].index,
    				f2=fields[i+1][j  ].index,
    				f3=fields[i  ][j+1].index,
    				f4=fields[i+1][j+1].index;
    			if (f1<=0 && f2<=0 && f3<=0 && f4<=0) 
    			{
    				f1=-1;
    				f2=-1;
    				f3=-1;
    				f4=-1; 
    			}				
    		}

    несколько минут пырился в код и не мог понять, где ошибка
    БЛЯДСКАЯ КРЕСТОПИДЕРАСТИЧНАЯ СИСТЕМА ИМЕНОВАНИЯ ТИПОВ ПЕРЕМЕННЫХ

    TarasB, 10 Февраля 2014

    Комментарии (46)
  2. C++ / Говнокод #14531

    +63

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    #include <iostream>
    using namespace std;
    
    int main() {
    	// your code goes here
    	float f = 267.0f;
    	unsigned char c = f;
    	cout << (int)c << endl;
    	return 0;
    }

    Кресты помогают обнаруживать ошибки в логике программы. За это Страуструпу респект и уважуха.

    http://ideone.com/V9rgSC

    LispGovno, 09 Февраля 2014

    Комментарии (112)
  3. C++ / Говнокод #14526

    +63

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    int main()
    {
    	server::CServer();
    	return 0;
    }
    
    class CServer {
    public:
        CServer()
        {
            SOCKET listen_sd = socket (AF_INET, SOCK_STREAM, 0);	  CHK_ERR(listen_sd, "socket");
            SET_NONBLOCK(listen_sd);
    
            struct sockaddr_in sa_serv;
            memset (&sa_serv, '\0', sizeof(sa_serv));
            sa_serv.sin_family      = AF_INET;
            sa_serv.sin_addr.s_addr = INADDR_ANY;
            sa_serv.sin_port        = htons (1111);          /* Server Port number */
    
            int err = ::bind(listen_sd, (struct sockaddr*) &sa_serv, sizeof (sa_serv));      CHK_ERR(err, "bind");
            
            err = listen (listen_sd, 5);            CHK_ERR(err, "listen");
    
            while(true)
            {
                    Sleep(1);
    
                    struct sockaddr_in sa_cli;  
                    size_t client_len = sizeof(sa_cli);
    #ifdef WIN32
                    const SOCKET sd = accept (listen_sd, (struct sockaddr*) &sa_cli, (int *)&client_len);
    #else
                    const SOCKET sd = accept (listen_sd, (struct sockaddr*) &sa_cli, &client_len);
    #endif  
                    Callback(sd);
            }
        }
    };

    http://habrahabr.ru/post/211853/

    Бесконечный цикл (event loop) в конструкторе.

    Опущены неинтересные строчки инициализации всякой фигни.

    Про Sleep вместо select/epoll/etc. я вовсе молчу.

    WGH, 08 Февраля 2014

    Комментарии (7)
  4. C++ / Говнокод #14525

    +56

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    #include <iostream>
    using namespace std;
    
    int main() {
    	int i = 5;
    	int* p1 = &i;
    	volatile int* p2 = &i;
    	cout << p1 << endl;
    	cout << p2 << endl;
    	return 0;
    }

    http://ideone.com/hpw4CB

    LispGovno, 08 Февраля 2014

    Комментарии (23)
  5. C++ / Говнокод #14519

    +51

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    #include <iostream>
    using namespace std;
    
    int main() {
        const int ci = 42;
        auto f = [ci]() mutable { std::cout << ++ci << '\n'; };
        f();
        return 0;
    }

    http://ideone.com/0P72sN
    А слона то я и не приметил.

    LispGovno, 07 Февраля 2014

    Комментарии (16)
  6. C++ / Говнокод #14504

    +42

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    57. 57
    58. 58
    59. 59
    60. 60
    61. 61
    62. 62
    63. 63
    64. 64
    65. 65
    66. 66
    67. 67
    68. 68
    69. 69
    70. 70
    71. 71
    72. 72
    73. 73
    74. 74
    75. 75
    76. 76
    77. 77
    78. 78
    79. 79
    80. 80
    81. 81
    82. 82
    83. 83
    84. 84
    85. 85
    86. 86
    87. 87
    88. 88
    89. 89
    90. 90
    91. 91
    92. 92
    93. 93
    94. 94
    #include <time.h>
    #include <string>
    #include <iostream>
    #include <functional>
    
    using namespace std::placeholders;
    
    class F
    {
            int inc;
    
    public:
    
            F( int inc_v ): inc( inc_v ) {};
    
            int calc( int x )
            {
                    return x + inc;
            };
    };
    
    F a1( -10 );
    F a2( 11 );
    
    int f1( int x )
    {
            return x - 10;
    };
    
    int f2( int x )
    {
            return x + 11;
    };
    
            struct my_ftor
            {
                    F *obj;
                    int (F::*meth)(int);
                    int operator()(int x)
                    {
                            return (obj->*meth)( x );
                    };
                    my_ftor() {};
                    my_ftor( F *x, int(F::*y)(int) ) : obj(x), meth(y) {};
            };
    
    template<typename functor_type>
    void test( std::function<functor_type(int)> filler, char *name )
    {
            const int size = 1000;
            const int iters = 10000;
    
            int beg_time, end_time;
    
            functor_type funcs[ size ];
    
            beg_time = clock();
            for ( int i = 0; i < iters; i++ )
            {
                    for ( int j = 0; j < size; j++ )
                    {
                            funcs[ j ] = filler(j);
                    }
            };
            end_time = clock();
            float creation = ((float)(end_time - beg_time) / CLOCKS_PER_SEC);
    
            beg_time = clock();
            int res = 0;
            for ( int i = 0; i < iters; i++ )
            {
                    for ( int j = 0; j < size; j++ )
                    {
                            res = funcs[ j ]( res );
                    };
            };
            end_time = clock();
            float execution = ((float)(end_time - beg_time) / CLOCKS_PER_SEC);
            std::cout << name << " creation time: " << creation << " execution time: " << execution << " result: " << res << "\n";
    }
    
    int main(int c, char * * v) 
    {
            test<int(*)(int)>( [](int i) {return i % 2 ? f1 : f2; }, "simple &function test" );
    
            test<std::function<int(int)>>( [](int i) {return i % 2 ? f1 : f2; }, "functor &function test" );
    
            test<std::function<int(int)>>( [](int i) {return i % 2 ? std::bind( &F::calc, &a1, _1 ) : std::bind( &F::calc, &a2, _1 ); }, "functor &object test" );
    
            test<my_ftor>( [](int i) {return i % 2 ? my_ftor( &a1, &F::calc ) : my_ftor( &a2, &F::calc ); }, "obj->*meth struct test" );
    
            std::cout << "END\n";
            return 0;
    }

    http://ideone.com/1iNzR
    Чем код так долго занимается?
    simple &function test creation time: 0.05 execution time: 0.09 result: 5000000
    functor &function test creation time: 0.51 execution time: 0.14 result: 5000000
    functor &object test creation time: 1.25 execution time: 0.14 result: 5000000
    obj->*meth struct test creation time: 0.12 execution time: 0.05 result: 5000000
    END

    LispGovno, 05 Февраля 2014

    Комментарии (58)
  7. C++ / Говнокод #14503

    +39

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    #include <iostream>
    #include <memory>
    
    struct Test {
        ~Test() { std::cout << "~Test\n"; }
    };
    
    int main() {
      std::shared_ptr<void> ptr( new Test );
      return 0;
    }

    http://ideone.com/xXPWhE

    Но:

    #include <iostream>
    #include <memory>

    struct Test
    {
    ~Test() { std::cout << "~Test\n"; }
    };

    int main() {
    std::shared_ptr<void> ptr( (void*) new Test );
    return 0;
    }
    http://ideone.com/jhNvpJ

    LispGovno, 05 Февраля 2014

    Комментарии (14)
  8. C++ / Говнокод #14502

    +46

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    48. 48
    49. 49
    50. 50
    51. 51
    52. 52
    53. 53
    54. 54
    55. 55
    56. 56
    //Сом ненужный щит
    
    #include <iostream>
    using namespace std;
    
    #include <string>
    #include <iostream>
    
    struct Tracer {
       Tracer(void)
          :m_name("(none)")
       {
          std::cout << "[" << m_name << "]    " << __PRETTY_FUNCTION__ << std::endl;
       }
       Tracer(const std::string & name)
          :m_name(name)
       {
          std::cout << "[" << m_name << "]    " << __PRETTY_FUNCTION__ << std::endl;
       }
       Tracer(const Tracer & other)
          :m_name(other.m_name)
       {
          std::cout << "[" << m_name << "]    " << __PRETTY_FUNCTION__ << std::endl;
       }
       Tracer(const Tracer && other)
          :m_name(other.m_name)
       {
          std::cout << "[" << m_name << "]    " << __PRETTY_FUNCTION__ << std::endl;
       }
       Tracer & operator=(const Tracer & other) {
          m_name = other.m_name;
          std::cout << "[" << m_name << "]    " << __PRETTY_FUNCTION__ << std::endl;
          return *this;
       }
       Tracer & operator=(const Tracer && other) {
          m_name = other.m_name;
          std::cout << "[" << m_name << "]    " << __PRETTY_FUNCTION__ << std::endl;
          return *this;
       }
       ~Tracer() {
          std::cout << "[" << m_name << "]    " << __PRETTY_FUNCTION__ << std::endl;
          m_name="You looser!";
       }
    
       std::string m_name;
    };
    
    //Тот щит, ради чего всё затевалось
    
    template<class T> const T&  Min(const T &x, const T &y) { return (x.m_name < y.m_name) ? x : y; }
    
    int main() {
      const Tracer& mr = Min(Tracer("a"), Tracer("b"));
      cout<<"Some work with mr: "<<mr.m_name<<endl;
      return 0;
    }

    [b] Tracer::Tracer(const string&)
    [a] Tracer::Tracer(const string&)
    [a] Tracer::~Tracer()
    [b] Tracer::~Tracer()
    Some work with mr:

    Этож сколько я коммитов сделал с возвратом константной ссылки на константный параметр.

    LispGovno, 04 Февраля 2014

    Комментарии (29)
  9. C++ / Говнокод #14501

    +36

    1. 1
    2. 2
    3. 3
    t = Min(::std::cref(x), ::std::cref(y));
    //или в зависимости от ситуации 
    t = Min(::std::move(x), ::std::move(y));

    Абстракционизм или Кубизм?

    LispGovno, 04 Февраля 2014

    Комментарии (19)
  10. C++ / Говнокод #14475

    +31

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    13. 13
    14. 14
    15. 15
    16. 16
    17. 17
    18. 18
    19. 19
    20. 20
    21. 21
    22. 22
    23. 23
    24. 24
    25. 25
    26. 26
    27. 27
    28. 28
    29. 29
    30. 30
    31. 31
    32. 32
    33. 33
    34. 34
    35. 35
    36. 36
    37. 37
    38. 38
    39. 39
    40. 40
    41. 41
    42. 42
    43. 43
    44. 44
    45. 45
    46. 46
    47. 47
    #include <string>
    #include <iostream>
    #include <functional>
     
    using namespace std;
    
    template<class L, class... Ls>
    struct OL: L, OL<Ls...>{
       OL(const L& l, const Ls&... ls): L(l), OL<Ls...>(ls...){}
       using L::operator();
       using OL<Ls...>::operator();
    };
    
    template<class L1, class L2>
    struct OL<L1, L2>: L1, L2{
       OL(const L1& l1, const L2& l2): L1(l1), L2(l2){}
       using L1::operator();
       using L2::operator();
    };
    
    
    template<class... Ls>
    OL<Ls...> OverloadLambda(const Ls&... l){
       return OL<Ls...>(l...);
    }
    
    template<class L>
    L OverloadLambda(const L& l){
       return l;
    }
    
    void OverloadLambda(void) = delete;
    
    int main() {
       int i=5;
       auto lambda=OverloadLambda(  [=](int v){cout<<__func__<<" "<<i<<" "<<v<<" int"<<endl;},
                                    [=](string v){cout<<__func__<<" "<<i<<" "<<v<<" string"<<endl;},
                                    [=](float v){cout<<__func__<<" "<<i<<" "<<v<<" float"<<endl;});
       lambda(0);
       lambda("Hello");
       lambda(1.0f);
       i=600;
       auto lambda1=OverloadLambda( [=](int v){cout<<__func__<<" "<<i<<" "<<v<<" int"<<endl;});
       lambda1(4);
       ///*auto lambda0 =*/ OverloadLambda();
       return 0;
    }

    Специальная олимпиада объявляется открытой!
    http://ideone.com/y14z5Y
    Там много другого кода и какой-то из старых тем, а мне лень весь облазить.

    LispGovno, 03 Февраля 2014

    Комментарии (181)