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

    +142

    1. 1
    2. 2
    #define PHYSICAL        unsigned long
    #define VIRTUAL         unsigned long

    прикольное legacy

    cerevra, 13 Февраля 2015

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

    +133

    1. 1
    2. 2
    3. 3
    4. 4
    5. 5
    6. 6
    7. 7
    8. 8
    9. 9
    int main(void)
    {
    	int a=0,b=1000;
    	int * p;
    	p=malloc(sizeof(int)*b-7); // уменьшаем выделяемую память на 7 байт, а почему оно не падает?
    	for(;a<b;a++) p[a]=a;
    	printf("%lu",sizeof(int)*b);
    	free(p);
    }

    а если убрать 8 байт то уже падает,что-то где-то округляется что-ли?

    pl7ofit, 10 Февраля 2015

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

    +109

    1. 1
    memcpy (stderr, stdout, sizeof (FILE));

    gpr, 06 Февраля 2015

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

    +143

    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
    98. 98
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    unsigned int board[4][4];
    void firstblood(); void drawboard(); int turn(); void changeup(); void changedown(); void changeleft(); void changeright(); int numbofpos(); void gameover(); 
    int pow2 ( int n ) { return 1<<n; }
    
    int main ()
    {	do{   firstblood();   drawboard();    while ( turn() );	puts("New game? (y,n)");	fflush(stdin);	char c=getchar();	if (c!='y') return 0;	for (int i=0;i<4;i++) for (int j=0;j<4;j++) board[i][j]=0;	} while (1);
    }
    
    void firstblood()
    { srand((unsigned int)time); board[rand()%4][rand()%4]=pow2(rand()%3+1);    board[rand()%4][rand()%4]=pow2(rand()%4+1); }
    
    void drawboard ()
    {   system("CLS");    puts("        2048        ");  int i,j;  for (i=0;i<4;i++, printf("\n\n") )        for (j=0;j<4;j++)            printf("%5u",board[i][j]);    puts("\nw,a,s,d and r to move and new game");
    }
    
    int numbofpos()
    {   int i,j,n=0;  for (i=1;i<4;i++      for (j=0;j<4;j++){if (board[i][j] && board[i][j]==board[i-1][j]) n++;	if (board[j][i] && board[j][i]==board[j][i-1]) n++;	}for (i=0;i<4;i++) for (j=0;j<4;j++) if (!board[i][j]) n++;
        return n ;
    }
    
    void gameover()
    {
        system("CLS");    puts("        2048        ");   int i,j;   for (i=0;i<4;i++, printf("\n\n") )     for (j=0;j<4;j++)     printf("%5u",board[i][j]);    puts("\n     Game over      ");
    }
    
    int turn()
    {
        char c=getchar(); int i,j,n=0, nulls[16][2];
        switch(c)
        {   case 'w' : changeup(); break;
            case 'a' : changeleft(); break;
            case 's' : changedown(); break;
            case 'd' : changeright(); break;
    		case 'r' : return 0;
    	}
        for (i=0;i<4;i++)
            for (j=0;j<4;j++)
                if ( !board[i][j] ) { nulls[n][0]=i; nulls[n++][1]=j; }
    	if (n)
    	{
    		int t=rand()%n;
    		board[ nulls[t][0] ][ nulls[t][1] ] = pow2(rand()%2+1);
    	}
        if ( !numbofpos() )
            { gameover(); return 0; }
        drawboard();
    	return 1;
    }
    void changeleft()
    {
        int i,j,k;
        for (i=0;i<4;i++)
        {
            int f= ( !board[i][3] )?0:1 ;
            for (j=2;j>=0;j--)
              { if (board[i][j] && !f) f=1;
    			if (!board[i][j] && f==1) { for (k=j+1;k<4;board[i][k-1]=board[i][k],k++); board[i][3]=0; }
    		  }
    		for (j=0;j<3;j++)
    			if ( board[i][j] && board[i][j]==board[i][j+1])  
    			{
    				board[i][j] *= 2;
    				for (k=j+1;k<3; board[i][k]=board[i][k+1], k++ );
    				board[i][3]=0;
    			}
    	}
    }
    void changeright()
    {
    	int i,j,k;
        for (i=0;i<4;i++)
        {
            int f= ( !board[i][0] )?0:1 ;
            for (j=1;j<4;j++)
              { if (board[i][j] && !f) f=1;
    			if (!board[i][j] && f==1) { for (k=j;k>0;board[i][k]=board[i][k-1],k--); board[i][0]=0; }
    		  }
    		for (j=3;j>0;j--)
    			if ( board[i][j] && board[i][j]==board[i][j-1])  
    			{
    				board[i][j] *= 2;
    				for (k=j-1;k>0; board[i][k]=board[i][k-1], k-- );
    				board[i][0]=0;
    			}
    	}
    }
    
    void changeup()
    {	int i,j,k;    for (i=0;i<4;i++)    {        int f= ( !board[3][i] )?0:1 ;        for (j=2;j>=0;j--)          { if (board[j][i] && !f) f=1;			if (!board[j][i] && f==1) { for (k=j+1;k<4;board[k-1][i]=board[k][i],k++); board[3][i]=0; }		  }
    		for (j=0;j<3;j++)			if ( board[j][i] && board[j][i]==board[j+1][i])  {board[j][i] *= 2;			for (k=j+1;k<3; board[k][i]=board[k+1][i], k++ );	board[3][i]=0;	}}}
    
    void changedown()
    {int i,j,k;    for (i=0;i<4;i++) {       int f= ( !board[0][i] )?0:1 ;       for (j=1;j<4;j++)          { if (board[j][i] && !f) f=1;			if (!board[j][i] && f==1) { for (k=j;k>0;board[k][i]=board[k-1][i],k--); board[0][i]=0; }		  }		for (j=3;j>0;j--)			if ( board[j][i] && board[j][i]==board[j-1][i])  				board[j][i] *= 2;
    				for (k=j-1;k>0; board[k][i]=board[k-1][i], k-- );			board[0][i]=0;
    }	}}

    2048 только с библиотеками stdio.h, stdlib.h (srand,rand) и time.h (тоже для рандома)

    Пожалуйста, уберите ограничение в 100 строк

    post_skript, 05 Февраля 2015

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

    +124

    1. 1
    value += (0<<17);   // PARK bit

    codemonkey, 03 Февраля 2015

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

    +143

    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>
    
    int main(void)
    {
    	int d2[3][3][3];
    	int i,i2,i3;
    	for(i = 0; i <= 3; ++i)
    		for(i2 = 0; i2 <= 3; ++i2)
    			for(i3 = 0; i3 <= 3; ++i3)
    				d2[i][i2][i3]=5;
    	i=0,i2=0,i3=0;
    	for(i = 0; i <= 3; ++i)
    		for(i2 = 0; i2 <= 3; ++i2)
    			for(i3 = 0; i3 <= 3; ++i3)
    				printf("%d\n",d2[i][i2][i3]);
    	return 0;
    }

    играюсь я короче с массивом, этот код компилируется,все печатает но в конце Segmentation fault, почему?
    gcc -Wall -Wextra -Werror -Wpedantic -ftrapv -fwrapv -fdiagnostics-show-option -std=gnu11 -o "test" "test.c"

    pl7ofit, 01 Февраля 2015

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

    +130

    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
    char* obrab_stroki(char*, int);
    int move(char*);
    int action_register(char*);
    int speed(char*);
    int condition(char*);
    
    char answer1[25];
    uint8_t Output[512];
    uint32_t number_for_output=16;
    typedef struct { char *name; uint8_t adress; } struct_label;
    typedef struct { char *name; uint8_t adress; } struct_goto_label;
    //----------------------------------------------------------------------------------------
    while (strcmp(qwerty[++string_count],""))
    	{
    		str=obrab_stroki(qwerty[string_count], 0);
    		printf("s:%s\n",str);
    		
    		if (!strcmp(qwerty[string_count]+strlen(qwerty[string_count])-1,"{")) fig_skob++;
    		if (!strcmp(qwerty[string_count]+strlen(qwerty[string_count])-1,"}")) fig_skob--;				
    		if (!strcmp(qwerty[string_count]+strlen(qwerty[string_count])-1,"{") && !strcmp(str,"if")) { fig_skob_if++; adres_return[fig_skob_if]=number_for_output+3; }
    		if (!strcmp(qwerty[string_count]+strlen(qwerty[string_count])-1,"}") && fig_skob_if>0) { Output[adres_return[fig_skob_if]]=number_for_output; fig_skob_if--;}
    		if (fig_skob==0)
    		{
    			Output[number_for_output++]=0;
    			Output[number_for_output++]=0;
    		}
    
    		if (!strcmp(qwerty[string_count]+strlen(qwerty[string_count])-1,":")) 
    		{ 
    			label[number_label].name=qwerty[string_count];
    			label[number_label++].adress = number_for_output;
    			continue;
    		}				
    //-------------------------------------------------------------------------------------------
    if (!strcmp(str,"r")) if (action_register(qwerty[string_count]) == 1) {printf("ERROR in string: %d", string_count); return 1;} else continue;
    		if (!strcmp(str,"move")) if (move(qwerty[string_count]) == 1) {printf("ERROR in string: %d", string_count); return 1;} else continue;
    		if (!strcmp(str,"speed")) if (speed(qwerty[string_count]) == 1) {printf("ERROR in string: %d", string_count); return 1;} else continue;
    		if (!strcmp(str,"if")) if (condition(qwerty[string_count]) == 1) {printf("ERROR in string: %d", string_count); return 1;} else continue;
    		
    //-----------------------------------------------------------------------------------------
    char* obrab_stroki(char* qwerty, int i)
    {
    	int count;
    	char asnwer1[25];
    
    	for (count=0; (qwerty[count+i]<='z' && qwerty[count+i]>='a') || (qwerty[count+i]<='Z' && qwerty[count+i]>='A'); count++)
    	{
    		answer1[count]=qwerty[count+i];
    	}
    	answer1[count]='\0';
    
    	return answer1;
    }//тут как бы нет ошибок =)
    
    //----------------------------------------------------------------------------------------------
    if (qwerty[i]=='-')
    	{
    		if (qwerty[i+1]=='1' && qwerty[i+2]=='0')
    			{
    				Output[number_for_output++]=answer|MOTOR_SPEC_SPEED_DEC;
    				Output[number_for_output++]=0;
    				return 0;
    			}
    		else
    			return 1;
    	}

    Человек писал компилятор. Самые эпичные моменты

    epichniygovnokoder, 31 Января 2015

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

    +133

    1. 01
    2. 02
    3. 03
    4. 04
    5. 05
    6. 06
    7. 07
    8. 08
    9. 09
    10. 10
    11. 11
    12. 12
    #include <stdio.h>
    int main () {
    char c;
    int result=0;
    while(scanf("%c",&c) ) {
    if( (c<'0') || (c > '1') ) break;
    result«=1;
    result+=c-'0';
    };
    printf("%d",result);
    return 0;
    }

    Этот код переводит число из двоичной системы в десятичную. Зачем-то основано на работе с символами.

    alexmir, 29 Января 2015

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

    +134

    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
    void MSSequencerPatternCopyToMusicTrack(MSSequencerPatternRef self, MusicTrack track)
    {
        // Get signature and length of pattern
        TimeSignature sign   = MSSequencerPatternGetTimeSignature(self);
        CABarBeatTime length = pattern_barbeat_duration_without_mutes(self);
        CABarBeatTime insert = CABarBeatTime(1, 1);
        // Get muted beats
        CFRange *mutedBeats = (CFRange*)CFDataGetBytePtr(self->mutedBeats);
        CFIndex  mutedCount = CFDataGetLength(self->mutedBeats) / sizeof(CFRange);
        // Copy with muted regions
        if (mutedCount > 0)
        {
            // Clear output track
            MSSequencerTrackClear(self->parent, track);
            // Copy phrase by phrase
            for (int i = 0; i < mutedCount; ++i)
            {
                CFRange muteRange  = mutedBeats[i];
                CFIndex beatsCount = (sign.numerator * sign.denominator);
                
                if ((beatsCount * (i + 1)) > muteRange.location + muteRange.length)
                {
                    if (muteRange.length > 0)
                    {
                        // Copy beats before mute range
                        if (muteRange.location > (beatsCount * i))
                        {
                            CFIndex start = (beatsCount * i);
                            CFIndex end   = muteRange.location;
                            insert = copy_beats_to_track_from_beat_to_beat(self, track, sign, insert, start, end);
                        }
                        // Copy beats after range
                        {
                            CFIndex start = (muteRange.location + muteRange.length);
                            CFIndex end   = (beatsCount * (i + 1));
                            insert = copy_beats_to_track_from_beat_to_beat(self, track, sign, insert, start, end);
                        }
                    }
                    else
                    {
                        // Copy without mutes
                        CFIndex start = (beatsCount * i);
                        CFIndex end   = (beatsCount * (i + 1));
                        insert = copy_beats_to_track_from_beat_to_beat(self, track, sign, insert, start, end);
                    }
                }
                else
                {
                    // Copy beats
                    CFIndex firstBeat = (muteRange.location + muteRange.length) % beatsCount;
                    CABarBeatTime start = CABarBeatTimeAddBeats(CABarBeatTime(1, 1), sign, (beatsCount * i) + firstBeat);
                    CABarBeatTime end   = CABarBeatTimeAddBeats(start, sign, (beatsCount - muteRange.length));
                    copy_beats_from_pattern_to_track(self, track, start, end, insert);
                    //CFLog("start: {%i, %i}, end: {%i, %i}, insert: {%i, %i}", (int)start.bar, (int)start.beat, (int)end.bar, (int)end.beat, (int)insert.bar, (int)insert.beat);
                    // Update insert time
                    insert = CABarBeatTimeAddBeats(insert, sign, (beatsCount - muteRange.length));
                }
            }
        }
        // Copy without muted regions
        else
        {
            copy_beats_from_pattern_to_track(self, track, insert, length, insert);
        }
        //CAShow(track);
    }

    Вот такая вот какашечка...

    gerasim13, 28 Января 2015

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

    +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
    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 a portable version of timegm(), set the TZ environment variable  to
    // UTC, call mktime(3) and restore the value of TZ.  Something like
    
    #include <time.h>
    #include <stdlib.h>
    
    time_t
    my_timegm(struct tm *tm)
    {
        time_t ret;
        char *tz;
    
        tz = getenv("TZ");
        if (tz)
            tz = strdup(tz);
        setenv("TZ", "", 1);
        tzset();
        ret = mktime(tm);
        if (tz) {
            setenv("TZ", tz, 1);
            free(tz);
        } else
            unsetenv("TZ");
        tzset();
        return ret;
    }

    Цитата из man timegm. Сборка unix timestamp из компонент (год, месяц и т.п.).

    Удобно, наглядно, потокобезопасно.

    bormand, 23 Января 2015

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