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

    +18

    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
    /*
     *      The use of singletons for globals makes globals not
     *      actually be initialized until it is first needed, this
     *      makes the library faster to load, and have a smaller
     *      memory footprint
     */
    
    #define json_global_decl(TYPE, NAME, VALUE)                              \
    class jsonSingleton ## NAME {                                            \
    public:                                                                  \
            inline static TYPE & getValue() json_nothrow {                   \
                    static jsonSingleton ## NAME single;                     \
                    return single.val;                                       \
            }                                                                \
    protected:                                                               \
            inline jsonSingleton ## NAME() json_nothrow : val(VALUE) {}      \
            TYPE val;                                                        \
    }
    
    #define json_global(NAME) jsonSingleton ## NAME::getValue()              \
    
    json_global_decl(json_string, CONST_TRUE, JSON_TEXT("true"));
    json_global_decl(json_string, CONST_FALSE, JSON_TEXT("false"));
    json_global_decl(json_string, CONST_NULL, JSON_TEXT("null"));
    
    /* Использование */
    json_global(ERROR_NULL_IN_CHILDREN)

    Наткнулся на утечку памяти, отловленную Valgrind'ом
    А внутри вот это. И главное, совершенно непонятно, зачем воротить эту свистопляску, если линкеры умеют распознавать "true" по всюду (читаем про ROMability тут: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2002/n1396.pdf )
    http://stackoverflow.com/questions/690176/c-c-optimization-of-pointers-to-string-constants

    Сам код отсюда: http://code.google.com/p/wot-replay-parser/source/browse/libjson/Source/JSONGlobals.h

    myaut, 10 Декабря 2012

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

    +25

    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
    95. 95
    96. 96
    97. 97
    public OnPlayerSpawn(playerid) {
        IsSpawned[playerid]=1;
        if(Team[playerid]==ZOMBIE) { TextDrawSetString(Textdraw9[playerid],"You are a ~r~Zombie~w~. Kill & Infect every Human to complete your mission.");
    	} else if(Team[playerid]==HUMAN) { TextDrawSetString(Textdraw9[playerid],"You are a ~r~Human~w~. Prevent Zombie attacks and survive till the end."); }
        TextDrawHideForAll(Box); TextDrawHideForAll(text_Top5[0]); TextDrawHideForAll(text_Top5[1]);
    	TextDrawShowForPlayer(playerid, Textdraw0);	 TextDrawShowForPlayer(playerid, Textdraw1);
    	TextDrawShowForPlayer(playerid, Textdraw2);	 TextDrawShowForPlayer(playerid, Textdraw3);
    	TextDrawShowForPlayer(playerid, Textdraw4);	 TextDrawShowForPlayer(playerid, Textdraw5);
    	TextDrawShowForPlayer(playerid, Textdraw6);	 TextDrawShowForPlayer(playerid, Textdraw7);
    	TextDrawShowForPlayer(playerid, Textdraw9[playerid]);
    	TextDrawShowForPlayer(playerid, Textdraw15); TextDrawShowForPlayer(playerid, Textdraw16[playerid]);
    	SetTeam(playerid); SetClass(playerid); 
    	if(Infection==1) {
            Infection=0;
            if(Totalplayers>=2) {
    			SetTimer("InfectionLoad",0,0);
    		} else if(Totalplayers>=10) {
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    		}else if(Totalplayers>=10) {
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    		}else if(Totalplayers>=20) {
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    		}else if(Totalplayers>=30) {
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    		}else if(Totalplayers>=45) {
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    		}else if(Totalplayers>=60) {
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    		}else if(Totalplayers<=75) {
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    			SetTimer("InfectionLoad",0,0);
    		}
    	} return 1; }

    Серьезно?

    d3n4, 08 Декабря 2012

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

    +1

    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
    #include "stdafx.h"
    #include <iostream>
    #include <stdio.h>
    using namespace std;
    class radian
    {protected:
        double x;
        public:
        radian();//конструктор без параметров, возвращает дежурное значение
        radian(double x1);//конструктор с параметрами
        radian sum_rad(radian a );
        radian unsum_rad(radian a);//разность 2-х радиан
        double get_r(){return x;}//нужный геттр
        void set_r(double x1);//бесполезный сеттрер
        double radian::quarter(radian a);//определение четверти угла заданного в радианах
        double operator *();//перегрузка операции. возводит во вторую степень
        double radian::rad_to_grad();//перевод из радиан в градусы
        ~radian();//деструкторуу
    };
    class namerad:public radian
    {
        private:
        string name;
        public:
        namerad::namerad(double corner,string name)
        {set_name(name);}
        void namerad::set_name(string newname)
        {name=newname;}
        void namerad::get_name(string currentname)
        {currentname=name;}
    };
    radian::radian()//конструктор по умолчанию
    {
        x=0;
    }
    radian::radian(double x1)//конструктор с 1 параметром
    {
        x=x1;
    }
    radian radian::sum_rad(radian a)//сумма 2-х радиан
    {radian n(x+a.x);
        return n;
    }
    void radian::set_r( double x1)//сеттер
    { x=x1;}
    radian::~radian() {x=0;}//деструктор
    radian radian::unsum_rad(radian a)//разность 2-х радиан
    {radian n(x-a.x);
        return n;
    }
    double radian::rad_to_grad() //перевод из радиан в градусы
    {
        return(x*(180/3.14));
    }
    double radian ::operator *()//перегрузка операции
    {
        return x*x;
    }
    double radian::quarter(radian a)//определение четверти угла заданного в радианах
    {while(a.x>6.28){a.x-=6.28;}
            if ((a.x >=0)&&(a.x<=1.57)) return 1;
            if ((a.x>=1.57)&&(a.x<=3.14)) return 2;
            if ((a.x>=3.14)&&(a.x<=4.71)) return 3;
            return 4;
        }
        int main()
        {
            radian a,b,c(5.0),d(6.20);
            cout<<"summa=";b=c.sum_rad(d);cout<<b.get_r() << endl;//сумма
            cout<<"raznost=";b=d.unsum_rad(c);cout<<b.get_r() << endl; //разность
            cout<<"chetvert: ";cout<< d.quarter(d) << endl;//четверть
            cout<<"iz radian v gradusi: ";cout<<d.rad_to_grad() << endl;//перевод из радиан в градусы
            cout<<"peregruz sqr "<<*c;//возведение в квадрат. перегруженная операция
            system ("PAUSE");
            return 0;
        }

    vovkalamer, 03 Декабря 2012

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

    +12

    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
    #include <iostream>
    #include <functional>
     
    using namespace std;
     
    struct  T
    {
        int a;
        T(const T&):a(1){cout<<"copy_construct T"<<endl;}
        T():a(1){cout<<"construct T"<<endl;}
        ~T(){a=0;cout<<"destruct T"<<endl;}
        const T& operator=(const T&){cout<<"copy_ass T"<<endl;return *this;}    
    };
     
    struct M: public T{};
     
    void f(const T& fa)
    {
        cout<<"fa.a= "<<fa.a<<endl;    
    }
     
    int main() {
        f(std::cref(T()));
        cout<<endl;
        const T& b = T();
        cout<<"b.a= "<<b.a<<endl;
        cout<<endl;
        const T& a = std::cref(T());
        cout<<"a.a= "<<a.a<<endl;
        return 0;
    }

    http://ideone.com/BmHo9w
    Есть на этом ресурсе великий знаватель крестов и вот он меня уверял, что объект, на который ссылается ссылка - должен дожить до конца выхода ссылки из скоупа. Почему мы этого не наблюдаем? А знаватель? Ты меня прямо даже убедил, и тут такая подстава от тебя. a - не дожил до конца.

    LispGovno, 03 Декабря 2012

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

    +6

    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
    template <typename T, size_t rows, size_t cols>
    class Matrix {
    public:
        Matrix() :
            m_matrix(reinterpret_cast<T*>(new char[sizeof(T) * rows * cols]))
        {
            memset(m_matrix, 0, sizeof(T) * rows * cols);
            new (m_matrix) T[rows * cols];
    
            if ( rows == cols ) {
                for ( size_t i = 0; i < rows; i++ )
                    m_matrix[i * cols + i] = 1; // FIXME: this is hack
            }
    
        }
    
        // ...
    private:
        T *m_matrix;
    };

    Из прошлого куска.

    Инициализируем память нулями. А вдруг тип скалярный? :)

    Elvenfighter, 01 Декабря 2012

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

    +10

    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
    template <typename T, size_t rows, size_t cols>
    class Matrix {
    public:
        Matrix() :
            m_matrix(new T[rows * cols])
        {
            // Make identity matrix, if possible
            if ( rows == cols ) {
                for ( size_t i = 0; i < rows; i++ )
                    m_matrix[i * cols + i] = 1; // FIXME: this is hack
            }
    
        }
    
        // ...
    
        Matrix<T, rows, cols>& operator =(Matrix<T, rows, cols> &&other) {
            if ( this != &other ) {
                delete [] m_matrix;
                m_matrix = other.m_matrix;
    
                other.m_matrix = new T[cols * rows];
                other = static_cast<const Matrix&>(Matrix());
            }
    
            return *this;
        }
    
        // ...
    };

    Издержки move construtor :)

    Прошу внимания к строчкам 19-23

    Elvenfighter, 30 Ноября 2012

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

    +14

    1. 1
    int main(){(([](){})());}

    preview.tinyurl.com/blrtfuo
    ideone.com/BXrXDR
    Или еще чуть веселее:
    ideone.com/C425yo

    Xom94ok, 30 Ноября 2012

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

    +2

    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
    template <typename A, typename B> 
    class ololo 
    {
    };
    
    template <typename A>
    class ololo <A, int>
    {
    };
    
    template <typename A>
    void bububu ()
    {
    }
    
    template <>
    void bububu <int> ()
    {
    }
    
    template <typename A, typename B> 
    void kokoko  ()
    {
    }
    
    template <typename A>
    void kokoko <A, int> ()
    {
    }

    http://www.gamedev.ru/flame/forum/?id=169781
    tarasboproblemi

    LispGovno, 29 Ноября 2012

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

    +19

    1. 1
    2. 2
    3. 3
    4. 4
    Class1* c1 = (Class1*)malloc(sizeof(Class1)*N);
    Class2* c2 = (Class2*)malloc(sizeof(Class2)*N);
    for (long i = 0; i < N; i++) c1[i] = Class1();
    for (long i = 0; i < N; i++) c2[i] = Class2();

    Рассказать ему про new[] / delete[]?

    runewalsh, 29 Ноября 2012

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

    +12

    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
    #include <stdio.h>
    #include <type_traits>
    #include <string>
    struct hack_t{};
    template<class TYPE>static hack_t operator&(const TYPE&,hack_t){return hack_t();}
    int main()
    {
      struct type{};
      std::string var="win";
      #define get_meta(var)[&]()->bool{hack_t unnamed;hack_t foo(var&unnamed);return std::is_function<decltype(foo)>::value;}()
      bool result_0=get_meta(var);
      bool result_1=get_meta(type);
      #undef get_meta
      printf("get_meta(var) == %s\n",result_0?"true":"false");
      printf("get_meta(type) == %s\n",result_1?"true":"false");
      return 0;
    }

    Код отличает переменную от типа.
    http://ideone.com/t7BBO4
    Сами знаете откуда.

    LispGovno, 27 Ноября 2012

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