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

    +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
    for (int i = 1; i++; i <= 20) {
        if (ExecSQL(...) >= 0) {
            Ok_rekord=true;
            break;
        }
        if (i == 20) {
            if (ExecSQL(...) < 0) {
                // показываем сообщение об ошибке
            } else {
                Ok_rekord=true;
            }
        }
    }

    Вот такой вот цикл для повтора при дедлоке...

    bormand, 20 Мая 2013

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

    +6

    1. 1
    http://www.work.ua/jobs/1286767/

    Я, конечно, знал, что С++ плох, но чтобы настолько...

    serpinski, 19 Мая 2013

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

    +17

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    int getFilesize(char* fname) {
        int f=open(fname, O_RDONLY);
        int size=0;
        if (f<0) {/*не важно*/}
        size=lseek(f, 0, SEEK_END);
        close(f);
        return size;
    }

    Писал прогу под линукс впервые. К концу написания уже знал что такое stat(), но как глянул в начало...)))))

    eax, 18 Мая 2013

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

    +12

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    // precondition: you already have a boost::shared_ptr<> to this or a derived object
    template<typename T>
    inline boost::shared_ptr<T> get_shared_ptr()
    { 
                // this cast lets the compiler verify the type compatibility
                assert( dynamic_cast<typename boost::shared_ptr<T>::element_type*>( &(*shared_from_this()) ) != 0);
                return *(boost::shared_ptr<T>*) &shared_from_this();
    }

    -

    blackhearted, 15 Мая 2013

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

    +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
    #define SET_VTYPE_AND_VARREF(type, val) \
      this->vt = VT_ ## type | VT_BYREF; \
      V_ ## type ## REF (this) = val;
    
    TVariantT& operator=(System::Currency* src)
    {
      Clear();
      if(src)
        SET_VTYPE_AND_VARREF(CY,
          reinterpret_cast<tagCY*>(&(src->Val)));
      return* this;
    }

    Быдлер такой быдлер
    стырено отсюда http://habrahabr.ru/company/pvs-studio/blog/179615/

    govnomonad, 14 Мая 2013

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

    +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
    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
    #include <iostream>
    
    namespace dynamic {
    
    template <class T> class scope;
    template <class T> class variable {
    public:
        variable() : initial(0), current(&initial) {}
        variable(const T &val) : initial(val, 0), current(&initial) {}
        operator T() { return current->val; }
        const T & operator = (const T & new_val) {
            return current->val = new_val;
        }
    private:
        struct node {
            node(node *prev) : val(), prev(prev) {}
            node(const T &val, node *prev) : val(val), prev(prev) {}
            T val;
            node *prev;
        };
        node initial;
        node *current;
        friend class scope<T>;
    };
    
    template <class T> class scope {
    public:
        scope(variable<T> &var) : var(var), node(var.current) {
            var.current = &node;
        }
        scope(variable<T> &var, const T &val) : var(var), node(val, var.current) {
            var.current = &node;
        }
        ~scope() {
            var.current = node.prev;
        }
    private:
        variable<T> &var;
        typename variable<T>::node node;
    };
    
    }
    
    
    dynamic::variable<int> x(100500);
    
    void foo() {
        std::cout << x << std::endl;
    }
    
    void bar() {
        dynamic::scope<int> x_(x, 42);
        foo();
        x = 265;
        foo();
    }
    
    int main() {
        foo();
        bar();
        foo();
        return 0;
    }

    Навеяно http://govnokod.ru/12993.

    https://ideone.com/7AA33Q

    bormand, 14 Мая 2013

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

    +20

    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
    #include <iostream>
    #include <cstring>
    
    const char tag[] = "Secret tag!";
    const size_t tagSize = sizeof(tag);
    const size_t nameSize = 32;
    
    template<class T>
    struct Value
    {
        Value(const char* name, const T& data) :
    data(data)
    {
        std::strncpy(this->tag, ::tag, tagSize);
        std::strncpy(this->name, name, nameSize);
    }
    char tag[tagSize];
    char name[nameSize];
    T data;
    };
    
    int getStackDir()
    {
        char a;
        char b;
        return &b > &a ? 1 : -1;
    }
    
    template<class T>
    T getValue(const char* name)
    {
        static const size_t stackSize = 1024 * 1024;
        const int stackDir = getStackDir();
        char begin;
        for(char* p = &begin, *end = &begin - stackSize * stackDir; p != end; p -= stackDir)
        {
            Value<T>* value = (Value<T>*)p;
            if(std::strcmp(value->tag, tag) != 0) continue;
            if(std::strcmp(value->name, name) != 0) continue;
            return value->data;
        }
        return T();
    }
    
    #define SET(type, name, value) Value<type> name(#name, value)
    #define GET(type, name) getValue<type>(#name)
    
    //-----------------------------------------------------------
    
    void test()
    {
        std::cout << GET(int, x) << std::endl;
    }
    
    int main()
    {
        SET(int, x, 13);
        test();
    }

    Отсюда http://www.rsdn.ru/forum/cpp/5163916.flat#5163916

    Artur, 13 Мая 2013

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

    +22

    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
    // поверка наличия указазанного флага в набора флагов
    bool __fastcall TfrmFieldsEditor::HasFlag(int nAFlag, int nAFlagsCollection)
    {
        bool bRetVal = false;
        std::bitset<8> bsFlagBits;
        bsFlagBits.reset();
        bsFlagBits = nAFlagsCollection;
        int nBitsCount = bsFlagBits.size();
        for(int i= 0 ; i < nBitsCount; ++i)
            {
            if(bsFlagBits[i]==1)
                {
                bsFlagBits.reset();
                bsFlagBits[i] = 1;
                if (bsFlagBits.to_ulong() == nAFlag)
                    {
                    bRetVal = true;
                    break;
                    }
                else
                    bsFlagBits = nAFlagsCollection;
                }
     
     
     
            }
        return bRetVal;
    }

    Ulysses, 13 Мая 2013

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

    +16

    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
    try
    {
       Application->Initialize();
    }
    catch (...)
     {
          try
         {
            throw Exception("");
           }
       catch (Exception &exception)
       {
          Application->ShowException(&exception);
       }
     }

    Найдено в проекте написанном на Borland C++Builder :)

    _Ru55_, 08 Мая 2013

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

    +13

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    bool[] Inmask;
    ....
            private void discreteOIForm_Load(object sender, EventArgs e)
            {
                if (icpI_measure.Connect() && icpO_measure.Connect())
                {
                    Inmask = new bool[1];
                    Inmask[Convert.ToInt32(0)] = true;
                }
            }

    Новоприбывший сотрудник откладывает вот такое. Это он сделал после совета использовать маски. Вы когда-нибудь видели массив из одного элемента? Я тоже нет.

    phys-tech, 07 Мая 2013

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