1. Си / Говнокод #12533

    +139

    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
    for( i=0; i<MAX_CHANNELS; i++ )
      {
        ht->ht_Voices[i].vc_Delta=1;
        ht->ht_Voices[i].vc_OverrideTranspose=1000;  // 1.5
        ht->ht_Voices[i].vc_SamplePos=ht->ht_Voices[i].vc_Track=ht->ht_Voices[i].vc_Transpose=ht->ht_Voices[i].vc_NextTrack = ht->ht_Voices[i].vc_NextTranspose = 0;
        ht->ht_Voices[i].vc_ADSRVolume=ht->ht_Voices[i].vc_InstrPeriod=ht->ht_Voices[i].vc_TrackPeriod=ht->ht_Voices[i].vc_VibratoPeriod=ht->ht_Voices[i].vc_NoteMaxVolume=ht->ht_Voices[i].vc_PerfSubVolume=ht->ht_Voices[i].vc_TrackMasterVolume=0;
        ht->ht_Voices[i].vc_NewWaveform=ht->ht_Voices[i].vc_Waveform=ht->ht_Voices[i].vc_PlantSquare=ht->ht_Voices[i].vc_PlantPeriod=ht->ht_Voices[i].vc_IgnoreSquare=0;
        ht->ht_Voices[i].vc_TrackOn=ht->ht_Voices[i].vc_FixedNote=ht->ht_Voices[i].vc_VolumeSlideUp=ht->ht_Voices[i].vc_VolumeSlideDown=ht->ht_Voices[i].vc_HardCut=ht->ht_Voices[i].vc_HardCutRelease=ht->ht_Voices[i].vc_HardCutReleaseF=0;
        ht->ht_Voices[i].vc_PeriodSlideSpeed=ht->ht_Voices[i].vc_PeriodSlidePeriod=ht->ht_Voices[i].vc_PeriodSlideLimit=ht->ht_Voices[i].vc_PeriodSlideOn=ht->ht_Voices[i].vc_PeriodSlideWithLimit=0;
        ht->ht_Voices[i].vc_PeriodPerfSlideSpeed=ht->ht_Voices[i].vc_PeriodPerfSlidePeriod=ht->ht_Voices[i].vc_PeriodPerfSlideOn=ht->ht_Voices[i].vc_VibratoDelay=ht->ht_Voices[i].vc_VibratoCurrent=ht->ht_Voices[i].vc_VibratoDepth=ht->ht_Voices[i].vc_VibratoSpeed=0;
        ht->ht_Voices[i].vc_SquareOn=ht->ht_Voices[i].vc_SquareInit=ht->ht_Voices[i].vc_SquareLowerLimit=ht->ht_Voices[i].vc_SquareUpperLimit=ht->ht_Voices[i].vc_SquarePos=ht->ht_Voices[i].vc_SquareSign=ht->ht_Voices[i].vc_SquareSlidingIn=ht->ht_Voices[i].vc_SquareReverse=0;
        ht->ht_Voices[i].vc_FilterOn=ht->ht_Voices[i].vc_FilterInit=ht->ht_Voices[i].vc_FilterLowerLimit=ht->ht_Voices[i].vc_FilterUpperLimit=ht->ht_Voices[i].vc_FilterPos=ht->ht_Voices[i].vc_FilterSign=ht->ht_Voices[i].vc_FilterSpeed=ht->ht_Voices[i].vc_FilterSlidingIn=ht->ht_Voices[i].vc_IgnoreFilter=0;
        ht->ht_Voices[i].vc_PerfCurrent=ht->ht_Voices[i].vc_PerfSpeed=ht->ht_Voices[i].vc_WaveLength=ht->ht_Voices[i].vc_NoteDelayOn=ht->ht_Voices[i].vc_NoteCutOn=0;
        ht->ht_Voices[i].vc_AudioPeriod=ht->ht_Voices[i].vc_AudioVolume=ht->ht_Voices[i].vc_VoiceVolume=ht->ht_Voices[i].vc_VoicePeriod=ht->ht_Voices[i].vc_VoiceNum=ht->ht_Voices[i].vc_WNRandom=0;
        ht->ht_Voices[i].vc_SquareWait=ht->ht_Voices[i].vc_FilterWait=ht->ht_Voices[i].vc_PerfWait=ht->ht_Voices[i].vc_NoteDelayWait=ht->ht_Voices[i].vc_NoteCutWait=0;
        ht->ht_Voices[i].vc_PerfList=0;
        ht->ht_Voices[i].vc_RingSamplePos=ht->ht_Voices[i].vc_RingDelta=ht->ht_Voices[i].vc_RingPlantPeriod=ht->ht_Voices[i].vc_RingAudioPeriod=ht->ht_Voices[i].vc_RingNewWaveform=ht->ht_Voices[i].vc_RingWaveform=ht->ht_Voices[i].vc_RingFixedPeriod=ht->ht_Voices[i].vc_RingBasePeriod=0;
    
        ht->ht_Voices[i].vc_RingMixSource = NULL;
        ht->ht_Voices[i].vc_RingAudioSource = NULL;
    
        memset(&ht->ht_Voices[i].vc_SquareTempBuffer,0,0x80);
        memset(&ht->ht_Voices[i].vc_ADSR,0,sizeof(struct hvl_envelope));
        memset(&ht->ht_Voices[i].vc_VoiceBuffer,0,0x281);
        memset(&ht->ht_Voices[i].vc_RingVoiceBuffer,0,0x281);
      }

    Щито оно делает?

    Govnocoder#0xFF, 03 Февраля 2013

    Комментарии (12)
  2. Си / Говнокод #12469

    +133

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    #if __BYTE_ORDER == __BIG_ENDIAN
    struct DWstruct { Wtype high, low;};
    #elif __BYTE_ORDER == __LITTLE_ENDIAN
    struct DWstruct { Wtype low, high;};
    #else
    #error Unhandled endianity
    #endif

    glibc, sysdeps/wordsize-32/divdi3.c

    bormand, 24 Января 2013

    Комментарии (18)
  3. Си / Говнокод #12447

    +110

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    int strnlen(const char *s, int size)
    {
            int i;
            for(i=0; i<size; i++) if(!*s) break;
            return i;
    }

    tz-lom, 18 Января 2013

    Комментарии (36)
  4. Си / Говнокод #12421

    +118

    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
    void dwflt_to_str(DWORD dw, char *pch, int &nsmb)
    {
      DWORD dw_a = dw;
      char ch_a;
      char tbldec[] = "0123456789";
      nsmb = 0;
      if (dw_a == 0) { pch[0] = '0'; nsmb++; goto lab2; }
      while (dw_a != 0)
      {
        pch[nsmb] = tbldec[dw_a%10]; dw_a /= 10; nsmb++;
      }
      dw_a = nsmb/2;
      while (dw_a)
      {
        ch_a = pch[nsmb - dw_a]; pch[nsmb - dw_a] = pch[dw_a - 1]; pch[dw_a - 1] = ch_a; dw_a--;
      }
    lab2:
      pch[nsmb] = 0;
    }
    
    const int n_fr2 = 7; // - эта константа определяет фиксированное число цифр после точки в выводимой строке, представляющей float.
    
    void float_to_str(float flt, char *pchar, int &nsmb)
    {
      int i, deg, ns_int, ns_frac;
      double frac_dbl;
      DWORD dw_f, mant, intg, fract;
      DW_FL f_flt;
      char szfl_int[16], szfl_frac[16];
      f_flt.fl = flt;
      dw_f = f_flt.dw;
      if (dw_f == 0) { pchar[0] = '0'; pchar[1] = '.'; pchar[2] = '0'; pchar[3] = 0; nsmb = 3; return; }
      if (dw_f & 0x80000000) { pchar[0] = '-'; } else { pchar[0] = '+'; }
      deg = int((dw_f & 0x7F800000) >> 23) - 127;
      mant = (dw_f & 0x007FFFFF) | 0x00800000;
      if (deg == 0) { intg = 1; fract = dw_f & 0x007FFFFF; goto lab_1; }
      if (deg > 0) { intg = mant >> (23 - deg); fract = ((dw_f & 0x007FFFFF) << deg) & 0x007FFFFF; goto lab_1; }
      if (deg < 0) { intg = 0; fract = ((dw_f & 0x007FFFFF) | 0x00800000) >> (-deg); }
    lab_1:
      frac_dbl = double(fract)*1.1920928955078125;// 1.1920928955078125 = 10^n_fr2 / 2^23 = 10^7 / 2^23
      fract = (int)frac_dbl;
    
      dwflt_to_str(intg, szfl_int, ns_int);
      nsmb = 1; i = ns_int; while (i) { pchar[i + 1] = szfl_int[i]; i--; } pchar[1] = szfl_int[0];
      nsmb += ns_int; pchar[nsmb] = '.'; nsmb++;
    
      dwflt_to_str(fract, szfl_frac, ns_frac); szfl_frac[n_fr2] = 0;
      i = ns_frac; while (i) { szfl_frac[6 - ns_frac + i] = szfl_frac[i - 1]; i--; }
      i = n_fr2 - ns_frac; while (i) { szfl_frac[i - 1] = '0'; i--; }
    
      i = n_fr2 - 1; while (i) { pchar[nsmb + i] = szfl_frac[i]; i--; } pchar[nsmb] = szfl_frac[0];
      nsmb += n_fr2; pchar[nsmb] = 0;
    }
    void float_to_str_exp(float flt, char *pchar, int &nsmb)
    {
      int i, deg, poli, ns_int, ns_frac, ns_poli;
      double frac_dbl;
      DWORD dw_f, mant, intg, fract;
      DW_FL f_flt;
      char szfl_int[16], szfl_frac[16];
      f_flt.fl = flt;
      dw_f = f_flt.dw;
      if (dw_f == 0) { pchar[0] = '0'; pchar[1] = '.'; pchar[2] = '0'; pchar[3] = 0; nsmb = 3; return; }
      if (dw_f & 0x80000000) { pchar[0] = '-'; } else { pchar[0] = '+'; }
      deg = int((dw_f & 0x7F800000) >> 23) - 127;
      mant = (dw_f & 0x007FFFFF) | 0x00800000;
      if (deg == 0) { intg = 1; fract = dw_f & 0x007FFFFF; goto lab_1; }
      if (deg > 0) { intg = mant >> (23 - deg); fract = ((dw_f & 0x007FFFFF) << deg) & 0x007FFFFF; goto lab_1; }
      if (deg < 0) { intg = 0; fract = ((dw_f & 0x007FFFFF) | 0x00800000) >> (-deg); }
    lab_1:
      frac_dbl = double(fract)*1.1920928955078125;// 1.1920928955078125 = 10^n_fr2 / 2^23 = 10^7 / 2^23
      fract = (int)frac_dbl;
    
      dwflt_to_str(intg, szfl_int, ns_int);
      dwflt_to_str(fract, szfl_frac, ns_frac); szfl_frac[n_fr2] = 0;
      if (intg != 0)
      {
        nsmb = 1; i = ns_int; while (i) { pchar[i + 2] = szfl_int[i]; i--; } pchar[1] = szfl_int[0];
        pchar[2] = '.'; nsmb += ns_int + 1; poli = ns_int - 1;
      }
      else
      {
        i = ns_frac - 1; while (i) { pchar[2 + i] = szfl_frac[i]; i--; } pchar[1] = szfl_frac[0];
        pchar[2] = '.'; nsmb = 3;//nsmb += ns_frac + 1;
        poli = ns_frac - n_fr2 - 1; goto lab_2;
      }
    
      i = ns_frac; while (i) { szfl_frac[6 - ns_frac + i] = szfl_frac[i - 1]; i--; }
      i = n_fr2 - ns_frac; while (i) { szfl_frac[i - 1] = '0'; i--; }
    
      i = n_fr2 - 1; while (i) { pchar[nsmb + i] = szfl_frac[i]; i--; } pchar[nsmb] = szfl_frac[0];
    lab_2:
      nsmb += n_fr2; pchar[nsmb] = 'E';
    
      int_to_str(poli, &pchar[nsmb + 1], ns_poli);
      nsmb += ns_poli + 1; pchar[nsmb] = 0;
    }

    оттуда

    TarasB, 15 Января 2013

    Комментарии (35)
  5. Си / Говнокод #12391

    +129

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    static int multipart_buffer_eof(multipart_buffer *self TSRMLS_DC)
    {
    	if ( (self->bytes_in_buffer == 0 && fill_buffer(self TSRMLS_CC) < 1) ) {
    		return 1;
    	} else {
    		return 0;
    	}
    }

    nyaknyan, 05 Января 2013

    Комментарии (4)
  6. Си / Говнокод #12380

    +137

    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
    GePolygon newPoly;
    int c_ai = 0, n_pi = 0;
    int fac[GE_MAX_POLY_VERTEX];
    newPoly.n = 0;
        
    Pt diff = ccpSub(end, bgn);
        
    int f = 0;
    do
       newPoly.p[newPoly.n] = ai[c_ai] == (n_pi - 1) ? (f = 1, a[c_ai++]) : (f = 0, inp->p[n_pi++]),
       fac[newPoly.n++] = f ? 0 : (ccpCross(ccpSub(newPoly.p[newPoly.n - 1], newPoly.p[newPoly.n - 2]), diff) > 0 ? 1 : -1 );
    while(n_pi < inp->n);
    if(c_ai < ac)
       newPoly.p[newPoly.n] = a[c_ai], fac[newPoly.n++] = 0;

    Из личной коллекции. Не помню, что конкретно делает, что-то вроде классификации точек полигона относительно линии...

    tirinox, 02 Января 2013

    Комментарии (8)
  7. Си / Говнокод #12379

    +135

    1. 001
    2. 002
    3. 003
    4. 004
    5. 005
    6. 006
    7. 007
    8. 008
    9. 009
    10. 010
    11. 011
    12. 012
    13. 013
    14. 014
    15. 015
    16. 016
    17. 017
    18. 018
    19. 019
    20. 020
    21. 021
    22. 022
    23. 023
    24. 024
    25. 025
    26. 026
    27. 027
    28. 028
    29. 029
    30. 030
    31. 031
    32. 032
    33. 033
    34. 034
    35. 035
    36. 036
    37. 037
    38. 038
    39. 039
    40. 040
    41. 041
    42. 042
    43. 043
    44. 044
    45. 045
    46. 046
    47. 047
    48. 048
    49. 049
    50. 050
    51. 051
    52. 052
    53. 053
    54. 054
    55. 055
    56. 056
    57. 057
    58. 058
    59. 059
    60. 060
    61. 061
    62. 062
    63. 063
    64. 064
    65. 065
    66. 066
    67. 067
    68. 068
    69. 069
    70. 070
    71. 071
    72. 072
    73. 073
    74. 074
    75. 075
    76. 076
    77. 077
    78. 078
    79. 079
    80. 080
    81. 081
    82. 082
    83. 083
    84. 084
    85. 085
    86. 086
    87. 087
    88. 088
    89. 089
    90. 090
    91. 091
    92. 092
    93. 093
    94. 094
    95. 095
    96. 096
    97. 097
    98. 098
    99. 099
    100. 100
    #include <stdio.h>
    #include <malloc.h>
    #include <sys/time.h>
    #include <pthread.h>
    
    #define MAXPRIME 10000001
    char sieve[MAXPRIME];
    
    typedef struct {
        int id, min, max, step;
        unsigned long long result;
    } Task;
    
    void primes() {
        printf("Searching prime numbers ...\n");
        sieve[0] = sieve[1] = 1;
        for (int i=2; i<MAXPRIME; i++)
            sieve[i] = 0;
        int i = 2;
        while (1) {
            while (i<MAXPRIME && sieve[i])
                i++;
            if (i >= MAXPRIME)
                break;
            for (int j=i*2; j<MAXPRIME; j+=i)
                sieve[j] = 1;
            i++;
        }
    }
    
    double utime() {
        struct timeval tv;
        gettimeofday(&tv, NULL);
        return tv.tv_sec + tv.tv_usec / 1000000.0;
    }
    
    unsigned long long calc(int thread, int min, int max, int step) {
        unsigned long long sum = 0;
        double start = utime();
        int nextshow = max+1;
        for (int n=max; n>=min; n-=step) {
            if (!sieve[n]) {
                sum += 1;
                continue;
            }
            if (n <= nextshow && n > min) {
                double elapsed = utime() - start, eta = elapsed/(max-n)*(n-min);
                printf("Thread %d: current=%d elapsed=%lfs eta=%lfh\n", thread, n, elapsed, eta/3600);
                nextshow = n < 10000 ? 0 : n - 10000;
            }
            int b;
            asm("movl %1, %%ecx\n"
                "1: dec %%ecx\n"
                "movl %%ecx, %%eax\n"
                "imull %%eax\n"
                "idivl %1\n"
                "cmpl %%ecx, %%edx\n"
                "jnz 1b\n"
                "movl %%ecx, %0"
                : "=g"(b)
                : "r"(n)
                : "%eax", "%ecx", "%edx");
            sum += b;
        }
        return sum;
    }
    
    void * thread(void *arg) {
        Task *task = arg;
        printf("Thread %d: working from %d to %d step %d\n", task->id, task->min, task->max, task->step);
        task->result = calc(task->id, task->min, task->max, task->step);
        printf("Thread %d: partial result is %llu\n", task->id, task->result);
        return NULL;
    }
    
    int main() {
        primes();
    
        int threads = 4;
        int max = 10000000;
        pthread_t tid[10];
        Task tasks[10];
    
        for (int i=0; i<threads; i++) {
            tasks[i].id = i;
            tasks[i].min = 1;
            tasks[i].max = max-i;
            tasks[i].step = threads;
            pthread_create(&tid[i], NULL, thread, &tasks[i]);
        }
    
        unsigned long long sum = 0;
        for (int i=0; i<threads; i++) {
            pthread_join(tid[i], NULL);
            sum += tasks[i].result;
        }
    
        printf("Result: %llu\n", sum);
        return 0;
    }

    Мое ужасное решение вот этой задачки: http://projecteuler.net/problem=407

    В день, когда математика упорно не желает вспоминаться...
    на помощь приходят брутальные и бессердечные ассемблер и мультитрединг.

    model name: Pentium(R) Dual-Core CPU E5400 @ 2.70GHz
    real 286m45.890s
    user 545m44.926s

    bormand, 01 Января 2013

    Комментарии (47)
  8. Си / Говнокод #12360

    +111

    1. 1
    2. 2
    for(p=first; p!=NULL; p=p->next)
      free(p);

    Освобождаем память всех элементов списка.

    taburetka, 26 Декабря 2012

    Комментарии (16)
  9. Си / Говнокод #12351

    +135

    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
    #include<stdio.h>
    #include<stdlib.h>
    #include<string.h>
    #define MAXSIZE 512
    
    typedef struct word
    {
    	char wordBody[MAXSIZE];
    	int count;
    	struct word* next;
    } Word;
    
    Word* alloc(char* incomeWord)
    {
    	Word* ret;
    	ret = (Word*)malloc(sizeof(Word));
    	ret->count=1;
    	ret->next=NULL;
    	strcpy(ret->wordBody, incomeWord);
    }
    
    void insert(Word **n, char* incomeWord)
    {
    	Word** p_p_n;
    	int h=0;
    
    	if(*n==NULL)
    	{
    		*n = alloc(incomeWord);
    		return;
    	}
    
    	for(p_p_n = n; *p_p_n!=NULL; p_p_n = &(*p_p_n)->next)
    	{
    		if((strcmp(incomeWord,&(*p_p_n)->wordBody))==0)
    		{
    			(*p_p_n)->count++;
    			return;
    		}
    	}
    
    	for(p_p_n = n; *p_p_n!=NULL; p_p_n = &(*p_p_n)->next)
    	{
    		Word* ins = alloc(incomeWord);
    		Word* temp = *p_p_n;
    		Word** tt;
    		int is=0;
    		tt=p_p_n;
    		ins->next=temp;
    		*p_p_n = ins;
    		break;
    	}
    }
    
    void print(Word* n)
    {
    	while(n!=NULL) {
    		if(n->count > 1)
    		{
    			printf("%s %d\n", n->wordBody, n->count);
    		}
    		n=n->next;
    	}
    }
    
    int main(void)
    {
    	char buf[MAXSIZE]={'\0'};
    	FILE *fr;
    	Word* sez=NULL;
    	fr=fopen("Text1.txt", "r");
    
    	while(!feof(fr))
    	{
    		fscanf(fr,"%s",buf);
    		insert(&sez,buf);
    	}
    
    	print(sez);
    	printf("\n%d\n", sizeof(sez));
    	fclose(fr);
    	return 0;
    }

    Считаем сколЬко раз каждое слово встречается в текстовом файле. Программа выполняется 6.5 минут с файлом размером 850 килобайт.

    taburetka, 25 Декабря 2012

    Комментарии (36)
  10. Си / Говнокод #12342

    +132

    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
    Было:
    
    typedef struct {
        int range;      /*enabled flag*/
    
        union {
            struct {
                unsigned str_length;
            };
            struct {
                long i_min;
                long i_max;
            };
            struct {
                double d_min;
                double d_max;
            };
            /* ... */
        };
    } wlp_range_t;
    
    #define WLP_NO_RANGE()              { .range = FALSE }
    #define WLP_STRING_LENGTH(length)   { .range = TRUE, .str_length = length}
    #define WLP_INT_RANGE(min, max)     { .range = TRUE, .i_min = min, .i_max = max }
    #define WLP_FLOAT_RANGE(min, max)   { .range = TRUE, .d_min = min, .d_max = max }
    
    Стало:
    
    typedef struct {
        int range;      /*enabled flag*/
    
        struct {
            struct {
                unsigned str_length;
            };
            struct {
                long i_min;
                long i_max;
            };
            struct {
                double d_min;
                double d_max;
            };
            /* ... */
    
        };
    } wlp_range_t;
    
    #define WLP_NO_RANGE()              { B_FALSE, { {0} } }
    #define WLP_STRING_LENGTH(length)   { B_TRUE, { {length} } }
    #define WLP_INT_RANGE(min, max)     { B_TRUE, { {0}, {min, max} } }
    #define WLP_FLOAT_RANGE(min, max)   { B_TRUE, { {0}, {0, 0}, {min, max} } }
    #define WLP_SIZE_RANGE(min, max)    { B_TRUE, { {0}, {0, 0}, {0.0, 0.0}, {min, max} } }
    
    /* ... */

    Такая вот бяка получилась при портировании проекта с GCC на Visual C. А все потому, что:
    1. ISO C++03 8.5.1[dcl.init.aggr]/15:
    | When a union is initialized with a brace-enclosed initializer,
    | the braces shall only contain an initializer for the first member of the union.
    2. Visual C не поддерживает designated initializers

    myaut, 24 Декабря 2012

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